在浏览现有的高票discord bots时候,发现一个很有趣的功能:

比如上图所示(周董的phantabear nft项目discord群),在输入/invites 后,invite tracker bot允许用户在用户列表里选择一个用户,查看该用户的信息。这个功能就是slash command啦。下面我们一起看一下这个功能要怎么实现吧。
-
生成bot
进入https://discord.com/developers/applications 页面
-> 点击New Application
-> 点击bot,选择add bot

->点击OAuth2URLGenerator选择一下选中选项:

然后该页面拉到底,复制GENERATED URL,将生成的bot邀请到自己的server。 -
代码准备
import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext
from discord_slash.utils.manage_commands import create_choice, create_option
from typing import Optional
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = "t.", intents=intents)
slash = SlashCommand(client, sync_commands=True)
@slash.slash(
name = "invites",
description = "Just test",
guild_ids = [ID], #server id, int
options = [
create_option(
name = "user",
description = "choose a user",
required=True,
option_type = 6 #type 6 is USER
)
]
)
async def _invites(context, user:Optional[discord.User]):
"""重要!!! 此处第二个arg的名字一定要和上面create_option 里name的value保持一致 """
await context.send(随便想send什么)
client.run("你的bot token")
完结撒花🎉
创建Discord Slash命令:用户信息查看功能
本文档介绍了如何在Discord上创建一个slash命令,让用户能够通过输入/invites来查看其他成员的信息。首先,你需要在Discord开发者应用页面创建一个新的应用并添加bot。接着,设置OAuth2 URL生成器,邀请bot加入你的server。然后,使用Python的discord库和discord_slash库编写代码,定义一个名为'invites'的slash命令,接收用户参数并发送信息。最后,运行bot以启用此功能。
2737

被折叠的 条评论
为什么被折叠?



