首页 > 解决方案 > 尝试创建嵌入命令 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”对象没有属性“发送”

您的帮助将不胜感激

标签: pythondiscorddiscord.pyembed

解决方案


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()需要一个 int。您正在将一个message对象传递给它。您需要获取消息的内容,然后将其转换为 int。就像是

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

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


推荐阅读