我最近开始尝试开发一个discord机器人。我以前使用过C++,但这是我第一次使用Python,所以我对它仍然很陌生。在了解cogs之后,我尝试将其实现到代码中。我制作了一个包含ping命令的简单cog,但是当我尝试运行它时,我得到一个错误,说“客户机”没有被识别。 这是cog文件中的代码:
import discord
from discord.ext import commands
#initializing
class Commands(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command(aliases=['hi','hello','test'])
async def ping(self, ctx):
await ctx.send(f'Pong! Your request took {round(client.latency * 1000)}') #says 'client' was not idenified
def setup(client):
client.add_cog(Commands(client))
这是main.py文件的代码:
import discord
import os
from discord.ext import commands
client = commands.Bot(command_prefix='trs-')
@client.command()
async def reload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
client.load_extension(f'cogs.{extension}')
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
@client.event
async def on_ready():
print(f'Bot is online. Logged in as {client.user.name}')
await client.change_presence(status=discord.Status.online)
await client.change_presence(activity=discord.Game("with my creator's mind"))
client.run(token goes here)
我尝试将client = commands.Bot(command_prefix='trs-')
放在cog文件中,它运行良好,但是当我尝试在discord上运行ping命令时,我在控制台中从wait ctx.send行得到一个错误,说ValueError: cannot convert float NaN to integer
我似乎无法解决这个问题,有人能告诉我我做错了什么吗
导致此错误的原因是您试图将client.latency转换为整数,因为client.latency返回的值为None。它会出错的
相关问题 更多 >
编程相关推荐