我的机器人有点问题。我正在尝试让bot工作,这样,如果它接收到带有命令“上诉”的DM,bot就会将上诉发送到服务器的上诉频道。到目前为止,我得到的是:
@bot.command()
async def appeal(ctx, guild_id, *reasons):
'''Command for a user to DM the bot with an appeal, and have that appeal sent to the corresponding server.'''
# Checking if the command is invoked in a DM
if isinstance(ctx.channel, discord.channel.DMChannel):
# Finding the guild through the provided guild id
bot.guild = bot.get_guild(guild_id)
# Finding and assigning a text channel called "appeals", and sending the appeal to that channel
appeals = discord.utils.get(bot.get_guild(guild_id).text_channels, name="appeals")
await bot.guild.appeals.send(f"**{ctx.author.id} has appealed to this server:** {reasons}")
这是我收到的回溯,但是:
Ignoring exception in command appeal:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/Users/secret/Desktop/OneDrive/PyBot/Millenium_Build.py", line 408, in appeal
appeals = discord.utils.get(bot.get_guild(guild_id).text_channels, name="appeals")
AttributeError: 'NoneType' object has no attribute 'text_channels'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/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: 'NoneType' object has no attribute 'text_channels'
任何帮助都将不胜感激
默认情况下,通过命令传递的参数都是字符串
bot.get_guild()
仅适用于整数您可以将
int(guild_id)
传递给函数,也可以编辑命令参数将其自动转换为如下所示的整数async def appeal(ctx, guild_id: int, *reasons):
我很确定您没有启用任何意图,DM的消息需要
intents.dm_messages
,get_guild
需要intents.guilds
。您还可以使用commands.dm_only
装饰器而不是isinstance
。此外,ID必须始终为整数以下是如何启用意图:
这里还有正在使用的
dm_only
装饰器您还可以在
on_ready
事件中获取guild
和appeal
通道,无需每次调用命令时都获取它相关问题 更多 >
编程相关推荐