Discord.py ping 命令在 cog 中不起作用

时间:2021-01-17 23:36:34

标签: python discord.py discord.py-rewrite

我的 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

1 个答案:

答案 0 :(得分:1)

简单的解决方案,您将 bot 添加到您的类对象 Miscself.bot,因此在该上下文中引用 bot.latency 时,您实际上应该使用 self.bot.latency .