【问题标题】:Embeds for Discord Python BotsDiscord Python 机器人的嵌入
【发布时间】:2021-07-24 20:59:58
【问题描述】:

所以我制作了这段代码,用户可以在其中选择自己的嵌入颜色,但如果他们嵌入错误的内容,它会搞砸整个事情,他们需要重做,如果他们不这样做,有没有办法做到这一点? t 响应或输入不正确的十六进制代码,它将自动转到十六进制颜色 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)

这是我的代码

【问题讨论】:

    标签: python discord bots


    【解决方案1】:

    您可以使用 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 并像这样发送消息。

    【讨论】:

    • 由于某种原因它仍然不适合我
    • @Kepzix 更具体一点,你有没有得到任何错误,是否将它设置为另一种颜色?
    • 没有错误,只是没有嵌入,如果你搞砸了
    • @Kepzix 我编辑了我的代码,它现在应该可以工作了。
    • 由于某种原因它仍然无法正常工作,mid-evil-tk.glitch.me/invite,请邀请机器人并执行 r!say。
    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2017-12-05
    • 2021-04-10
    • 2021-07-17
    • 2019-01-12
    相关资源
    最近更新 更多