discord.py中随机发送6个骰子Embed失败问题求助
解决Discord机器人随机发送骰子Embed的报错问题
问题说明
尝试实现Discord机器人随机发送6个带骰子图片的discord.Embed,但代码运行时报错:字符串没有to_dict属性。原代码如下:
embed1 = discord.Embed() embed1.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/28e8fc51f2e2945fd7cd6b3a158ab7115993a1be3328e656713f01107282c4cc.png') embed2 = discord.Embed() embed2.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/a2daafe7bb4b6fd9aef54aa313d7e19b5beae28a2dbcba4d3dacc9be8b2ffcee.png') embed3 = discord.Embed() embed3.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/04fd25da832cb85af325ab32e714642bef78c97a0858062bb0b15a168f1e185f.jpeg') embed4 = discord.Embed() embed4.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/b26214af7789cf5bd239f862485e10910fe4cb167437338c12abdbb374f4f683.png') embed5 = discord.Embed() embed5.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/64472c33b4a99025e133e9d063128e08b468abb58ab8b8d8fa3d8b57d7da52bc.png') embed6 = discord.Embed() embed6.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/183014dc016ae2658314b25d8f2ad5a442aea04999b2457c66381a9e651c4c82.png') embeds = [f'{embed1}', f'{embed2}', f'{embed3}', f'{embed4}', f'{embed5}', f'{embed6}'] @client.command() async def dados(ctx): await ctx.send(embed={random.choice(embeds)})
错误原因
- Embed对象被转为字符串:使用
f'{embed1}'将discord.Embed实例强制转为字符串,导致embeds列表中存储的是字符串而非Embed对象。但ctx.send()的embed参数要求传入discord.Embed实例,字符串没有Discord序列化所需的to_dict()方法,因此触发报错。 - 语法错误的大括号:
await ctx.send(embed={random.choice(embeds)})中的大括号属于错误语法,会把选中的内容包裹成集合,而非直接传递Embed对象。
修复后的代码
方案一:直接存储Embed对象
import random import discord from discord.ext import commands client = commands.Bot(command_prefix='!') # 根据你的机器人前缀修改 # 创建Embed对象列表,直接存储实例而非字符串 embed1 = discord.Embed() embed1.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/28e8fc51f2e2945fd7cd6b3a158ab7115993a1be3328e656713f01107282c4cc.png') embed2 = discord.Embed() embed2.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/a2daafe7bb4b6fd9aef54aa313d7e19b5beae28a2dbcba4d3dacc9be8b2ffcee.png') embed3 = discord.Embed() embed3.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/04fd25da832cb85af325ab32e714642bef78c97a0858062bb0b15a168f1e185f.jpeg') embed4 = discord.Embed() embed4.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/b26214af7789cf5bd239f862485e10910fe4cb167437338c12abdbb374f4f683.png') embed5 = discord.Embed() embed5.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/64472c33b4a99025e133e9d063128e08b468abb58ab8b8d8fa3d8b57d7da52bc.png') embed6 = discord.Embed() embed6.set_image(url='https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/183014dc016ae2658314b25d8f2ad5a442aea04999b2457c66381a9e651c4c82.png') embeds = [embed1, embed2, embed3, embed4, embed5, embed6] @client.command() async def dados(ctx): await ctx.send(embed=random.choice(embeds))
方案二:简化代码,动态创建Embed
如果不想重复创建Embed对象,可以先存储所有骰子图片的URL,随机选中后再生成Embed:
import random import discord from discord.ext import commands client = commands.Bot(command_prefix='!') # 存储所有骰子图片URL dice_image_urls = [ 'https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/28e8fc51f2e2945fd7cd6b3a158ab7115993a1be3328e656713f01107282c4cc.png', 'https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/a2daafe7bb4b6fd9aef54aa313d7e19b5beae28a2dbcba4d3dacc9be8b2ffcee.png', 'https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/04fd25da832cb85af325ab32e714642bef78c97a0858062bb0b15a168f1e185f.jpeg', 'https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/b26214af7789cf5bd239f862485e10910fe4cb167437338c12abdbb374f4f683.png', 'https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/64472c33b4a99025e133e9d063128e08b468abb58ab8b8d8fa3d8b57d7da52bc.png', 'https://cdn-longterm.mee6.xyz/plugins/commands/images/551250166196207634/183014dc016ae2658314b25d8f2ad5a442aea04999b2457c66381a9e651c4c82.png' ] @client.command() async def dados(ctx): selected_url = random.choice(dice_image_urls) embed = discord.Embed() embed.set_image(url=selected_url) await ctx.send(embed=embed)
内容的提问来源于stack exchange,提问作者Aby
相关产品推荐
相关产品推荐

