我的 Discord 机器人不会从齿轮内发送嵌入。(属性错误:"int"对象没有属性"send")



所以基本上我试图把我的不和机器人的命令放入齿轮。它把齿轮装得很好。但是当我运行命令和机器人试图发送嵌入到我的日志通道它不工作。

代码如下:

from discord.ext import commands 
import datetime
from ruamel.yaml import YAML
yaml = YAML()
with open(r"C:UsersJeaDesktopYuna-Discord-Botconfig.yml", "r", encoding="utf-8") as file:
config = yaml.load(file)

bot = commands.Bot(command_prefix=config['Prefix'])
bot.debugchannel = config['Debug Channel ID']
bot.embed_color = discord.Color.from_rgb(
config['Embed Settings']['Color']['r'],
config['Embed Settings']['Color']['g'],
config['Embed Settings']['Color']['b'])
bot.footer = config['Embed Settings']['Footer']['Text']
bot.footerimg = config['Embed Settings']['Footer']['Icon URL']
class Testing(commands.Cog):
def __init__(self, bot):
self.bot = bot 
@bot.command(name="ping",
aliases=["p"],
help="Check if the bot is online with a simple command")
async def ping(self, ctx):
embed = discord.Embed(title=f"Pinged {bot.user}", color=bot.embed_color, timestamp=datetime.datetime.now(datetime.timezone.utc))
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
embed.set_footer(text=bot.footer, icon_url=bot.footerimg)
await bot.debugchannel.send(embed=embed)
print("Sent and embed")
await ctx.message.add_reaction('✅')
print("Added a reaction to a message")
await ctx.send("Pong!")
print("Sent a message")

def setup(bot):
bot.add_cog(Testing(bot))
print("Loaded testing cog")

这是它给出的错误:

Traceback (most recent call last):
File "C:UsersJeaAppDataLocalProgramsPythonPython310libsite-packagesdiscordextcommandscore.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:UsersJeaDesktopYuna-Discord-Botcogstesting.py", line 34, in ping
await bot.debugchannel.send(embed=embed)
AttributeError: 'int' object has no attribute 'send'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersJeaAppDataLocalProgramsPythonPython310libsite-packagesdiscordextcommandsbot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:UsersJeaAppDataLocalProgramsPythonPython310libsite-packagesdiscordextcommandscore.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:UsersJeaAppDataLocalProgramsPythonPython310libsite-packagesdiscordextcommandscore.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'int' object has no attribute 'send'

,我知道它是行await bot.debugchannel.send(embed=embed)。因为当我删除它时,命令工作得很好。然而,我不知道是什么引起的。任何帮助将不胜感激!

bot.debugchannel只是存储通道的id,而不是discord.TextChannel对象,因此它不能用于发送消息。首先,您需要从id获取通道本身,这可以使用ctx.guild.get_channel(<id>)

完成因此用以下两行替换ping方法的第4行:

debugchannel = ctx.guild.get_channel(bot.debugchannel)
await debugchannel.send(embed=embed)

您可能还想考虑将bot.debugchannel的变量名更改为bot.debugchannel_id或类似的名称,以使这里发生的事情更清楚。