python - 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'
谢谢:)
解决方案
这是因为 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))
推荐阅读
- node.js - Lambda function: how to solve asynchronus issue
- c++ - 捕获的静态断言 lambda
- angular - Cannot find module "@angular/common/http"
- java - HTTPS issue with apache cxf client in karaf for url with invalid ssl certificate
- flutter - 如何设置 InputDecoration 的 OutlineInputBorder 样式?
- verilog - 您可以在多个 always 块中使用/操作相同的输出/reg 变量吗?
- tableau-api - Tableau REST API 2.6 中的查询视图数据
- qt - 我们可以使用 PathView 获得 SwipeView 吗?
- laravel - laravel 5.6 从 csrf 中间件中排除 webhook 路由
- php - 如何在PHP中计算相同数组值的总和