如何使discord.py机器人通过控制台将消息发送到特定频道

时间:2020-04-11 06:42:41

标签: python discord.py

discord.py - Send messages though console-该问题已过时,因此我将尝试重新提出,因为那里提供的解决方案不起作用。

我希望我的不和谐机器人通过控制台中的输入将消息发送到特定频道。我的代码如下:

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'

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

  1. 您不需要将其注册为client.event,因为您不会对在Discord中发生的事情进行反应,这意味着不需要装饰器。

  2. 我不得不说我很困惑,为什么要在get中的整数上使用channel_entry.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。