【问题标题】:Latency in a cog in Discord.py isn't recognized as a valid attributeDiscord.py 中 cog 的延迟未被识别为有效属性
【发布时间】:2021-03-23 14:04:03
【问题描述】:

起初,当我在一个文件中为我的 discord 机器人创建所有命令而不在 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'

谢谢:)

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    这是因为 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))
    

    【讨论】:

    • 感谢您的帮助,但它仍然显示相同的错误消息。
    • @wurstm162 抱歉,我没早点看到,检查你的最后一行:client.add_cog(basicComs(commands)) ...(basicComs(client) 编辑我的答案
    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多