Discord Py bot接收DM并将其转发到指定服务器

2024-10-02 22:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我的机器人有点问题。我正在尝试让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'

任何帮助都将不胜感激


Tags: thetextinpyidgetbotext
2条回答

默认情况下,通过命令传递的参数都是字符串bot.get_guild()仅适用于整数

您可以将int(guild_id)传递给函数,也可以编辑命令参数将其自动转换为如下所示的整数

async def appeal(ctx, guild_id: int, *reasons):

我很确定您没有启用任何意图,DM的消息需要intents.dm_messagesget_guild需要intents.guilds。您还可以使用commands.dm_only装饰器而不是isinstance。此外,ID必须始终为整数

以下是如何启用意图:

# Enabling default intents (everything apart from privileged ones)
intents = discord.Intents.default()

bot = commands.Bot(..., intents=intents)

这里还有正在使用的dm_only装饰器

@bot.command()
@commands.dm_only()
async def appeal(ctx, guild_id: int, *, reason):
    # ...

您还可以在on_ready事件中获取guildappeal通道,无需每次调用命令时都获取它

@bot.event
async def on_ready():
    bot.main_guild = bot.get_guild(some_guild_id) # You'd need to hardcode this
    bot.appeals = discord.utils.get(bot.main_guild.text_channels, name='appeals') # I'm not sure why it's not possible with `bot.main_guild.appeals`, but this should work

# Now you can simply bot.main_guild.appeals.send('some content')

相关问题 更多 >