You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord Python Bot无法响应命令,报错'TextChannel' object is not callable

问题

参照YouTube视频开发Discord Python Bot,程序编译通过且Bot已上线,但无法响应命令。控制台输出如下:

rug#5048 is now running!
rugmaster76 said: "hello" (general)
'TextChannel' object is not callable

发送"hello"时,Bot应回复"Hey there!",相关代码如下:

bot.py

import discord
import responses


async def send_message(message, user_message, is_private):
    try:
        response = responses.get_response(user_message)
        await message.author.send(response) if is_private else await message.channel(response)

    except Exception as e:
        print(e)


def run_discord_bot():
    TOKEN = 'MTEyODExMDc4NDA4MjM1NDIxNg.GLoasi.NN3Yy2kbyDJ4yBGhPloDE3onIEhQU5OpzumtM4'
    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)

    @client.event
    async def on_ready():
        print(f'{client.user} is now running!')

    @client.event
    async def on_message(message):
        if message.author == client.user:
            return

        username = str(message.author)
        user_message = str(message.content)
        channel = str(message.channel)

        print(f'{username} said: "{user_message}" ({channel})')

        if user_message[0] == '?':
            user_message = user_message[1:]
            await send_message(message, user_message, is_private=True)
        else:
            await send_message(message, user_message, is_private=False)


    client.run(TOKEN)

responses.py

import random


def get_response(message: str) -> str:
    p_message = message.lower()

    if p_message == 'hello':
        return 'Hey there!'

    if message == 'roll':
        return str(random.randint(1, 6))

    if p_message == '!help':
        return'`this is a help message that u can modify`'

    return 'i didnt understand what u wrote, try typing "!help."'

main.py

import bot

if __name__ == '__main__':
    bot.run_discord_bot()
解决方案

核心错误修复

控制台报错'TextChannel' object is not callable,原因是message.channel是频道对象,不能直接调用,需要使用send()方法发送消息。修改bot.py中的send_message函数:

async def send_message(message, user_message, is_private):
    try:
        response = responses.get_response(user_message)
        # 将 message.channel(response) 改为 message.channel.send(response)
        await message.author.send(response) if is_private else await message.channel.send(response)
    except Exception as e:
        print(e)

潜在问题优化

  1. 命令大小写兼容:responses.py中roll命令未转小写,用户输入大写或混合大小写时无法触发,修改为:
# 原代码:if message == 'roll':
if p_message == 'roll':
    return str(random.randint(1, 6))
  1. 空输入防护:当用户只输入?时,user_message[0] == '?'会触发索引错误,在bot.py的on_message函数中添加长度判断:
# 原代码:if user_message[0] == '?':
if len(user_message) > 0 and user_message[0] == '?':
    user_message = user_message[1:]
    await send_message(message, user_message, is_private=True)

修改后Bot即可正常响应"hello"等命令。

内容的提问来源于stack exchange,提问作者Rug Master

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 05:33:15