(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

查看:97
本文介绍了(discord.py)如何让我的机器人读取发送给它的DM并打印它们或将其发送到特定频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最近创建了一个带有各种meme命令和主持人命令的discord机器人.我对python比较陌生,但我了解它的要旨.我已经创建了一个命令,该命令使我(只有我自己)可以通过该机器人DM用户.我现在想尝试使该机器人能够读取发送回它的消息并将其发送给我,无论它是打印在外壳中还是发送到我不在乎的特定频道/我,我只想能够看到发送给它的东西.我四处阅读并发现

So I recently created a discord bot with various meme commands and moderating commands. I am relatively new to python but I understand the gist of it. 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. 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)

仅此一项对我而言不起作用,当我对机器人进行DM处理时,出现一条错误消息,提示"AttributeError:'Message'对象没有属性'server'".有人告诉我discord.py重写不使用服务器",而是使用行会".因此,我将其更改为message.guild.从那里它给了我一个错误"AttributeError:'Bot'对象没有属性'send_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. I was told that discord.py rewrite does not use 'server', but rather 'guild'. So I changed it to say 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. 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)

这给了我错误"TypeError:on_message()缺少1个必需的位置参数:'message'".到目前为止,我已经知道了.如我所说,任何帮助将不胜感激,我对python还是有些陌生,我大约在2个月前才开始使用它.

This gives me the error "TypeError: on_message() missing 1 required positional argument: '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.

推荐答案

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

Your first code is using ancient, outdated and unsupported dpy version.

除了大多数错误,例如

You mostly updated it correctly in the second code except for few mistakes like the on_message event.

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

It is not working because it only takes one argument message, there is no ctx in that event.

因此,该事件传递了一个参数,但您的函数接受了2个错误.正确的代码是:异步定义on_message(message):

So the event is passing one argument but your function takes 2 thus the error. The correct code would be: async def on_message(message):

但是,现在您将对使用此行写的内容感到困惑:等待ctx.send(频道,消息内容)

But now you'll be confused what to do with this line you wrote: await ctx.send(channel, message.content)

即使存在 ctx ,这也不起作用,因为send不再将目的地作为目的地,而是您在目的地 destination.send("Hello")上调用send,因此如果您要发送 channel 的位置被翻译为您的代码,则为 await channel.send(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)

还有一个最后的说明,我注意到您在这里使用了字符串: bot.get_channel('频道ID')这可能只是您的示例,但请记住

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('123456789111315178')

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

这篇关于(discord.py)如何让我的机器人读取发送给它的DM并打印它们或将其发送到特定频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆