【问题标题】:What is the difference between @client.command and @commands.command@client.command 和 @commands.command 有什么区别
【发布时间】:2021-12-13 05:31:10
【问题描述】:
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")

@client.command()@commands.command() 都有效,实际上有什么区别?

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

【问题讨论】:

  • commands.command() 在扩展中使用,因为客户端没有在那里定义。我不相信有任何区别。
  • 加上 Taku 所说的,commands.command() 用于 cogs。

标签: python discord discord.py


【解决方案1】:

使用 @client/bot.commands() 是指您使用的文件未在 cog 或其他文件夹中调用。

@commands.command() 是当客户端/机器人未定义时,也使得在您的 async def 参数中必须调用客户端

例子:

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

或者对于齿轮:

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

【讨论】:

    【解决方案2】:

    @client.command() 是一个只能在主文件(定义了客户端)中使用的命令

    @commands.command() 可以在 cogs 和主文件中使用。这比一个巨大的 3k 行长的主文件要干净得多

    【讨论】:

      猜你喜欢
      • 2010-10-02
      • 2011-12-12
      • 2010-09-16
      • 2012-03-14
      • 2012-02-06
      • 2011-02-25
      • 2011-11-22
      • 2015-03-26
      • 2013-08-19
      相关资源
      最近更新 更多