【问题标题】:Discord.py - 'id': target.id AttributeError: 'NoneType' object has no attribute 'id' - in version 1.7.3Discord.py - 'id': target.id AttributeError: 'NoneType' object has no attribute 'id' - in version 1.7.3
【发布时间】: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.channelNone。你应该问自己为什么会这样,因为这一切都取决于此。另外,请将回溯添加到问题中,而不是 cmets。所有相关信息都应该在那里。

标签: python discord discord.py


【解决方案1】:

使用Member.create_dm() 创建 dm 通道,它返回一个 DMChannel 对象,因此将其存储在一个变量中,以便您可以访问其id 属性。 不用担心它不会在 DM 中发送消息,也不会创建一个新的空消息(如果它已经存在)

只有在 author.dm_channel 返回 None 时才能调用它,使用一些基本的 if 语句来避免每次都调用它

@client.event
async def on_message(message):
  dmchannel = await message.author.create_dm()
  if message.channel.id == dmchannel.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")

【讨论】:

  • 其实有DM频道。
  • 我刚刚做了,但现在我收到了这个新错误,而它正在向用户发送消息。 Traceback (most recent call last): dmchannel = await message.author.create_dm() File "C:\ProgramData\Anaconda3\lib\site-packages\discord\user.py", line 764, in create_dm data = await state.http.start_private_message(self.id) File "C:\ProgramData\Anaconda3\lib\site-packages\discord\http.py", line 254, in request raise HTTPException(r, data) discord.errors.HTTPException: 400 Bad Request (error code: 50007): Cannot send messages to this user你能调查一下吗? @AneeshYR
  • @JoyantaJ.Mondal 也许你已经禁用了 DM,或者它正在测试机器人的任何人。如果您与机器人共享服务器,请在右键单击服务器图标时转到隐私设置并选中允许来自服务器成员的 DM。也许请其他人也尝试一下
  • 服务器成员的私信也是允许的。
猜你喜欢
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 2022-01-10
  • 2019-05-28
相关资源
最近更新 更多