fixed bug in pkmn, moved pkmn msg counter to cog

This commit is contained in:
phixxy 2024-01-20 22:28:36 -08:00
parent d90c8a4418
commit 6aede9296b
2 changed files with 92 additions and 91 deletions

View file

@ -139,90 +139,105 @@ class PokemonGame(commands.Cog):
embed.add_field(name="Buddy XP", value=buddy_xp, inline=True) embed.add_field(name="Buddy XP", value=buddy_xp, inline=True)
embed.add_field(name="Types", value=type_str, inline=False) embed.add_field(name="Types", value=type_str, inline=False)
return embed return embed
try:
if args[0]=='start': if args[0]=='start':
if not os.path.isdir("databases/pokemon/"): if not os.path.isdir("databases/pokemon/"):
os.makedirs("databases/pokemon/") os.makedirs("databases/pokemon/")
if not os.path.isfile("databases/pokemon/"+str(ctx.author.id)+'.json'): if not os.path.isfile("databases/pokemon/"+str(ctx.author.id)+'.json'):
uniq_id = time.time() uniq_id = time.time()
starter_id = await generate_starter(ctx.author.id) starter_id = await generate_starter(ctx.author.id)
json_data = await get_pkmn_from_id(starter_id) json_data = await get_pkmn_from_id(starter_id)
is_shiny = await shiny_roll() is_shiny = await shiny_roll()
nature = random.randint(0,19) nature = random.randint(0,19)
nature_data = await self.get_json('https://pokeapi.co/api/v2/nature/') nature_data = await self.get_json('https://pokeapi.co/api/v2/nature/')
nature = nature_data['results'][nature]['name'] nature = nature_data['results'][nature]['name']
json_data['shiny'] = is_shiny json_data['shiny'] = is_shiny
json_data['nickname'] = None json_data['nickname'] = None
json_data['unique_id'] = uniq_id json_data['unique_id'] = uniq_id
json_data['nature'] = nature json_data['nature'] = nature
json_data['buddy_level'] = 1 json_data['buddy_level'] = 1
json_data['buddy_xp'] = 0 json_data['buddy_xp'] = 0
json_data['last_food'] = 0 json_data['last_food'] = 0
json_data['last_hug'] = 0 json_data['last_hug'] = 0
await save_pokemon(ctx.author.id, json_data)
embed = await make_pmkn_embed(json_data)
await ctx.channel.send(embed=embed)
return
else:
await ctx.channel.send("You already have a pokemon!")
return
elif args[0] == 'nick' or args[0] == 'nickname':
nickname = args[1]
json_data = await load_pokemon(ctx.author.id)
json_data['nickname'] = nickname
await save_pokemon(ctx.author.id, json_data) await save_pokemon(ctx.author.id, json_data)
embed = await make_pmkn_embed(json_data) message = "You gave " + nickname + ' a new name!'
await ctx.channel.send(embed=embed) await ctx.channel.send(message)
return
else:
await ctx.channel.send("You already have a pokemon!")
return return
elif args[0] == 'nick' or args[0] == 'nickname': elif args[0] == 'feed':
nickname = args[1] json_data = await load_pokemon(ctx.author.id)
json_data = await load_pokemon(ctx.author.id) json_data, fed = await give_buddy_food(json_data)
json_data['nickname'] = nickname if fed:
await save_pokemon(ctx.author.id, json_data) await save_pokemon(ctx.author.id, json_data)
message = "You gave " + nickname + ' a new name!' if json_data['nickname']:
await ctx.channel.send(message) message = "You " + args[0] + ' ' + json_data['nickname']
return else:
message = "You " + args[0] + ' ' + json_data['name']
elif args[0] == 'feed': await ctx.channel.send(message)
json_data = await load_pokemon(ctx.author.id) return
json_data, fed = await give_buddy_food(json_data)
if fed:
await save_pokemon(ctx.author.id, json_data)
if json_data['nickname']:
message = "You " + args[0] + ' ' + json_data['nickname']
else: else:
message = "You " + args[0] + ' ' + json_data['name'] if json_data['nickname']:
await ctx.channel.send(message) message = "Your " + json_data['nickname'] + " isn't hungry!"
return else:
message = "Your " + json_data['name'] + " isn't hungry!"
await ctx.channel.send(message)
return
elif args[0] == 'hug':
json_data = await load_pokemon(ctx.author.id)
json_data, hugged = await give_buddy_affection(json_data)
if hugged:
await save_pokemon(ctx.author.id, json_data)
if json_data['nickname']:
message = "You " + args[0] + ' ' + json_data['nickname']
else:
message = "You " + args[0] + ' ' + json_data['name']
await ctx.channel.send(message)
return
else:
if json_data['nickname']:
message = "You hugged " + json_data['nickname'] + " but " + json_data['nickname'] + " has been hugged recently."
else:
message = "You hugged " + json_data['name'] + " but " + json_data['name'] + " has been hugged recently."
await ctx.channel.send(message)
return
except:
#Default !pokemon behavior (Load and show pokemon embed)
discord_id = ctx.author.id
buddy_json = await load_pokemon(discord_id)
if not buddy_json:
await ctx.channel.send("You don't have a buddy yet. Type ```!pokemon start``` to start your Pokemon journey!")
else: else:
if json_data['nickname']: embed = await make_pmkn_embed(buddy_json)
message = "Your " + json_data['nickname'] + " isn't hungry!" message = await ctx.channel.send(embed=embed)
else:
message = "Your " + json_data['name'] + " isn't hungry!"
await ctx.channel.send(message)
return return
elif args[0] == 'hug': async def pkmn_msg(self, discord_id):
json_data = await load_pokemon(ctx.author.id) path = "databases/pokemon/"+str(discord_id)+'.json'
json_data, hugged = await give_buddy_affection(json_data) if os.path.isfile(path):
if hugged: with open(path, 'r') as f:
await save_pokemon(ctx.author.id, json_data) json_data = json.loads(f.readline())
if json_data['nickname']: json_data['buddy_xp'] += random.randint(1,5)
message = "You " + args[0] + ' ' + json_data['nickname'] json_data = json.dumps(json_data)
else: with open(path, 'w') as f:
message = "You " + args[0] + ' ' + json_data['name'] f.writelines(json_data)
await ctx.channel.send(message)
return @commands.Cog.listener()
else: async def on_message(self, message: discord.Message):
if json_data['nickname']: await self.pkmn_msg(message.author.id)
message = "You hugged " + json_data['nickname'] + " but " + json_data['nickname'] + " has been hugged recently."
else:
message = "You hugged " + json_data['name'] + " but " + json_data['name'] + " has been hugged recently."
await ctx.channel.send(message)
return
#Default !pokemon behavior (Load and show pokemon embed)
discord_id = ctx.author.id
buddy_json = await load_pokemon(discord_id)
if not buddy_json:
await ctx.channel.send("You don't have a buddy yet. Type ```!pokemon start``` to start your Pokemon journey!")
else:
embed = await make_pmkn_embed(buddy_json)
message = await ctx.channel.send(embed=embed)
return
@commands.command( @commands.command(
description="Pokedex", description="Pokedex",

View file

@ -212,17 +212,6 @@ async def on_reaction_add(reaction, user):
message = reaction.message message = reaction.message
emoji = reaction.emoji emoji = reaction.emoji
await message.add_reaction(emoji) await message.add_reaction(emoji)
async def pkmn_msg(discord_id):
path = "databases/pokemon/"+str(discord_id)+'.json'
if os.path.isfile(path):
with open(path, 'r') as f:
json_data = json.loads(f.readline())
json_data['buddy_xp'] += random.randint(1,5)
json_data = json.dumps(json_data)
with open(path, 'w') as f:
f.writelines(json_data)
@bot.event @bot.event
@ -232,9 +221,6 @@ async def on_message(ctx):
channel_vars = await get_channel_config(ctx.channel.id) channel_vars = await get_channel_config(ctx.channel.id)
chat_history_string = await log_chat_and_get_history(ctx, logfile, channel_vars) chat_history_string = await log_chat_and_get_history(ctx, logfile, channel_vars)
#add pokemon xp
await pkmn_msg(ctx.author.id)
#handle non-text channels (dms, etc) #handle non-text channels (dms, etc)
if ctx.channel.type.value != 0 and ctx.author.id != 242018983241318410: if ctx.channel.type.value != 0 and ctx.author.id != 242018983241318410:
#This used to notify the user it cannot respond in this channel, but that spammed threads #This used to notify the user it cannot respond in this channel, but that spammed threads
@ -242,7 +228,7 @@ async def on_message(ctx):
await react_to_msg(ctx, channel_vars["react_to_msgs"]) #emoji reactions await react_to_msg(ctx, channel_vars["react_to_msgs"]) #emoji reactions
if channel_vars["commands_enabled"] or (ctx.author.id == 242018983241318410 and ctx.content[0] == "!"): if channel_vars["commands_enabled"] or (ctx.author.id == 242018983241318410 and ctx.content[0] == "?"):
await bot.process_commands(ctx) await bot.process_commands(ctx)
if not channel_vars["commands_enabled"]: if not channel_vars["commands_enabled"]:
await ctx.channel.send("This command only ran because you set it to allow to run even when commands are disabled") await ctx.channel.send("This command only ran because you set it to allow to run even when commands are disabled")