这是我在齿轮箱里的代码:
import discord
from discord.ext import commands
import random
class Basic(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print("Logged In!")
@commands.command()
async def help(self, ctx):
await ctx.send("placeholder text")
@commands.command()
async def ping(self, ctx):
await ctx.send(f"Pong!, {round(client.latency) * 1000} ms")
def setup(client):
client.add_cog(Basic(client))我只是在client.latency中得到“未解决的引用”客户端,错误消息是“客户端没有定义”,为什么我会有这个问题?
发布于 2022-01-15 16:13:31
不要使用client.latency,而是使用self.client.latency。
@commands.command()
async def ping(self, ctx):
await ctx.send(f"Pong!, {round(self.client.latency * 1000)} ms")发布于 2022-01-15 16:21:08
使用self.client而不是client。
但是,您的代码将始终返回0,latency属性将以毫秒为单位返回延迟,例如:0.124,如果您舍入它将成为0 (和0 * 1000 = 0),则应该在圆形函数中进行乘法。
await ctx.send(f"Pong!, {round(self.client.latency * 1000)} ms")https://stackoverflow.com/questions/70723069
复制相似问题