Embeds for Discord Python Bots



所以我制作了这个代码,用户可以选择自己的嵌入颜色,但如果他们嵌入错误,就会把整个事情搞砸,他们需要重做,不管怎样,如果他们没有响应或输入错误的十六进制代码,它会自动转到十六进制颜色FF00EC。

@client.command(aliases=['say'])
@commands.has_permissions(manage_messages=True)
async def embed(ctx):
questions = ["Which should be the tile of the embed?",
"What should be the description?",
"What is the color of the embed? This should be a hex color. Note, Must put a `Correct` Hex color or you will need to redo this also leave out the #."]
answers = []
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
for i in questions:
await ctx.send(i)
try:
msg = await client.wait_for('message', timeout=30, check=check)
except asyncio.TimeoutError:
await ctx.send('You did not answer in time. Please do it under 30 seconds next time.')
return
else:
answers.append(msg.content)
embedcolor = answers[2]
embed = discord.Embed(description=answers[1], title=answers[0], colour=int(embedcolor, 16))
await ctx.send(embed=embed)

这是我的代码

您可以使用try-except语句轻松完成此操作。

import re
@client.command(aliases=['say'])
@commands.has_permissions(manage_messages=True)
async def embed(ctx):
questions = ["Which should be the tile of the embed?",
"What should be the description?",
"What is the color of the embed? This should be a hex color. Note, Must put a `Correct` Hex color or you will need to redo this also leave out the #."]
answers = []
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
for i in questions:
await ctx.send(i)
try:
msg = await client.wait_for('message', timeout=30, check=check)
except asyncio.TimeoutError:
await ctx.send('You did not answer in time. Please do it under 30 seconds next time.')
return
else:
answers.append(msg.content)
embedcolor = answers[2]
embed = discord.Embed(description=answers[1], title=answers[0], colour=int(embedcolor, 16))
if re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', embedcolor):
await ctx.send(embed=embed)
else:
embed = discord.Embed(description=answers[1], title=answers[0], colour=int("FF00EC", 16))
await ctx.send(embed=embed)

基本上,如果颜色无效,我们将颜色设置为FF00EC并发送这样的消息。

最新更新