(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
所以我最近创建了一个带有各种模因命令和审核命令的不和谐机器人。我对 python 比较陌生,但我理解它的要点。我已经创建了一个命令,让我(只有我)通过机器人与用户私信。我现在想尝试让机器人能够读取发回给它的消息并将它们发送给我,无论它是打印在 shell 中还是发送到特定的 channel/me 我不关心,我只是希望能够看到发送给它的内容。
我四处阅读并找到 ,并从中收集了这个:
@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' object has no attribute 'server'"。有人告诉我 discord.py 重写不使用 'server',而是 'guild'。所以我把它改成message.guild。从那里它给了我错误 "AttributeError: 'Bot' object has no attribute '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 个月前才开始使用它。
基于那个错误,我会说你可能在某个时候调用了 on_message() 而没有给出 message
参数。查看您的代码,看看是否可能是这种情况。
或者,如果是您未编写的其他代码调用您的函数,那么它可能仅使用一个参数调用 on_message(),在这种情况下,ctx 参数可能与该参数不兼容函数。
您正在使用命令独有的 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)
您的第一个代码使用的是古老、过时且不受支持的 dpy 版本。
除了像 on_message 事件这样的少数错误外,您在第二个代码中的大部分更新都是正确的。
它不起作用,因为它只接受一个参数 message
,那个事件中没有 ctx
。
因此该事件传递了一个参数,但您的函数接受了 2 个参数,因此出现了错误。正确的代码是:
async def on_message(message):
但是现在你会困惑如何处理你写的这一行:
await ctx.send(channel, message.content)
即使 ctx
存在,这也行不通,因为发送不再以目的地为目的地,而是您在目的地 destination.send("Hello")
上调用发送,因此如果 channel
是你想发送的地方 await channel.send(message.content)
还有最后一点,我注意到你在这里使用了字符串:
bot.get_channel('channel ID')
这可能是您只是举个例子,但请记住 IDs are ints,如果您传递字符串,它将永远找不到该频道。
例如这是无效的:
bot.get_channel('123456789111315178')
这将是有效的:bot.get_channel(123456789111315178)
所以我最近创建了一个带有各种模因命令和审核命令的不和谐机器人。我对 python 比较陌生,但我理解它的要点。我已经创建了一个命令,让我(只有我)通过机器人与用户私信。我现在想尝试让机器人能够读取发回给它的消息并将它们发送给我,无论它是打印在 shell 中还是发送到特定的 channel/me 我不关心,我只是希望能够看到发送给它的内容。
我四处阅读并找到
@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' object has no attribute 'server'"。有人告诉我 discord.py 重写不使用 'server',而是 'guild'。所以我把它改成message.guild。从那里它给了我错误 "AttributeError: 'Bot' object has no attribute '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 个月前才开始使用它。
基于那个错误,我会说你可能在某个时候调用了 on_message() 而没有给出 message
参数。查看您的代码,看看是否可能是这种情况。
或者,如果是您未编写的其他代码调用您的函数,那么它可能仅使用一个参数调用 on_message(),在这种情况下,ctx 参数可能与该参数不兼容函数。
您正在使用命令独有的 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)
您的第一个代码使用的是古老、过时且不受支持的 dpy 版本。
除了像 on_message 事件这样的少数错误外,您在第二个代码中的大部分更新都是正确的。
它不起作用,因为它只接受一个参数 message
,那个事件中没有 ctx
。
因此该事件传递了一个参数,但您的函数接受了 2 个参数,因此出现了错误。正确的代码是:
async def on_message(message):
但是现在你会困惑如何处理你写的这一行:
await ctx.send(channel, message.content)
即使 ctx
存在,这也行不通,因为发送不再以目的地为目的地,而是您在目的地 destination.send("Hello")
上调用发送,因此如果 channel
是你想发送的地方 await channel.send(message.content)
还有最后一点,我注意到你在这里使用了字符串:
bot.get_channel('channel ID')
这可能是您只是举个例子,但请记住 IDs are ints,如果您传递字符串,它将永远找不到该频道。
例如这是无效的:
bot.get_channel('123456789111315178')
这将是有效的:bot.get_channel(123456789111315178)