Home > Software design >  What is the difference between @client.command and @commands.command
What is the difference between @client.command and @commands.command

Time:10-29

import discord
from discord.ext import commands

client = commands.Bot(command_prefix = "", intents = discord.Intents.all())

class music(commands.Cog):
    def __init__(self, client):
        self.client = client

    @client.command()
    async def join(self, ctx):
        if ctx.author.voice is None:
            await ctx.send("You are not in a voice channel")

Both @client.command() and @commands.command() works, what does the differences actually ?

import discord
from discord.ext import commands

client = commands.Bot(command_prefix = "", intents = discord.Intents.all())

class music(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def join(self, ctx):
        if ctx.author.voice is None:
            await ctx.send("You are not in a voice channel")

CodePudding user response:

Using @client/bot.commands() is when you are using a file that isn't being called in a cog or another folder.

@commands.command() is when client/bot isn't defined also making it so in your async def parameter have to call client

Example:

@client.command()
async def hi(ctx):
  await ctx.send("Hey!")

Or for cogs:

@commands.command()
async def hi(client,ctx):
  await ctx.send("Hey")

CodePudding user response:

@client.command() is a command that can only be used in the main file (where client is defined)

@commands.command() is can be used in both cogs and the main file. This is way cleaner than a huge 3k lines long main file

  • Related