While I was coding this discord.py command, I struck with an idea of 'easter egg' kinda part to this command.
The idea was if someone typed in '+fact birb'('+fact (a string)' triggers this command), it will send 'easter egg' embed first (which I have already coded in the elif part) and then send another embed that would have been sent if anyone typed '+fact bird' (which triggers if part).
I wanted to ask if just copy&pasting the 'if part' to 'elif part' is the only answer or is there any other way to fire 'if part' from 'elif part'.
I can accept if your answer is retriggering the entire command with input of '+fact bird' in elif part.
Here is the code:
@command(name='fact')
async def animal_fact(self, ctx, animal: str):
if (animal := animal.lower()) in ('dog', 'cat', 'panda', 'fox', 'bird', 'koala'):
fact_url = f'https://some-random-api.ml/facts/{animal}'
image_url = f'https://some-random-api.ml/img/{"birb" if animal == "bird" else animal}'
async with request('GET', image_url, headers={}) as response:
if response.status == 200:
data = await response.json()
image_link = data['link']
else:
image_link = None
async with request('GET', fact_url, headers={}) as response:
if response.status == 200:
data = await response.json()
embed = Embed(title=f'{animal.title()} fact',
description=data['fact'],
colour=ctx.author.color)
if image_link is not None:
embed.set_image(url=image_link)
await ctx.send(embed=embed)
else:
await ctx.send(f'API returned a {response.status} status.')
elif (animal := animal.lower()) == 'birb':
birb_num = randrange(3)
if birb_num == 0:
birb_url = 'https://upload3.inven.co.kr/upload/2020/04/18/bbs/i13666922526.jpg'
elif birb_num == 1:
birb_url = 'https://cdn.download.ams.birds.cornell.edu/api/v1/asset/85774871/1800'
else:
birb_url = 'https://www.4flix.co.kr/data/file/gallery/3743818203_HLK71nSR_d2bc42b6af7e42d5a78dce28231a600a7dc10fc8.jpg'
async with request('GET', birb_url, headers={}) as response:
if response.status == 200:
embed_c = Embed(title = "You've been greeted by this cute birb. continue to the bird facts.",
colour = 0xDAA520)
embed_c.set_image(url=birb_url)
await ctx.send(embed=embed_c)
else:
await ctx.send(f'API returned a {response.status} status.')
else:
await ctx.send('No facts are available for that.\nfacts availabe for: dog, cat, panda, fox, bird, koala')
Aucun commentaire:
Enregistrer un commentaire