python dicord py问题显示嵌入中的括号值

mwg9r5ms  于 2023-01-08  发布在  Python
关注(0)|答案(1)|浏览(112)

尝试为我的diceroll命令做一个很好的嵌入输出,但我的问题是,嵌入保持只显示原始值,而不是我试图得到的整数值(下面的图片供参考)我已经尝试在线找到变通方法或解决方案,但找不到任何工作或特定于我的问题

@bot.command()
async def rolldice(ctx):
    messagetwo = await ctx.send("Choose a number:\n**4**, **6**, **8**, **10**, **12**, **20** ")
    user = ctx.message.author.display_name
    
    def check(m):
        return m.author == ctx.author

    try:
        messageone = await bot.wait_for("message", check = check, timeout = 30.0)
        m = messageone.content

        if m != "4" and m != "6" and m != "8" and m != "10" and m != "12" and m != "20":
            await ctx.send("Sorry, invalid choice.")
            return
        
        coming = await ctx.send("Here it comes...")
        asyncio.sleep(1)
        await coming.delete()
        await messagetwo.delete()
        await ctx.channel.purge(limit=2)
        embedVar = discord.Embed(title="{user}'s Dice",color=0x00ffff)
        embedVar.add_field(name="rolled", value="D{m}", inline=True)
        embedVar.add_field(name="landed", value="{random.randint(1, int(m))}", inline=True)
        embedVar.set_footer(text='Booty Police | Dungeon Dice',icon_url="http://canvaswrite.com/PI/pybot/attachments/server-icon-full.png")
        await ctx.send(embed=embedVar)
        await ctx.send(f"{user} rolled a **D{m}** and got a **{random.randint(1, int(m))}**")
    except asyncio.TimeoutError:
        await messageone.delete()
        await ctx.send("Procces has been canceled because you didn't respond in **30** seconds.")

ljo96ir5

ljo96ir51#

我认为你正在尝试使用f-string,但是忘记了在字符串之前放置f。没有f,它将不会格式化字符串。如果你想阅读更多关于f-strings的内容,这里有一个指南

x = 3 
print(f"This is the value {x}")

>>> This is the value 3

所以为了让你的代码正常工作,你只需要在字符串前面加上一个f。固定的代码看起来像这样:

@bot.command()
async def rolldice(ctx):
    messagetwo = await ctx.send("Choose a number:\n**4**, **6**, **8**, **10**, **12**, **20** ")
    user = ctx.message.author.display_name
    
    def check(m):
        return m.author == ctx.author

    try:
        messageone = await bot.wait_for("message", check = check, timeout = 30.0)
        m = messageone.content

        if m != "4" and m != "6" and m != "8" and m != "10" and m != "12" and m != "20":
            await ctx.send("Sorry, invalid choice.")
            return
        
        coming = await ctx.send("Here it comes...")
        asyncio.sleep(1)
        await coming.delete()
        await messagetwo.delete()
        await ctx.channel.purge(limit=2)
        embedVar = discord.Embed(title=f"{user}'s Dice",color=0x00ffff)
        embedVar.add_field(name="rolled", value=f"D{m}", inline=True)
        embedVar.add_field(name="landed", value=f"{random.randint(1, int(m))}", inline=True)
        embedVar.set_footer(text='Booty Police | Dungeon Dice',icon_url="http://canvaswrite.com/PI/pybot/attachments/server-icon-full.png")
        await ctx.send(embed=embedVar)
        await ctx.send(f"{user} rolled a **D{m}** and got a **{random.randint(1, int(m))}**")
    except asyncio.TimeoutError:
        await messageone.delete()
        await ctx.send("Procces has been canceled because you didn't respond in **30** seconds.")

相关问题