所以基本上我是想把我的不和谐机器人的命令放进齿轮里。它把齿轮装得很好。但是,当我运行这个命令,而bot试图向我的日志通道发送一个嵌入时,它就不能工作了。
以下是代码:
from discord.ext import commands
import datetime
from ruamel.yaml import YAML
yaml = YAML()
with open(r"C:\Users\Jea\Desktop\Yuna-Discord-Bot\config.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:\Users\Jea\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\Jea\Desktop\Yuna-Discord-Bot\cogs\testing.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:\Users\Jea\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Jea\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Jea\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.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)的台词。因为当我删除它的时候,命令运行得很好。但是,我不知道是什么原因造成的。任何帮助都将不胜感激!
发布于 2022-07-09 18:35:00
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等,以便更清楚地了解这里发生的事情。
https://stackoverflow.com/questions/72923727
复制相似问题