Skip to content
Advertisement

Client and Cogs in discord.py

I recently started trying to develop a discord bot. I’ve used c++ before, but it’s my first time using python so i’m still pretty new to it. After learning about cogs, i tried to implement it into the code. I made a simple cog that contains a ping command, but when i try to run it, i get an error saying that ‘client’ was not identified. This is the code in the cog file:

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))

This is the code of the main.py file:

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)

I tried putting the client = commands.Bot(command_prefix='trs-') in the cog file and it runs fine, but the when i try to run the ping command on discord i get an error in the console saying ValueError: cannot convert float NaN to integer from the await ctx.send line.

I can’t seem to fix this, can someone please tell me what i’m doing wrong?

Advertisement

Answer

The error caused is because you are trying to convert client.latency to an integer, as client.latency is returning a value of None. It will error out.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement