【发布时间】:2021-06-18 15:40:16
【问题描述】:
我遇到了问题。
我正在使用discord.py-1.7.3 和此代码来获取和回复 DM 中的消息:
import discord
import os
import requests
import json
with open("config.json") as f:
configData = json.load(f)
token = configData["TOKEN"]
client = discord.Client()
@client.event
async def on_message(message):
if message.channel.id == message.author.dm_channel.id:
msg = message.content
msg_low = msg.lower()
if message.author == client.user:
return
if msg_low.startswith('name'):
try:
print(message.content)
await message.author.send("Hello {username} {id}".format(username = message.author.name, id = message.author.id))
except:
print("problem")
一切正常 - 直到我发送消息。我一发,
我在代码中收到此错误
AttributeError: 'NoneType' object has no attribute 'id'
在这一行
if message.channel.id == message.author.dm_channel.id:
它也忽略了这样的异常:
Ignoring exception in on_message
我需要如何更改代码,或者我应该怎么做?
【问题讨论】:
-
这发生在哪一行?已经是minimal reproducible example 了吗?异常的完整回溯是什么?无论如何,错误非常明显,并且有几个关于该错误的类似问题,请阅读其中的一些。
-
这一行出现错误。
if message.channel.id == message.author.dm_channel.id:我已经阅读了这些,但很抱歉它没有解决问题。这是我可以提供的最少代码以便更好地理解。这是回溯:Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs)我希望它有所帮助。 @UlrichEckhardt -
message.channel是None。你应该问自己为什么会这样,因为这一切都取决于此。另外,请将回溯添加到问题中,而不是 cmets。所有相关信息都应该在那里。
标签: python discord discord.py