【问题标题】:Trying to create Embed Command discord.py尝试创建嵌入命令 discord.py
【发布时间】:2021-08-09 18:46:17
【问题描述】:

我正在为我的机器人创建 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”对象没有“发送”属性

非常感谢您的帮助

【问题讨论】:

    标签: python discord discord.py embed


    【解决方案1】:
    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,这应该可以工作。

    【讨论】:

    • 谢谢你,非常感谢,也会调查你所说的
    • @IDK 请注意,如果找不到频道,get_channel() 仍然可以返回 None。您可以考虑使用 TextChannelConverter.convert,除了 ID 之外,它还适用于频道名称和提及。
    【解决方案2】:

    您没有传递 id(消息内容),而是 guild.get_channel()

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

    【讨论】:

      猜你喜欢
      • 2021-02-28
      • 2021-06-04
      • 1970-01-01
      • 2020-08-12
      • 2020-09-08
      • 2021-06-20
      • 1970-01-01
      • 2021-10-20
      • 2022-01-05
      相关资源
      最近更新 更多