首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能在齿轮箱里使用客户呢?discord.py

为什么我不能在齿轮箱里使用客户呢?discord.py
EN

Stack Overflow用户
提问于 2022-01-15 15:59:33
回答 2查看 121关注 0票数 1

这是我在齿轮箱里的代码:

代码语言:javascript
复制
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中得到“未解决的引用”客户端,错误消息是“客户端没有定义”,为什么我会有这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-15 16:13:31

不要使用client.latency,而是使用self.client.latency

代码语言:javascript
复制
    @commands.command()
    async def ping(self, ctx):
        await ctx.send(f"Pong!, {round(self.client.latency * 1000)} ms")
票数 1
EN

Stack Overflow用户

发布于 2022-01-15 16:21:08

使用self.client而不是client

但是,您的代码将始终返回0latency属性将以毫秒为单位返回延迟,例如:0.124,如果您舍入它将成为0 (和0 * 1000 = 0),则应该在圆形函数中进行乘法。

代码语言:javascript
复制
await ctx.send(f"Pong!, {round(self.client.latency * 1000)} ms")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70723069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档