尝试创建嵌入命令 discord.py

时间:2021-05-20 13:46:33

标签: python discord discord.py embed

我正在为我的机器人创建嵌入 cmd,我希望我的机器人询问用户想要发送嵌入的频道,但我在这样做时遇到了错误。

代码:

@bot.command()
async def buildembed(ctx):
    def check(message):
        return message.author == ctx.author and message.channel == ctx.channel

    await ctx.send('Waiting for a title')
    title = await bot.wait_for('message', check=check)
  
    await ctx.send('Waiting for a description')
    desc = await bot.wait_for('message', check=check)
    
    await ctx.send('Channel ID')
    guild = bot.get_guild(12345678)
    channel = guild.get_channel(await bot.wait_for('message', check=check))
    
    embed = discord.Embed(title=title.content, description=desc.content, color=discord.Color.blue())
    await channel.send(embed=embed)
<块引用>
raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an

异常:AttributeError: 'NoneType' 对象没有属性 'send'

非常感谢您的帮助

2 个答案:

答案 0 :(得分:2)

channel = guild.get_channel(await bot.wait_for('message', check=check))
await channel.send(embed=embed)

channelNone,因此错误 exception: AttributeError: 'NoneType' object has no attribute 'send'

https://discordpy.readthedocs.io/en/stable/api.html?highlight=member_count#discord.Guild.get_channel

get_channel() 需要一个整数。您正在向它传递一个 message 对象。您需要获取消息的内容,然后将其转换为 int。类似的东西

int((await bot.wait_for('message', check=check)).content)

真的很丑的代码。您应该重构它以使其看起来更漂亮。但假设提供的频道 ID 是有效的频道 ID,这应该可行。

答案 1 :(得分:1)

您传递的不是 id(消息内容)而是 guild.get_channel()

中的消息对象
   channel_id = await bot.wait_for('message', check=check)
   channel = guild.get_channel(int(channel_id.content))