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

Discord.py中ctx.send无法发送消息问题求助

Discord机器人ctx.send无法发送消息的解决办法

你的代码问题出在hello命令的实现里——调用ctx.send()时没有加await关键字。

ctx.send()是一个异步协程方法,在异步函数(用async修饰的函数)里必须通过await来触发执行,否则这个发送消息的操作根本不会被实际执行。

修正后的代码如下:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='.')

@bot.event
async def on_ready():
    print('BOT connected')

@bot.command()
async def hello(ctx):
    await ctx.send('hello')  # 这里添加了await关键字

token = 'token here hehe'
bot.run(token)

Discord.py框架中几乎所有涉及网络交互的操作都是异步实现的,必须用await等待协程完成,才能确保对应的动作(比如发送消息)被执行。

内容的提问来源于stack exchange,提问作者фёдор бажуков

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 13:22:34