简体   繁体   English

如何将我的 discord 机器人变成使用斜杠命令的机器人?

[英]How can I turn my discord bot into one which works with slash commands instead?

I made a nice and simple discord bot, here's the source code:我制作了一个漂亮而简单的 discord 机器人,这是源代码:

import discord

TOKEN = 'hi:)'


intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)


@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)

    print(f'{username}: {user_message} ({channel})')  # chat logs

    if message.author == client.user:  # so that the bot wouldn't reply to itself to infinity
        return

    if message.channel.name == 'bot-commands':
        if user_message.lower() == 'hello':
            await message.channel.send(f'Oh hello')
            return

        if user_message.lower() == 'bye':
            await message.channel.send(f'Goodbye')
            return

    if user_message.lower() == '!anywhere':
        await message.channel.send('This can be used anywhere!')
        return


client.run(TOKEN)

The bot also has a bunch of other cool (and more complex) commands that aren't shown here.该机器人还有许多此处未显示的其他很酷(且更复杂)的命令。

Now, how can I upgrade it all so that it'd work based on slash commands instead?现在,我怎样才能升级它,让它基于斜杠命令工作呢? I don't want to start from scratch, it took a long time to develop it.我不想从头开始,开发它花了很长时间。 So is there some quick "upgrade" that I could apply to it which would make it operate on slash commands?那么有没有一些我可以应用到它的快速“升级”,使它可以在斜杠命令上运行?

If not, how can I implement slash commands in addition to what I have?如果没有,除了我拥有的之外,我如何实现斜杠命令? I think having both normal and slash commands is a good compromise.我认为同时使用普通命令和斜线命令是一个很好的折衷方案。

Is there some template I can use for slash commands?有没有我可以用于斜线命令的模板? I mean for normal commands there clearly is a template, you simply insert:我的意思是对于普通命令,显然有一个模板,您只需插入:

if user_message.lower() == 'insert some command here':
    # you do coding magic and then do:
    await message.channel.send(f'your result is here')
    return

If there is such template, what is it?如果有这样的模板,它是什么? Do I need to insert other things to the code so that such a template would work?我是否需要在代码中插入其他内容,这样的模板才能工作?

If you don't know how to help with any of the questions above, what tutorials do you recommend?如果您不知道如何解决上述任何问题,您推荐哪些教程? I tried a few yt tutorials already (from Civo and Glowstik), but neither of them worked.我已经尝试了一些 yt 教程(来自 Civo 和 Glowstik),但它们都没有用。 One had errors, even though I copied the code word-for-word.一个有错误,即使我逐字复制了代码。 The other just wouldn't show slash commands at all.另一个根本不会显示斜杠命令。

The code you posted is for event listeners mainly.您发布的代码主要用于事件侦听器。

Have like this if you want both prefix & slash command for your bot.如果你想为你的机器人同时使用前缀斜杠命令,就这样吧。

from discord.ext.commands import when_mentioned_or

intents = discord.Intents.all()
client = Client(command_prefix=when_mentioned_or("/"), intents=intents)

To have slash commands, you have to use like:要使用斜线命令,您必须使用类似:

commands.slash_command() => if you using in cogs commands.slash_command() => 如果你在 cogs 中使用

client.slash_command() => if you using in single file client.slash_command() => 如果你在单个文件中使用

for example:例如:

@commands.slash_command(description="test")
async def test(self, ctx):
await ctx.respond("Test complete!")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用斜杠命令在我的 discord.py v2.1 机器人上添加选项? - How do I add options on my discord.py v2.1 bot with slash commands? 我怎样才能让我只能在我的 Discord 机器人上使用某些命令? (Python) - How can I make sue only I can use certain commands on my Discord bot? (Python) 如何通过pycord中的斜杠命令在discord bot中生成图像? - How to generate images in discord bot through slash commands in pycord? 如何修复命令不起作用的 Discord python 机器人 - How can I fix a Discord python bot whose commands are not working 我无法使用 discord.py 向我的不和谐机器人添加命令 - I can't add commands to my discord bot with discord.py 在 discord.py 我怎样才能让我的机器人命令只能在特定通道或特定服务器中使用? - in discord.py How can i make my bot that the commands only can be use in specific channel or specific server? 如何让我的机器人的 Slash 响应检测 discord.py 中的反应? - How do I make my bot's Slash response detect reactions in discord.py? (discord.py)我怎样才能让我的机器人在当前命令完成之前不回复其他命令 - (discord.py) How can I make it so my bot that doesn't reply to other commands until the current command is finished 我怎样才能让我的 discord.py 机器人将多个 args 识别为一个? - How can i do my discord.py bot recognize multiple args as just one? Discord 机器人没有响应我的命令 - Discord bot not responding to my commands
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM