So I'm using this code to make my announcements
@bot.command(pass_context = True, aliases=[ 'a', 'announce' ])
async def announce_command(ctx):
await ctx.send(f"**{ctx.author}**, send a title in 60 seconds, to begin embed builder!",delete_after=45)
def check(m: discord.Message):
return m.author.id == ctx.author.id and m.channel.id == ctx.channel.id
try:
titlemsg = await bot.wait_for(event = 'message', check = check, timeout= 10 )
await ctx.send(f"**{ctx.author}**, State a description.", delete_after=45)
msg= await bot.wait_for(event = 'message', check = check, timeout= 40)
await ctx.send(f"**{ctx.author}**, State a Url, if you want a thumbnail added.",delete_after=45)
UrlThmsg= await bot.wait_for(event= 'message', check = check, timeout= 10)
except asyncio.TimeoutError:
await ctx.send(f"**{ctx.author}**, you didn't send any message that meets the check in this channel for 60 seconds..", delete_after=45)
return
else:
embed = d.Embed(title = f"{titlemsg.content}", description = f"{msg.content}", color = 0xFFFFFF)
embed.set_thumbnail(url = f"{UrlThmsg.content}")
embed.set_footer(text = f'Annouced By {ctx.author}')
await ctx.send('@everyone', embed = embed)
I wanted to make UrlTHmsg = a option event and if no one puts anything in it will say something like a thumbnail wont be added and move onto the next event without stopping the embed. If anyone can help me that would be great. This is discord.py btw.
You can use finally:
to execute code regardless of the exception. Also it would be better to declare a dictionary and then check the existence of a specific key to avoid an exception when you try to use an undeclared variable (or you can just declare your variables before try:
):
@bot.command(pass_context = True, aliases=[ 'a', 'announce' ])
async def announce_command(ctx):
await ctx.send(f"**{ctx.author}**, send a title in 60 seconds, to begin embed builder!",delete_after=45)
def check(m: discord.Message):
return m.author.id == ctx.author.id and m.channel.id == ctx.channel.id
announce_info = {}
try:
announce_info['title'] = await bot.wait_for(event = 'message', check = check, timeout= 10 )
await ctx.send(f"**{ctx.author}**, State a description.", delete_after=45)
announce_info['description'] = await bot.wait_for(event = 'message', check = check, timeout= 40)
await ctx.send(f"**{ctx.author}**, State a Url, if you want a thumbnail added.",delete_after=45)
announce_info['thumbnail'] = await bot.wait_for(event= 'message', check = check, timeout= 10)
except asyncio.TimeoutError:
await ctx.send(f"**{ctx.author}**, you didn't send any message that meets the check in this channel for 60 seconds..", delete_after=45)
return
finally:
if announce_info.get('title') and announce_info.get('description'):
embed = d.Embed(title = f"{announce_info['title'].content}", description = f"{announce_info['description'].content}", color = 0xFFFFFF)
embed.set_thumbnail(url = f"{announce_info['thumbnail'].content}") if announce_info.get('thumbnail') else None
embed.set_footer(text = f'Annouced By {ctx.author}')
await ctx.send('@everyone', embed = embed)