首页 > 解决方案 > Discord.py 中 cog 的延迟未被识别为有效属性

问题描述

起初,当我在一个文件中为我的不和谐机器人创建所有命令而不在 cogs 中重新组织它时,一切都运行良好。现在我创建了 cogs(并将我的 ping 命令放在那里),我的程序不再将“延迟”识别为属性。我的其余命令(ban、unban、kick、clearText)奇怪地仍然正常运行,没有任何错误。

import discord
from discord.ext import commands

class basicComs(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    async def ping(self, ctx):
        await ctx.send(f"Pong {round(commands.latency * 1000)}ms")

def setup(client):
    client.add_cog(basicComs(commands))

错误信息是:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: module 'discord.ext.commands' has no attribute 'latency'

谢谢:)

标签: pythonpython-3.xdiscorddiscord.py

解决方案


这是因为 discord.ext.commands 没有延迟属性。延迟属性仍然属于您的客户端,即discord.ext.commands.Bot

因此,即使在迁移到 cogs 之后,您仍然必须消除客户端的延迟。此外,您没有在最后一行将 Cog 添加到您的客户端。

    @commands.command()
    async def ping(self, ctx):
        await ctx.send(f"Pong {round(self.client.latency * 1000)}ms")

def setup(client):
    client.add_cog(basicComs(client))

推荐阅读