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

Discord.py机器人无法检测消息且$hello命令失效的问题求助

Discord.py机器人无法检测消息且$hello命令失效的问题求助

我写了一个Python Discord机器人,控制台输入的命令都能正常工作,但它完全不响应Discord里发送的消息。这个机器人本来应该把Discord里(机器人有权限访问的频道和私信里)的消息打印到控制台,同时也支持通过控制台输入命令往Discord发消息。

现在的情况是控制台命令没问题,但机器人既不把Discord消息打印到控制台,$hello命令也没反应,而且没有任何报错信息。我一开始以为是那个while True循环的问题,试着把它放到线程里运行,但问题还是没解决。

以下是我的代码:

import discord
import os
import asyncio

cchannel = 1257783751249891361
cdm = 962026630497321000

token = os.environ['toke']

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

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

    print(message.content)

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

async def command_loop():
    while True:
        global cchannel
        global cdm
        commando = input("$ ")
        commandy = commando.split(" ", 1)
        command = commando.split(" ", 1)[0]
        if command == "exit":
            print("Shutting down the bot...")
            await client.close()
            break
        elif command == "say":
            print(commandy[1])
            cchannel2 = await client.fetch_channel(cchannel)
            await cchannel2.send(commandy[1])
        elif command == "setchannel":
            print(commandy[1])
            cchannel = commandy[1]
        elif command == "setdm":
            print(commandy[1])
            cdm = commandy[1]
        elif command == "dm":
            print(commandy[1])
            cdm2 = await client.fetch_user(cdm)
            await cdm2.send(commandy[1])
        else:
            print(f"Command received: {command}")

async def main():
    bot_task = asyncio.create_task(client.start(token))
    loop_task = asyncio.create_task(command_loop())

    done, pending = await asyncio.wait(
        [bot_task, loop_task],
        return_when=asyncio.FIRST_COMPLETED
    )

    for task in pending:
        task.cancel()

asyncio.run(main())

有没有大佬能帮我看看问题出在哪呀?

备注:内容来源于stack exchange,提问作者Kit64

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 18:03:13