discord bot之 slash command(选择user,给出option)

在浏览现有的高票discord bots时候,发现一个很有趣的功能:
在这里插入图片描述

比如上图所示(周董的phantabear nft项目discord群),在输入/invites 后,invite tracker bot允许用户在用户列表里选择一个用户,查看该用户的信息。这个功能就是slash command啦。下面我们一起看一下这个功能要怎么实现吧。

  1. 生成bot

    进入https://discord.com/developers/applications 页面
    -> 点击 New Application
    -> 点击bot,选择add bot
    在这里插入图片描述
    ->点击OAuth2 URLGenerator 选择一下选中选项:
    在这里插入图片描述
    然后该页面拉到底,复制GENERATED URL,将生成的bot邀请到自己的server。

  2. 代码准备

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")

完结撒花🎉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值