changed pokemon game to mostly be a cog and commands

This commit is contained in:
phixxy 2024-02-02 17:41:44 -08:00
parent 022d9afbee
commit 9e1624eb6b

View file

@ -12,6 +12,8 @@ async def get_json(self, url):
json_data = await resp.json() json_data = await resp.json()
return json_data return json_data
#@commands.cooldown(1, 60, commands.BucketType.user) Cooldowns....
class Pokemon: class Pokemon:
async def __init__(self, id: int, generate: bool = True) -> None: async def __init__(self, id: int, generate: bool = True) -> None:
if generate: # Should I even do it this way? if generate: # Should I even do it this way?
@ -39,15 +41,7 @@ class PokemonGame(commands.Cog):
except: except:
self.bot.logger.exception("PokemonGame failed to make directories") self.bot.logger.exception("PokemonGame failed to make directories")
@commands.command( async def starter_picker(self, id): #id = pokedex number
description="Pokemon",
help="Pokemon game",
brief="Pokemon Game",
aliases=['pkmn'],
hidden=True
)
async def pokemon(self, ctx, *args):
async def starter_picker(id): #id = pokedex number
url = "https://pokeapi.co/api/v2/pokemon-species/" + str(id) url = "https://pokeapi.co/api/v2/pokemon-species/" + str(id)
json_data = await get_json(url) json_data = await get_json(url)
if (json_data["evolves_from_species"] == None) and (not json_data['is_mythical']) and (not json_data['is_legendary']): if (json_data["evolves_from_species"] == None) and (not json_data['is_mythical']) and (not json_data['is_legendary']):
@ -55,18 +49,18 @@ class PokemonGame(commands.Cog):
else: else:
return False return False
async def shiny_roll(): async def shiny_roll(self):
roll = random.randint(0,2047) roll = random.randint(0,2047)
return not roll return not roll
async def save_pokemon(discord_id, pokemon_dict): async def save_pokemon(self, discord_id, pokemon_dict):
path = self.data_dir+str(discord_id)+".json" path = self.data_dir+str(discord_id)+".json"
pokemon_dict = json.dumps(pokemon_dict) pokemon_dict = json.dumps(pokemon_dict)
with open(path, 'w') as f: with open(path, 'w') as f:
f.writelines(pokemon_dict) f.writelines(pokemon_dict)
return True return True
async def load_pokemon(discord_id): async def load_pokemon(self, discord_id):
if os.path.isfile(self.data_dir+str(discord_id)+".json"): if os.path.isfile(self.data_dir+str(discord_id)+".json"):
with open(self.data_dir+str(discord_id)+".json", 'r') as f: with open(self.data_dir+str(discord_id)+".json", 'r') as f:
json_data = json.loads(f.readline()) json_data = json.loads(f.readline())
@ -74,23 +68,23 @@ class PokemonGame(commands.Cog):
else: else:
return False return False
async def generate_starter(discord_id): async def generate_starter(self, discord_id):
random.seed(discord_id) random.seed(discord_id)
json_data = await get_json('https://pokeapi.co/api/v2/pokemon-species/') json_data = await get_json('https://pokeapi.co/api/v2/pokemon-species/')
pokemon_count = json_data['count'] pokemon_count = json_data['count']
base_pokemon = False base_pokemon = False
while not base_pokemon: while not base_pokemon:
starter_id = random.randint(1,pokemon_count) starter_id = random.randint(1,pokemon_count)
base_pokemon = await starter_picker(starter_id) base_pokemon = await self.starter_picker(starter_id)
random.seed() random.seed()
return starter_id return starter_id
async def get_pkmn_from_id(id): async def get_pkmn_from_id(self, id):
url = 'https://pokeapi.co/api/v2/pokemon/' + str(id) url = 'https://pokeapi.co/api/v2/pokemon/' + str(id)
json_data = await get_json(url) json_data = await get_json(url)
return json_data return json_data
async def give_buddy_food(pkmn_data): async def give_buddy_food(self, pkmn_data):
try: try:
last_food = pkmn_data['last_food'] last_food = pkmn_data['last_food']
except: except:
@ -98,13 +92,13 @@ class PokemonGame(commands.Cog):
this_food = time.time() this_food = time.time()
if (this_food - last_food) >= 1800: if (this_food - last_food) >= 1800:
pkmn_data['last_food'] = this_food pkmn_data['last_food'] = this_food
level = await calc_pkmn_buddy_level(pkmn_data) level = await self.calc_pkmn_buddy_level(pkmn_data)
pkmn_data['buddy_xp'] += (4*level) pkmn_data['buddy_xp'] += (4*level)
return pkmn_data, True return pkmn_data, True
else: else:
return pkmn_data, False return pkmn_data, False
async def give_buddy_affection(pkmn_data): async def give_buddy_affection(self, pkmn_data):
try: try:
last_hug = pkmn_data['last_hug'] last_hug = pkmn_data['last_hug']
except: except:
@ -112,17 +106,17 @@ class PokemonGame(commands.Cog):
this_hug = time.time() this_hug = time.time()
if (this_hug - last_hug) >= 600: if (this_hug - last_hug) >= 600:
pkmn_data['last_hug'] = this_hug pkmn_data['last_hug'] = this_hug
level = await calc_pkmn_buddy_level(pkmn_data) level = await self.calc_pkmn_buddy_level(pkmn_data)
pkmn_data['buddy_xp'] += (3*level) pkmn_data['buddy_xp'] += (3*level)
return pkmn_data, True return pkmn_data, True
else: else:
return pkmn_data, False return pkmn_data, False
async def calc_pkmn_buddy_level(pkmn_json): #this uses the 'fast' xp rate async def calc_pkmn_buddy_level(self, pkmn_json): #this uses the 'fast' xp rate
buddy_xp = pkmn_json['buddy_xp'] buddy_xp = pkmn_json['buddy_xp']
return min(math.floor(((5*buddy_xp)/4)**(1/3)),100) return min(math.floor(((5*buddy_xp)/4)**(1/3)),100)
async def make_pmkn_embed(pkmn_dict): async def make_pmkn_embed(self, pkmn_dict):
if pkmn_dict['nickname']: if pkmn_dict['nickname']:
title = pkmn_dict['nickname'] + ' (' + pkmn_dict['name'].capitalize() + ')' title = pkmn_dict['nickname'] + ' (' + pkmn_dict['name'].capitalize() + ')'
else: else:
@ -133,7 +127,7 @@ class PokemonGame(commands.Cog):
else: else:
embed.set_image(url=pkmn_dict['sprites']['front_default']) embed.set_image(url=pkmn_dict['sprites']['front_default'])
nature = pkmn_dict['nature'] nature = pkmn_dict['nature']
buddy_level = await calc_pkmn_buddy_level(pkmn_dict) buddy_level = await self.calc_pkmn_buddy_level(pkmn_dict)
buddy_xp = pkmn_dict['buddy_xp'] buddy_xp = pkmn_dict['buddy_xp']
types = [] types = []
for key in pkmn_dict['types']: for key in pkmn_dict['types']:
@ -144,13 +138,14 @@ 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': @commands.command()
async def pkmn_start(self, ctx):
if not os.path.isfile(self.data_dir+str(ctx.author.id)+'.json'): if not os.path.isfile(self.data_dir+str(ctx.author.id)+'.json'):
uniq_id = time.time() uniq_id = time.time()
starter_id = await generate_starter(ctx.author.id) starter_id = await self.generate_starter(ctx.author.id)
json_data = await get_pkmn_from_id(starter_id) json_data = await self.get_pkmn_from_id(starter_id)
is_shiny = await shiny_roll() is_shiny = await self.shiny_roll()
nature = random.randint(0,19) nature = random.randint(0,19)
nature_data = await get_json('https://pokeapi.co/api/v2/nature/') nature_data = await get_json('https://pokeapi.co/api/v2/nature/')
nature = nature_data['results'][nature]['name'] nature = nature_data['results'][nature]['name']
@ -162,71 +157,67 @@ class PokemonGame(commands.Cog):
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) await self.save_pokemon(ctx.author.id, json_data)
embed = await make_pmkn_embed(json_data) embed = await self.make_pmkn_embed(json_data)
await ctx.channel.send(embed=embed) await ctx.channel.send(embed=embed)
return return
else: else:
await ctx.channel.send("You already have a pokemon!") await ctx.channel.send("You already have a pokemon!")
return return
elif args[0] == 'nick' or args[0] == 'nickname':
nickname = args[1] @commands.command()
json_data = await load_pokemon(ctx.author.id) async def pkmn_nick(self, ctx, nickname):
json_data = await self.load_pokemon(ctx.author.id)
json_data['nickname'] = nickname json_data['nickname'] = nickname
await save_pokemon(ctx.author.id, json_data) await self.save_pokemon(ctx.author.id, json_data)
message = "You gave " + nickname + ' a new name!' message = "You gave " + nickname + " a new name!"
await ctx.channel.send(message) await ctx.channel.send(message)
return
elif args[0] == 'feed':
json_data = await load_pokemon(ctx.author.id)
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:
message = "You " + args[0] + ' ' + json_data['name']
await ctx.channel.send(message)
return
else:
if json_data['nickname']:
message = "Your " + json_data['nickname'] + " isn't hungry!"
else:
message = "Your " + json_data['name'] + " isn't hungry!"
await ctx.channel.send(message)
return
elif args[0] == 'hug': @commands.command()
json_data = await load_pokemon(ctx.author.id) @commands.cooldown(1, 3600, commands.BucketType.user)
json_data, hugged = await give_buddy_affection(json_data) async def pkmn_feed(self, ctx):
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 discord_id = ctx.author.id
buddy_json = await load_pokemon(discord_id) json_data = await self.load_pokemon(discord_id)
json_data, fed = await self.give_buddy_food(json_data)
if fed:
await self.save_pokemon(ctx.author.id, json_data)
if json_data['nickname']:
message = "You Feed " + json_data['nickname']
else:
message = "You Feed " + json_data['name']
await ctx.channel.send(message)
return
@commands.command()
@commands.cooldown(1, 1800, commands.BucketType.user)
async def pkmn_hug(self, ctx):
discord_id = ctx.author.id
json_data = await self.load_pokemon(discord_id)
json_data, hugged = await self.give_buddy_affection(json_data)
if hugged:
await self.save_pokemon(discord_id, json_data)
if json_data['nickname']:
message = "You " + "hugged" + ' ' + json_data['nickname']
else:
message = "You " + "hugged" + ' ' + json_data['name']
await ctx.channel.send(message)
@commands.command()
async def pokemon(self, ctx):
discord_id = ctx.author.id
buddy_json = await self.load_pokemon(discord_id)
if not buddy_json: if not buddy_json:
await ctx.channel.send("You don't have a buddy yet. Type ```!pokemon start``` to start your Pokemon journey!") await ctx.channel.send("You don't have a buddy yet. Type ```!pokemon start``` to start your Pokemon journey!")
else: else:
embed = await make_pmkn_embed(buddy_json) embed = await self.make_pmkn_embed(buddy_json)
message = await ctx.channel.send(embed=embed) message = await ctx.channel.send(embed=embed)
return return
async def pkmn_msg(self, discord_id): async def pkmn_msg(self, discord_id):
path = self.data_dir+str(discord_id)+'.json' path = self.data_dir+str(discord_id)+'.json'
if os.path.isfile(path): if os.path.isfile(path):