如何接收DM并将其记录到文本文件?

时间:2020-04-17 00:04:26

标签: python discord discord.py

我设法通过提供他们的ID和我要发送的消息来使我的机器人将DM发送给人们。问题是人们响应了该漫游器,并且我希望将消息接收到我的控制台窗口(同时列出了例外/错误)。我尝试使用以下代码,但返回的结果是它没有属性“消息”:

@client.event(pass_context=True)
async def on_message(ctx):
    print(f"{ctx.author} said: {ctx.message}")
    await ctx.send(f"Hi, I'm an automated message. I cannot receive any information on your message. All I might get is: {ctx}")
    log = open("commands/info/logging/logmsg.txt", "a")
    log.writelines(f"{ctx.author} has sent: {ctx.message}")

错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\MODERATED\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "showmessages.py", line 18, in on_message
    print(f"{ctx.author} said: {ctx.message}")
AttributeError: 'Message' object has no attribute 'message'

1 个答案:

答案 0 :(得分:1)

on_message收到一个Message对象,而不是Context对象。

@client.event
async def on_message(message):
    if message.author.bot:
        return # ignore our own messages
    print(f"{message.author} said: {message.content}")
    await message.channel.send(f"Hi, I'm an automated message. I cannot receive any information on your message. All I might get is: {message}")
    log = open("commands/info/logging/logmsg.txt", "a")
    log.writelines(f"{message.author} has sent: {message.content}")

请注意,这目前会在机器人可以“看到”的所有消息上触发