首页 > 解决方案 > 如何让 discord.py 机器人通过控制台向特定频道发送消息

问题描述

discord.py - 通过控制台发送消息- 这个问题已经过时,所以我将尝试重新询问,因为那里提供的解决方案不起作用。

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

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'

任何帮助表示赞赏。

标签: pythondiscord.py

解决方案


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

  2. 我不得不说我很困惑为什么你getchannel_entry.get. 只需使用整数本身。这可能是您错误的根源。因为你没有向它传递一个整数,client.get_channel所以它返回一个 None 对象,因为没有找到通道。文件

工作示例:

channel_entry = 614001879831150605
msg_entry = 'test'

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

还要确保您安装了最新版本的 discord.py。


推荐阅读