在Discord机器人中的斜杠命令

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

Slash command in discord bot

问题

我有这个脚本,但只有在输入“rollmap”或“rollagent”时才能正常工作。有没有办法重新制作它,以便可以使用“/rollmap”或“/rollagent”来触发?

我尝试了很多解决方案,但都没有帮助我,希望您可以在这里帮助我)

英文:
import random
from random import choice

MAPS = ('Ascent' , 'Bind' , 'Breeze' , 'Fracture' , 'Haven' , 'Icebox' , 'Lotus' , 'Pearl' , 'Split')
AGENTS = ('Brimstone' , 'Chamber', 'Cypher' , 'Deadlock' , 'Fade' , 'Gekko', 'Harbor' , 'Jett' , 'KAY/O' , 'Killjoy' , 'Neon' , 'Omen' , 'Phoenix' , 'Raze' , 'Reyna' , 'Skye' , 'Sova' , 'Viper' , 'Yoru' , 'Astra' , 'Breach')

def get_response(message: str) -> str:
    p_message = message.lower()

    if p_message == 'rollmap':
        return str(random.choice(MAPS))

    if p_message == 'rollagent':
        return str(random.choice(AGENTS))

I have this script but it works when typing the word rollmap or rollagent. Is there any way to remake it so that it would be written /rollmap or /rollagent?

I tried many solutions but none helped me, I hope You will help me here)

答案1

得分: 0

我不确定你的问题具体是什么,但如果我理解正确,你想知道如何创建一个既可以使用前缀又可以使用斜杠命令的命令,对吧?

在这种情况下,你可以使用 discord.py 来做类似以下的操作:

import random
from discord.ext import commands

@commands.hybrid_command(
    name="rollmap",
    description="Rolls a random map",
)
async def rollmap(self, context: Context) -> None:
    MAPS = ('Ascent', 'Bind', 'Breeze', 'Fracture', 'Haven', 'Icebox', 'Lotus', 'Pearl', 'Split')
    await context.send(str(random.choice(MAPS)))

你可以对 rollagent 命令做相同的操作。你可以在这里找到相关文档。

英文:

I am not sure what exactly your question is, but if I understand correctly you are asking how to make a command that both works with a prefix and as a slash command?

In that case, using discord.py you can do something like this:

import random
from discord.ext import commands

@commands.hybrid_command(
    name="rollmap",
    description="Rolls a random map",
)
async def rollmap(self, context: Context) -> None:
    MAPS = ('Ascent' , 'Bind' , 'Breeze' , 'Fracture' , 'Haven' , 'Icebox' , 'Lotus' , 'Pearl' , 'Split')
    await context.send(str(random.choice(MAPS)))

You can do the same for the rollagent command. You can find the documentation Here

huangapple
  • 本文由 发表于 2023年7月12日 21:36:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671199.html
匿名

发表评论

匿名网友

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

确定