AttributeError:“非类型”对象没有属性“发送”discord.py

2024-07-05 14:15:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图使用命令.dmtest将dm发送给特定用户,但每当我运行该命令时,就会出现标题中提到的错误 这是我的密码:

import discord

from discord.ext import commands
import os
import requests
import asyncio



from keep_alive import keep_alive

client = discord.Client()

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))



@client.event
async def on_message(message):
  if message.author == client.user:
    return

  if message.content.startswith('.dmtest'):
    user = client.get_user(384185404510961664)
    # await message.author.send(...)
    await message.channel.send('test worked')
    await user.send('a')

keep_alive()
client.run(os.getenv('TOKEN'))

错误:

line 78, in on_message
  await user.send('a')
AttributeError: 'NoneType' object has no attribute 'send'

Tags: fromimport命令clientsendmessageoson
1条回答
网友
1楼 · 发布于 2024-07-05 14:15:15

在对您的代码进行了一些修改之后,我发现在将await user.send('a')替换为await message.author.send('a')之后,我得到了DMed“a”。进行更改后,user = client.get_user(384185404510961664)行是不必要的,可以删除。

编辑:因为这不是你要找的,我在互联网上翻了翻,发现你只需要用await client.fetch_user(id)替换client.get_user(id)

相关问题 更多 >