AttributeError:'NoneType'对象没有属性'send'Discord.py rewrite [英] AttributeError: 'NoneType' object has no attribute 'send' Discord.py rewrite
问题描述
我正在尝试向特定频道发送消息.我仍然无法弄清楚如何做到这一点.非常感谢您的帮助.
I'm trying to send a message to a specific channel. I have still not been able to figure out how to do this. Help is greatly appreciated.
import keep_alive
import discord
client = discord.Client()
channel = client.get_channel('ID'`enter code here`)
@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('$hello'):
await channel.send('hello')
keep_alive.keep_alive()
client.run('ID')
推荐答案
get_channel
在您拥有的地方不起作用,因为该机器人尚未连接到Discord(在期间发生)运行
).当漫游器连接时,它将建立它知道的所有内容(成员,行会,渠道等)的内部缓存.这些缓存是各种get方法使用的缓存,但是由于缓存为空,因此这些方法返回 None
.
get_channel
won't work where you have it because the bot hasn't connected to discord yet (that happens during run
). When the bot connects, it will build internal caches of all the things it's aware of (members, guilds, channels, etc.). Those caches are what the various get methods use, but since the caches are empty those methods return None
.
相反,您可以每次在 on_message
中获取频道,或在 on_ready
中使用全局变量:
Instead, you can either get the channel in the on_message
every time, or use a global variable in the on_ready
:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
channel = client.get_channel(1234)
await channel.send('hello')
这篇关于AttributeError:'NoneType'对象没有属性'send'Discord.py rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!