【问题标题】:Discord.py ping command doesn't work in a cogDiscord.py ping 命令在 cog 中不起作用
【发布时间】:2021-12-04 12:38:13
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 2019-07-23
      • 1970-01-01
      • 2020-11-21
      • 2021-06-29
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多