[英]Discord.py ping command doesn't work in a cog
我的 ping 命令在 cog 中不起作用,但在我的 main.py 文件中起作用。 这是齿轮的代码:
import discord
from discord.ext import commands
class Misc(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
print('Misc cog loaded\n-----')
@commands.command()
async def ping(self, ctx):
await ctx.send(f'pong!\n{round(bot.latency * 1000)}ms')
def setup(bot):
bot.add_cog(Misc(bot))
当我运行 ping 命令时,我收到此错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'bot' is not defined
简单的解决方案,您将bot
添加到 class object Misc
作为self.bot
因此在该上下文中引用bot.latency
时,您实际上应该使用self.bot.latency
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.