ICode9

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

Bot in Discord with discord.js (5)

2022-07-12 00:33:15  阅读:244  来源: 互联网

标签:discord Bot js --- Boolean 隐私 打码 null 交互


Bot in Discord with discord.js (5)

Chapter 6 - 交互:对斜杠命令回复的那些事儿(2)

本章是 Chapter 5 的续集。

抓取响应 Fetching responses

除了对斜杠命令进行回复外,我们还可以抓取这个回复,比如用于给这个回复添加表情回复(reactions)。

靠这个:CommandInteraction#fetchReply()

来个例子,commands/pingFetch.js

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

module.exports = {
	data: new SlashCommandBuilder()
		.setName('ping7')
		.setDescription('Replies with Pong, and fetch!'),
	async execute(interaction) {
	    await interaction.reply("Pong!");
		const message = await interaction.fetchReply();
		console.log(message); // 控制台输出抓取的消息对象本身
	},
};

控制台输出:

<ref *1> Message {
  channelId: '---隐私打码---',
  guildId: '---隐私打码---',
  id: '---隐私打码---',
  createdTimestamp: 1657542153021,
  type: 'APPLICATION_COMMAND',
  system: false,
  content: 'Pong!',
  author: ClientUser {
    id: '---隐私打码---',
    bot: true,
    system: false,
    flags: UserFlags { bitfield: 0 },
    username: 'BlogTest',
    discriminator: '---隐私打码---',
    avatar: null,
    banner: undefined,
    accentColor: undefined,
    verified: true,
    mfaEnabled: false
  },
  pinned: false,
  tts: false,
  nonce: null,
  embeds: [],
  components: [],
  attachments: Collection(0) [Map] {},
  stickers: Collection(0) [Map] {},
  editedTimestamp: null,
  reactions: ReactionManager { message: [Circular *1] },
  mentions: MessageMentions {
    everyone: false,
    users: Collection(0) [Map] {},
    roles: Collection(0) [Map] {},
    _members: null,
    _channels: null,
    crosspostedChannels: Collection(0) [Map] {},
    repliedUser: null
  },
  webhookId: '---隐私打码---',
  groupActivityApplication: null,
  applicationId: '---隐私打码---',
  activity: null,
  flags: MessageFlags { bitfield: 0 },
  reference: null,
  interaction: {
    id: '---隐私打码---',
    type: 'APPLICATION_COMMAND',
    commandName: 'ping7',
    user: User {
      id: '---隐私打码---',
      bot: false,
      system: false,
      flags: [UserFlags],
      username: '---隐私打码---',
      discriminator: '---隐私打码---',
      avatar: '---隐私打码---',
      banner: undefined,
      accentColor: undefined
    }
  }
}

这里返回的 json 信息很丰富,从命令发起者到机器人自己的信息,都包括了。

删除响应 Deleting responses

靠这个 CommandInteraction#deleteReply()

注意:你不能删除一个“短暂回复”(Ephemeral response)。

比如:commands/pingDelete.js

const { SlashCommandBuilder } = require('@discordjs/builders');
const wait = require('node:timers/promises').setTimeout;

module.exports = {
	data: new SlashCommandBuilder()
		.setName('ping')
		.setDescription('Replies with Pong!'),
	async execute(interaction) {
		await interaction.reply("Pong!");
		await wait(1000);
	    await interaction.deleteReply();
	},
};

它会在回复 Pong! 1 秒后,删除这条回复。

补充:snowflake 类型

snowflake 类型源自推特(Twitter)公司。它们是 64 位无符号整数,具有全局唯一性,基于时间生成,而不是按顺序生成。

在 Javascript 中,整数最大只有 53 位,因此我们一般选用字符串来存储 snowflake 值。

在 Discord 中,假设我们有一个 snowflake 值 ‘266241948824764416’,它会被这样解读:

64                                          22     17     12          0
 000000111011000111100001101001000101000000  00001  00000  000000000000
 number of milliseconds since Discord epoch  worker  pid    increment

63 代表最高位,0代表最低位。

项目 含义 位的个数 说明 提取方法
Timestamp 时间戳,单位毫秒 63 to 22 42 bits 自 Discord Epoch 以来的毫秒数,即 2015 年的第一毫秒(即 UNIX 时间戳 1420070400000 所对应的那一毫秒) (snowflake >> 22) + 1420070400000
Internal worker ID Discord 集群编号 21 to 17 5 bits 无需理会 (snowflake & 0x3E0000) >> 17
Internal process ID 集群内部进程编号 16 to 12 5 bits 无需理会 (snowflake & 0x1F000) >> 12
Increment 这 1 毫秒内,条目的增量 11 to 0 12 bits 对于在该进程上生成的每个 ID,此数字都会递增 snowflake & 0xFFF

CommandInteraction 类

来自文档 https://discord.js.org/#/docs/discord.js/main/class/CommandInteraction

类方法

我们在上面谈到的许多方法,都是来自于 CommandInteraction 类,这个类拓展自 BaseInteraction 类。

我们已经见过了 .deferReply().deleteReply().editReply().fetchReply().followUp().reply(),不再赘述。

下面来看 CommandInteraction 类自己非继承来的方法:

方法名 有参数吗 返回值类型 说明
.awaitModalSubmit() Y Promise < ModalSubmitInteraction > 收集通过过滤器的单个模态提交交互。 如果时间到期,Promise 将拒绝
.deferReply() Y Promise < (Message or InteractionResponse) > 推迟对此交互的回复
.deleteReply() N Promise < void > 删除对此交互的初始回复
.editReply() Y Promise < Message > 编辑对此交互的初始回复
.fetchReply() N Promise < Message > 获取对此交互的初始回复
.followUp() Y Promise < Message > 向此交互发送后续消息
.reply() Y Promise < (Message or InteractionResponse) > 创建对此交互的回复
.showModel() Y void 显示模态组件

以下方法来自父类 BaseInteraction

方法名 有参数吗 返回值类型 说明
.inCachedGuild() N Boolean 指示此交互是否同时被缓存并从服务器接收
.inGuild() N Boolean 指示此交互是否来自服务器
.inRawGuild() N Boolean 指示此交互是否来自未缓存的服务器
.isButton() N Boolean 指示此交互是否为 ButtonInteraction
.isChatInputCommand() N Boolean 指示此交互是否为 ChatInputCommandInteraction
.isContextMenuCommand() N Boolean 指示此交互是否为 ContextMenuCommandInteraction
.isMessageContextMenuCommand() N Boolean 指示此交互是否为 MessageContextMenuCommandInteraction
.isRepliable() N Boolean 指示是否可以回复此交互
.isSelectMenu() N Boolean 指示此交互是否为 SelectMenuInteraction
.isUserContextMenuCommand() N Boolean 指示此交互是否为 UserContextMenuCommandInteraction

类属性

属性名 类型 描述
.applicationId Snowflake 应用程序的 ID
.appPermissions < PermissionsBitField > 应用程序或机器人在发送交互的频道内拥有的权限集
.channel TextBasedChannels 发送此交互的频道
.channelId Snowflake 发送此交互的频道的 ID
.client Client 实例化这个交互的客户端
.createdAt Date 创建交互的时间
.createdTimestamp number 创建交互的时间戳
.guild Guild 发送此交互的服务器
.guildId Snowfalke 发送此交互的服务器 ID
.guildLocale Locale 发送此交互的服务器的首选语言环境
.id Snowflake 交互的 ID
.locale Locale 调用此交互的用户的语言环境
.member GuildMember or APIGuildMember 如果此交互是在公会中发送的,发送它的成员
.memberPermissions PermissionsBitField 执行此交互的通道中成员的权限(如果存在)
.token string 交互的令牌
.type InteractionType 交互的类型
.user User 发送此交互的用户
.version number 版本

很多我们都用不上,当然很多能节约我们的敲代码的时间。要学会在不断地尝试中学习,学会在错误中吸取经验。

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

标签:discord,Bot,js,---,Boolean,隐私,打码,null,交互
来源: https://www.cnblogs.com/hhzm/p/16468524.html

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

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

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

ICode9版权所有