简体   繁体   English

(discord.py) 如何让我的机器人读取发送给它的 DM 并打印它们或将它们发送到特定频道

[英](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

So I recently created a discord bot with various meme commands and moderating commands.所以我最近创建了一个带有各种 meme 命令和调节命令的 discord bot。 I am relatively new to python but I understand the gist of it.我对 python 比较陌生,但我了解它的要点。 I have already created a command that lets me(only me) DM a user through the bot.我已经创建了一个命令,让我(只有我)可以通过机器人向用户发送消息。 I now want to try and make the bot be able to read the messages that are sent back to it and send them to me, whether its printed in shell or sent to a specific channel/me I don't care, I just want to be able to see what is sent to it.我现在想尝试让机器人能够读取发回给它的消息并将它们发送给我,无论是打印在 shell 中还是发送到特定的频道/我我不在乎,我只想能够看到发送给它的内容。 I did some reading around and found this , and from that I collected 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)

This alone has not worked for me, I get an error saying that "AttributeError: 'Message' object has no attribute 'server'" when I DM'ed the bot.仅此一项对我不起作用,当我对机器人进行 DM 处理时,我收到一条错误消息,指出“AttributeError: 'Message' 对象没有属性 'server'”。 I was told that discord.py rewrite does not use 'server', but rather 'guild'.有人告诉我 discord.py 重写不使用“服务器”,而是“公会”。 So I changed it to say message.guild.所以我把它改成message.guild。 From there it gave me the error "AttributeError: 'Bot' object has no attribute 'send_message'", and that's about as far as I got there.从那里它给了我错误“AttributeError:'Bot'对象没有属性'send_message'”,这就是我到达那里的程度。 I've tinkered with it and changed things here and there and made some slight progress...I think.我已经修补了它,并在这里和那里改变了一些东西,并取得了一些小小的进步......我想。 My new code is:我的新代码是:

@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)

This gives me the error "TypeError: on_message() missing 1 required positional argument: 'message'".这给了我错误“TypeError:on_message() 缺少 1 个必需的位置参数:'message'”。 That is as far as I've gotten now.这就是我现在所得到的。 Any help would be appreciated, as I said, I'm still somewhat new to python, I've only started using it about 2 months ago.任何帮助将不胜感激,正如我所说,我对 python 还是有点陌生​​,我大约 2 个月前才开始使用它。

You're using the ctx parameter, which is unique to commands.您正在使用 ctx 参数,它是命令独有的。

'ctx' is commands.Context, which provides information about commands that are executed. 'ctx' 是 commands.Context,它提供有关已执行命令的信息。 Since this isn't a command, but rather an event, you shouldn't provide it.由于这不是命令,而是事件,因此您不应提供它。

Untested, but this should do the trick:未经测试,但这应该可以解决问题:

(I made it so it ignores messages from ALL bots, to avoid spam) (我这样做是为了忽略来自所有机器人的消息,以避免垃圾邮件)

for printing messages to console用于将消息打印到控制台

@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)

for dumping message contents to a channel用于将消息内容转储到通道

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)

Your first code is using ancient, outdated and unsupported dpy version.您的第一个代码使用的是古老、过时且不受支持的 dpy 版本。

You mostly updated it correctly in the second code except for few mistakes like the on_message event.除了on_message事件等少数错误外,您在第二个代码中大部分都正确更新了它。

It is not working because it only takes one argument message , there is no ctx in that event.它不起作用,因为它只需要一个参数message ,在该事件中没有ctx

So the event is passing one argument but your function takes 2 thus the error.所以事件传递了一个参数,但你的函数需要 2 因此错误。 The correct code would be: async def on_message(message):正确的代码是: async def on_message(message):

But now you'll be confused what to do with this line you wrote: await ctx.send(channel, message.content)但现在你会困惑如何处理你写的这一行: await ctx.send(channel, message.content)

Even if ctx existed this wouldn't work because send doesn't take destination anymore, instead you call send on destination destination.send("Hello") so translated to your code if the channel is where you want to send it would be await channel.send(message.content)即使ctx存在,这也不起作用,因为 send 不再接受目的地,而是在目的地destination.send("Hello")上调用 send,因此如果channel是您要发送的位置,则将其转换为您的代码将是await channel.send(message.content)

Also one final note I noticed that you used string here: bot.get_channel('channel ID') this could be you just giving example but just remember that IDs are ints , if you pass string it will never find that channel.还有最后一个注意事项,我注意到您在这里使用了字符串: bot.get_channel('channel ID')这可能是您只是举例,但请记住ID 是 ints ,如果您传递字符串,它将永远找不到该频道。

For example this is invalid : bot.get_channel('123456789111315178')例如这是无效的bot.get_channel('123456789111315178')

And this would be valid: bot.get_channel(123456789111315178)这将是有效的: bot.get_channel(123456789111315178)

Based on that error, I'd say you're likely calling on_message() at some point and not giving the message parameter.基于该错误,我会说您可能会在某个时候调用 on_message() 而没有提供message参数。 Look through your code and see if that's maybe the case.查看您的代码,看看是否可能是这种情况。

Alternatively, if it's some other code you haven't written that's calling your function, then it might only be calling on_message() with one argument, in which case the ctx argument might be incompatible with that function.或者,如果是您尚未编写的其他一些代码正在调用您的函数,那么它可能只使用一个参数调用 on_message(),在这种情况下,ctx 参数可能与该函数不兼容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我提到他们时,我是否可以使用 discord.py 让我的机器人 DM 某人? - Is it possible for me with discord.py to be able to have my bot DM someone when I mention them? 如何让我的 discord.py 机器人对频道中发送的所有图像做出反应 - How do i make my discord.py bot react to all images sent in a channel 如何让我的 discord.py 机器人删除在特定频道上发送的每条消息? - How can I make my discord.py bot delete every message send on a specific channel? 如何让 discord.py 机器人 DM 特定用户 - How to get discord.py bot to DM a specific user Discord.py 如何让机器人向特定频道发送消息 - Discord.py How to make a bot send message to a specific channel discord.py | 我的机器人无法将 DM 发送给其他机器人 - discord.py | My bot cannot send DM to other bot 如何在 on_message 事件 discord.py 后触发我的机器人向用户发送 DM - How do I trigger my bot to DM a user after an on_message event discord.py discord.py 如何让机器人在特定用户类型时发送消息? - discord.py how do I get a bot to send a message when a specific user types? 为什么我的机器人不向特定频道发送消息? Discord.py 通道 = bot.get_channel() - Why won't my bot send messages to specific channels? Discord.py channel = bot.get_channel() 如何让我的 discord.py 机器人向我选择的频道发送我选择的消息? - how can i make my discord.py bot send a message i choose to a channel i choose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM