ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Bot in Discord with discord.js (7)

2022-07-13 02:00:28  阅读:202  来源: 互联网

标签:interaction name discord required Bot js user server theUser


Bot in Discord with discord.js (7)

Chapter 8 - 子命令 Subcommands

如果你有一个包含子命令的命令,你可以以使用与解析 Options 和 Choices 的值那样相似的方式解析它们。 以下代码片段详细说明了解析子命令并使用 CommandInteractionOptionResolver#getSubcommand() 方法做出相应响应所需的逻辑:

代码 commands/subCmd.js

const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
	data: new SlashCommandBuilder()
		.setName('subcmd')
		.setDescription('Subcommand! Reply with user\'s or server \'s info.')
		.addSubcommand(subcommand => 
			subcommand.setName('user')
			.setDescription('Info about a user')
			.addUserOption(option => option.setName('target').setDescription('The user')))
		.addSubcommand(subcommand =>
			subcommand.setName('server')
			.setDescription('Info about the server')),

	async execute(interaction) {
		if (interaction.options.getSubcommand() === 'user') {
			const theUser = interaction.options.getUser('target');

			if (theUser) {
				await interaction.reply(`Username: ${theUser.username}\nID: ${theUser.id}`);
			} else {
				// 用户没有指定 target user,我们输出命令发起者自己就好
				await interaction.reply(`Your username: ${interaction.user.username}\nYour ID: ${interaction.user.id}`);
			}
		} else if (interaction.options.getSubcommand() === 'server') {
			await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);
		}
	},
};

注意,只有子命令才是真正可以执行的命令,比如上面的代码中 /subcmd 没有真正的函数逻辑实现来支撑这个命令,只有子命令 /subcmd server 和 /subcmd user 才是真正有函数实现的命令。直接执行 /subcmd 指望它能干事是不可能的。

还是先上效果图,再分析代码:

  • 输入命令 /subcmd 时,提示了两个子命令:
    image

  • 执行 /subcmd server
    image

  • 执行 /subcmd user @BlogTest:(注意你需要从提示栏中回车选定 target 用户,可不能直接输入 "@ + 用户名" 就完活了,因为可能存在重名用户)
    image

  • 执行 /subcmd user
    image

分析代码:

下面的节选代码,它声明了一个叫 user 的子命令,它的输入参数是一个 Option,一个 User 类型的 Option。这个 Option 叫 target。

...

		.addSubcommand(subcommand => 
			subcommand.setName('user')
			.setDescription('Info about a user')
			.addUserOption(option => option.setName('target').setDescription('The user')))

...

下面的节选代码,它声明了一个叫 server 的子命令,没有参数。

...

		.addSubcommand(subcommand =>
			subcommand.setName('server')
			.setDescription('Info about the server')),

...

下面的代码,首先上来,判断子命令是 user 还是 server。

  • 如果子命令是 user,则通过 interaction.options.getUser(target) 获取 target 字段的值,并赋值给 theUser 变量。
    • 如果 theUser 为 null,则用户没有给定值,则会回复用户的信息(用户名和用户的 Snowflake ID)。
    • 如果 theUser 非 null,则输出这个用户的信息(用户名和用户的 Snowflake ID)。注意这里的 user 将是一个有效 Discord 用户,不会是无效的 Discord 用户作为输入。
  • 如果子命令是 server,则回复服务器的信息(服务器名称和服务器人数)。
...

	async execute(interaction) {
		if (interaction.options.getSubcommand() === 'user') {
			const theUser = interaction.options.getUser('target');

			if (theUser) {
				await interaction.reply(`Username: ${theUser.username}\nID: ${theUser.id}`);
			} else {
				// 用户没有指定 target user,我们输出命令发起者自己就好
				await interaction.reply(`Your username: ${interaction.user.username}\nYour ID: ${interaction.user.id}`);
			}
		} else if (interaction.options.getSubcommand() === 'server') {
			await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);
		}
	},

...

CommandInteractionOptionResolver 类

命令交互选项的解析器。

方法(Methods):

方法名 第一个参数 第二个参数 返回值类型 说明
.get() name [required] CommandInteractionOption 按名称获取选项
.getAttachment() name [required] Attachment 获取附件选项
.getBoolean() name [required] boolean 获取一个布尔选项
.getChannel() name [required] GuildChannelThreadChannelAPIChannel 获取频道选项
.getFocused() [getFull] string 或 AutocompleteFocusedOption 获取关注点选项
.getInteger() name [required] number 获取整数选项
.getMember() name GuildMemberAPIGuildMember 获取服务器成员选项
.getMentionable() name [required] UserGuildMemberAPIGuildMember 或 RoleAPIRole 获得一个服务器成员或身份组的选项
.getMessage() name [required] Message 获取消息选项
.getNumber() name [required] number 获取数字选项
.getRole() name [required] RoleAPIRole 获取身份组选项
.getString() name [required] string 获取字符串选项
.getSubcommand() [required] string 获取选定的子命令
.getSubcommandGroup() [required] string 获取选定的子命令组
.getUser() name [required] User 获取用户选项

属性(Properties):

属性名 类型 描述
.client Client 实例化这个的 client
.data Array< CommandInteractionOption > 交互的 options 数组
.resolved < CommandInteractionResolvedData > 交互解析数据

原文链接:https://www.cnblogs.com/hhzm/p/16472398.html
转载需注明出处。

标签:interaction,name,discord,required,Bot,js,user,server,theUser
来源: https://www.cnblogs.com/hhzm/p/16472398.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有