[英]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.