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,提问作者фёдор бажуков
相关产品推荐
相关产品推荐

