【问题标题】:(discord.py) How do I get my bot to read DM's that are sent to it and either print them or send them to a specific channel(discord.py) 如何让我的机器人读取发送给它的 DM 并打印它们或将它们发送到特定频道
【发布时间】:2020-06-30 08:40:06
【问题描述】:

所以我最近创建了一个包含各种 meme 命令和审核命令的不和谐机器人。我对 python 比较陌生,但我理解它的要点。我已经创建了一个命令,让我(只有我)通过机器人 DM 用户。我现在想尝试让机器人能够读取发回给它的消息并将它们发送给我,无论是打印在 shell 中还是发送到特定的频道/我,我不在乎,我只想能够看到发送给它的内容。 我做了一些阅读,找到了this,并从中收集了这个:

@bot.event
async def on_message(message):
    channel = bot.get_channel('channel ID')
    if message.server is None and message.author != bot.user:
        await bot.send_message(channel, message.content)
    await bot.process_commands(message)

仅此一项对我不起作用,当我对机器人进行 DM 管理时,我收到一条错误消息,提示“AttributeError:'Message' 对象没有属性 'server'”。有人告诉我 discord.py rewrite 不使用“服务器”,而是使用“公会”。所以我把它改成了message.guild。从那里它给了我错误“AttributeError:'Bot'对象没有属性'send_message'”,这就是我到达那里的程度。我已经对其进行了修补,并在这里和那里改变了一些东西,并取得了一些轻微的进展......我想。我的新代码是:

@bot.event
async def on_message(ctx, message):
    channel = bot.get_channel('channel ID')
    if message.guild is None and message.author != bot.user:
        await ctx.send(channel, message.content)
    await bot.process_commands(message)

这给了我错误“TypeError: on_message() missing 1 required positional argument: 'message'”。 这就是我现在所得到的。任何帮助都将不胜感激,正如我所说,我对 python 还有些陌生,我大约 2 个月前才开始使用它。

【问题讨论】:

    标签: python bots discord discord.py discord.py-rewrite


    【解决方案1】:

    您正在使用命令特有的 ctx 参数。

    'ctx' 是commands.Context,它提供有关已执行命令的信息。 由于这不是命令,而是事件,因此您不应该提供它。

    未经测试,但这应该可以解决问题:

    (我这样做是为了让它忽略来自所有机器人的消息,以避免垃圾邮件)

    用于将消息打印到控制台

    @bot.event
    async def on_message(message: discord.Message):
        if message.guild is None and not message.author.bot:
            print(message.content)
        await bot.process_commands(message)
    

    用于将消息内容转储到频道

    msg_dump_channel = 1234
    @bot.event
    async def on_message(message: discord.Message):
        channel = bot.get_channel(msg_dump_channel)
        if message.guild is None and not message.author.bot:
            # if the channel is public at all, make sure to sanitize this first
            await channel.send(message.content)
        await bot.process_commands(message)
    

    【讨论】:

      【解决方案2】:

      您的第一个代码使用的是古老、过时且不受支持的 dpy 版本。

      除了on_message 事件之类的一些错误之外,您在第二个代码中大部分都正确更新了它。

      它不起作用,因为它只接受一个参数message,在那个事件中没有ctx

      所以事件传递了一个参数,但你的函数需要 2 因此错误。正确的代码是: async def on_message(message):

      但现在你会困惑如何处理你写的这行代码: await ctx.send(channel, message.content)

      即使ctx 存在,这也行不通,因为 send 不再使用目的地,而是在目的地 destination.send("Hello") 上调用 send ,因此如果 channel 是您要发送的位置,则将其转换为您的代码是await channel.send(message.content)

      最后一点我注意到你在这里使用了字符串: bot.get_channel('channel ID') 这可能只是您举例,但请记住 IDs are ints,如果您传递字符串,它将永远找不到该频道。

      例如这是无效bot.get_channel('123456789111315178')

      这将是有效的:bot.get_channel(123456789111315178)

      【讨论】:

      • 好的,所以我早些时候回答说,看看机器人是否可以做类似“发送用户#0000:插入消息”之类的事情。但我想通了并删除了回复。但现在我想看看如何让机器人获取附件 URL 并发送它们。这是我走了多远:@bot.event async def on_message(message: discord.Message, attachment: discord.Attachment): if message.guild is None and not message.author.bot: print(message.author , "sent: ", message.content) if attachment.size > 0: print(attachment.url) await bot.process_commands(message)
      • 这似乎是一个新问题,但无论如何,正如我之前所说的 on_message 只接受一个参数。你只会得到message 使用,永远。因此,您可以使用 message.attachments discordpy.readthedocs.io/en/latest/… 访问该消息中的附件
      【解决方案3】:

      基于该错误,我想说您可能会在某个时候调用 on_message() 而没有提供 message 参数。查看您的代码,看看是否可能是这种情况。

      或者,如果是您尚未编写的其他代码调用您的函数,那么它可能只使用一个参数调用 on_message(),在这种情况下,ctx 参数可能与该函数不兼容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-11
        • 2020-05-17
        • 2021-08-21
        • 2020-02-09
        • 1970-01-01
        • 1970-01-01
        • 2021-01-07
        • 2023-01-17
        相关资源
        最近更新 更多