discord.py channel.send AttributeError:“ NoneType”对象没有属性“ send”

时间:2020-10-13 20:37:17

标签: python discord discord.py

好的,我有这样的代码:

# bot.py
import os
import sched
import time
import discord
import datetime

from discord.ext import tasks, commands
from dotenv import load_dotenv

import Library

load_dotenv()
TOKEN = os.getenv('discordToken')

bot = commands.Bot(command_prefix='Question')
client = discord.Client()
lastEmbed = None
# s = sched.scheduler(time.time, time.sleep)
channelId = 692724253237313576
channelId = 765541418701684736
channel = client.get_channel(channelId)

@bot.event
async def on_ready():
    channelId = 765541418701684736
    channel = client.get_channel(channelId)
    print(f'{bot.user.name} has connected to Discord!')
    await channel.send(self.client.get_channel(channelId), "And what")

我对最后一部分有疑问:

await channel.send(self.client.get_channel(channelId), "And what")

我尝试了许多不同的解决方案,但是每次它抛出完全相同的异常时:

Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:\Users\grzes\PycharmProjects\CoronaBot\venv\lib\site-packages\discord\client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/grzes/PycharmProjects/pythonProject1/bot.py", line 57, in on_ready
    await channel.send(self.client.get_channel(channelId), "And what")
AttributeError: 'NoneType' object has no attribute 'send'

2 个答案:

答案 0 :(得分:0)

在使用成员,频道,公会,角色ID时,您需要使用整数类型而不是字符串。您只需将channelId = "765541418701684736"更改为channelId = 765541418701684736

答案 1 :(得分:0)

您不在任何类别中,因此self不存在,类型为NoneType
只需在最后一行的self.之前删除client,即可解决您遇到的错误。

在登录之前,您也无法获得频道,因此应将该行移至on_ready事件中。

假设您提供的所有频道ID均正确,则代码应该可以正常工作。如果找不到具有指定频道ID的频道,则方法client.get_channel(channel_id: int)将返回None,因此,如果尝试按比例放大内容,则以后应添加一些检查。