如何使discord.py bot通过控制台向特定频道发送消息

2024-06-16 09:14:12 发布

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

discord.py - Send messages though console-这个问题已经过时了,所以我将尝试重新提问,因为这里提供的解决方案不起作用

我希望我的discord机器人通过控制台中的输入向特定通道发送消息。我的代码如下:

channel_entry = 614001879831150605
msg_entry = 'test'

@client.event
async def send2():
    channel = client.get_channel(channel_entry.get) 
    await channel.send(msg_entry)

我得到以下错误:

AttributeError: 'NoneType' object has no attribute 'send'

感谢您的帮助


Tags: pyclientsend消息getchannel机器人msg
1条回答
网友
1楼 · 发布于 2024-06-16 09:14:12
  1. 您不需要将其注册为client.event,因为您没有对不和谐中发生的事情做出反应,这意味着不需要装饰器

  2. 我必须说,我很困惑为什么要对channel_entry.get中的整数使用get。只需使用整数本身。这可能是您错误的根源。因为没有将整数传递给client.get_channel,所以它返回一个None对象,因为没有找到通道documentation

工作示例:

channel_entry = 614001879831150605
msg_entry = 'test'

async def send_channel_entry():
    channel = client.get_channel(channel_entry) 
    await channel.send(msg_entry)

还要确保已安装最新版本的discord.py

相关问题 更多 >