python - 尝试创建嵌入命令 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”对象没有属性“发送”
您的帮助将不胜感激
解决方案
channel = guild.get_channel(await bot.wait_for('message', check=check))
await channel.send(embed=embed)
channel
是None
,因此错误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,这应该可以工作。
推荐阅读
- c++ - C++,不能处理大于 10^5 的数字的问题
- opencv - 如何创建一个只识别我的 Haar Cascade?
- javascript - Playwright (Puppeteer) 上下文在启动时是一个空数组?
- python - 获取模块'google.protobuf.descriptor_pool'在我的python脚本中没有属性'Default'
- javascript - 创建元素列表
- winapi - 如何使用 Direct2D 在 DirectWrite 中围绕图像或抽象形状环绕文本?
- python - PyQt5 QCompleter如何在选择自动完成文本后清除QLineEdit
- r - 了解 seq 函数的输出
- r - 突出显示 ggplot 中的特定日期
- c - arm-none-eabi-gcc 生成的代码是否比 Keil uVision 慢