📜  discord.js 按钮 - Javascript (1)

📅  最后修改于: 2023-12-03 15:14:41.941000             🧑  作者: Mango

Discord.js 按钮

简介

Discord.js 是一个用于创建 Discord 机器人的 Node.js 库。它允许你轻松地控制你的 Discord 服务器和频道,并从你的应用中自动化操作和扩展聊天体验。其中有一个很重要的概念是按钮,可以实现更交互的控件。

安装

使用 npm 进行安装:

npm install discord.js
使用
创建按钮
const { MessageButton } = require('discord.js');

const button = new MessageButton()
  .setLabel('点击我')
  .setStyle('PRIMARY')
  .setCustomId('button_click');
将按钮添加到消息中
const { MessageActionRow } = require('discord.js');

const row = new MessageActionRow().addComponents(button);

message.channel.send({ content: '请点击按钮', components: [row] });
监听按钮点击
client.on('interactionCreate', async (interaction) => {
  if (!interaction.isButton()) return;

  await interaction.reply({ content: '按钮被点击', ephemeral: true });
});

其中 ephemeral 表示这条消息只能被互动者和机器人所看到。

其他

Discord.js 不仅可以创建按钮,还有其他很多功能。参见 官方文档 了解更多信息。

# Discord.js 按钮

## 简介

Discord.js 是一个用于创建 Discord 机器人的 Node.js 库。它允许你轻松地控制你的 Discord 服务器和频道,并从你的应用中自动化操作和扩展聊天体验。其中有一个很重要的概念是按钮,可以实现更交互的控件。

## 安装

使用 npm 进行安装:

\`\`\`javascript
npm install discord.js
\`\`\`

## 使用

### 创建按钮

\`\`\`javascript
const { MessageButton } = require('discord.js');

const button = new MessageButton()
  .setLabel('点击我')
  .setStyle('PRIMARY')
  .setCustomId('button_click');
\`\`\`

### 将按钮添加到消息中

\`\`\`javascript
const { MessageActionRow } = require('discord.js');

const row = new MessageActionRow().addComponents(button);

message.channel.send({ content: '请点击按钮', components: [row] });
\`\`\`

### 监听按钮点击

\`\`\`javascript
client.on('interactionCreate', async (interaction) => {
  if (!interaction.isButton()) return;

  await interaction.reply({ content: '按钮被点击', ephemeral: true });
});
\`\`\`

其中 \`ephemeral\` 表示这条消息只能被互动者和机器人所看到。

## 其他

Discord.js 不仅可以创建按钮,还有其他很多功能。参见 [官方文档](https://discord.js.org/) 了解更多信息。