commands.Bot 和 interactions.Client 之间的区别

huangapple go评论58阅读模式
英文:

Difference between commands.Bot and interactions.Client

问题

我已经写了一个 Discord 机器人已经有一周了,今天尝试领取 Discord Active Developer Badge。令我惊讶的是,我无法领取它 - 我检查了要求,发现我的机器人本月需要执行至少1个命令 - 我肯定已经完成了。我进一步研究了一下,似乎 Discord 鼓励人们使用斜杠命令而不是我用我的机器人使用的前缀命令。以下是一个示例:

# 基于前缀的
import discord
from discord.ext import commands

permissions = discord.Intents()
permissions.message_content = True
permissions.messages = True
permissions.reactions = True
bot = commands.Bot(command_prefix="/", intents=permissions)

@bot.command()
async def test(ctx: commands.Context):
    await ctx.send("Registered!")

bot.run(TOKEN_HERE)

经过一段时间的尝试,我无法弄清楚如何使用常规的 Discord 库创建斜杠命令,但我找到了一个名为 "Interactions" 的库,通过该库,我可以定义一个所谓的命令架构并执行某些操作。以下是一个示例:

# 基于斜杠的
import discord
import interactions

bot = interactions.Client(TOKEN_HERE)
@bot.command(
    name="testing",
    description="testing_command!"
)
async def test(ctx: interactions.CommandContext):
    await ctx.send("Registered!")

bot.start()

这被认为是斜杠命令,它甚至在 Discord 中创建了帮助信息:
commands.Bot 和 interactions.Client 之间的区别

问题是,我应该继续使用 commands.Bot 机器人进行开发,还是切换到 interactions 库并学习它?我是否可以以某种方式迁移我当前已存在的机器人或添加一个所谓的架构,而不使用 interactions 库?

只是令人讨厌的是,我在已经对常规的 discord 库非常熟悉的情况下才发现了这一点。

英文:

I've been writing a discord bot for a week now and tried claiming Discord Active Developer Badge today. I was surprised, because I couldn't claim it - I checked requirements and my bot needed to execute at least 1 command this month - certainly I completed it already. I researched it a bit deeper, and it seems that discord encourages people to use slash-commands and not prefix-based commands as I did with my bot. Here's an example of it:

# Prefix based
import discord
from discord.ext import commands

permissions = discord.Intents()
permissions.message_content = True
permissions.messages = True
permissions.reactions = True
bot = commands.Bot(command_prefix="/", intents=permissions)

@bot.command()
async def test(ctx: commands.Context):
    await ctx.send("Registered!")

bot.run(TOKEN_HERE)

After a good amount of time, I couldn't figure out how to create slash commands using regular discord libraries, but I found an "Interactions" library, with which, I could define a so called command schema and do something with it. Here's an example:

# Slash based
import discord
import interactions

bot = interactions.Client(TOKEN_HERE)
@bot.command(
    name="testing",
    description="testing_command!"
)
async def test(ctx: interactions.CommandContext):
    await ctx.send("Registered!")

bot.start()

This is considered slash command, it even creates a helping information in the discord itself:
commands.Bot 和 interactions.Client 之间的区别

The question is, should I continue developing with commands.Bot bot or switch to the interactions library and learn it instead? Can I migrate my currently existing bot somehow or add a so-called schema without using interactions library?

It's just annoying that I found out about it when I'm already really familiar with regular discord library

答案1

得分: 1

以下是翻译好的部分:

"discord"库具有内置的斜杠命令,使用app_commands.CommandTree。以下是示例:

import discord
from discord import app_commands

class MyClient(discord.Client):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
    async def on_ready(self):
        print('准备就绪')

client = MyClient(intents=discord.Intents.default())
tree = app_commands.CommandTree(client)

@tree.command(name='mycommand')
async def mycommand(interaction: discord.Interaction):
    await interaction.response.send_message('你好,世界!')
    # 请注意Interaction和InteractionResponse之间的区别

是否更好地使用斜杠命令?

简短回答:是的

长回答:斜杠命令比普通前缀命令更灵活。尽管输入前缀命令更快捷和方便,但斜杠命令的优势超过前缀命令。Discord 未来也会更专注于开发支持斜杠命令的库,因此您可以期待将来有更多功能面向斜杠命令。

有用的斜杠命令功能包括限制用户输入、更好地处理输入参数等等。

互动 vs Discord

我个人从未使用过"互动"库,而一直使用"discord"。然而,根据您的代码示例,似乎很容易将前缀命令转换为斜杠命令。

互动库的文档中可以看到:
> 即使它能够正常工作,我们强烈建议不要使用discord.py与我们的库一起使用。

> 此外,除了“不要使用discord.py”之外,我们将不会提供任何使用discord.py与我们的库相关的问题的帮助。

互动库的开发人员不建议使用discord.py,这使得它们不太可能与discord.py一起使用。

在可预见的将来,使用discord.py可能更好,因为它专为Discord设计。然而,我认为这归结为个人偏好,但最好选择两者中的一个而不是同时使用两者。


有关使用discord.py的斜杠命令的更多信息,
discord.Interactiondiscord.app_commands.CommandTreediscord.Client

英文:

The discord library does have in-built slash commands with app_commands.CommandTree. Here's an example of that

import discord
from discord import app_commands

class MyClient(discord.Client):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
    async def on_ready(self):
        print('Ready')

client = MyClient(intents=discord.Intents.default())
tree = app_commands.CommandTree(client)

@tree.command(name='mycommand')
async def mycommand(interaction: discord.Interaction):
    await interaction.response.send_message('Hello World!')
    # Important to note the difference between Interaction and InteractionResponse

Is it better to develop with slash commands?

Short answer: Yes

Long answer: Slash commands are more versatile than normal prefix commands. Although typing out prefix commands is faster and more convenient, the advantages of slash commands outweigh that of prefix commands. Discord would also be more focused on developing the library with slash commands than prefix commands so you would expect more features to be catered towards slash commands in the future.

Useful features of slash commands include, limiting user input, better handling of input arguments, and much more.

Interactions vs Discord

I've personally never used the interactions library and stuck with discord. However, looking at your code example, it seems like an easy way to convert prefix commands to slash commands.

From the documentation of Interactions library
> Even if it is working, we strongly advise against using d.py with our library.

> Additionally, we will not give help for any issue resulting from using d.py with our library except of “Do not use discord.py”.

The developers of the Interactions library discourage the use of discord.py which makes it unlikely they want to work with them.

For the forseeable future, it might be better to use discord.py because it is meant for discord. However, I feel that this comes down to personal preference but it would be better to choose only one of the two instead of both.


For more information on slash commands using discord.py,
discord.Interaction, discord.app_commands.CommandTree, discord.Client

huangapple
  • 本文由 发表于 2023年5月28日 02:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定