You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求基于discord.js 14.14.1的SelectMenuBuilder简易项目示例

基于discord.js 14.14.1的SelectMenuBuilder完整示例项目

以下是适配discord.js 14.14.1版本的完整SelectMenuBuilder示例,包含命令注册、菜单发送及交互响应的完整可运行流程:

项目文件结构

  • index.js(项目主入口)
  • commands/selectStarter.js(菜单命令逻辑)

1. 主入口文件 index.js

const { Client, GatewayIntentBits, Collection, Events } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');

// 初始化客户端,启用必要的意图权限
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent
    ]
});

// 注册命令集合
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);
    if ('data' in command && 'execute' in command) {
        client.commands.set(command.data.name, command);
    } else {
        console.log(`[警告] ${filePath} 中的命令缺少必要的"data"或"execute"属性`);
    }
}

// 处理交互事件
client.on(Events.InteractionCreate, async interaction => {
    // 处理Slash命令
    if (interaction.isChatInputCommand()) {
        const command = client.commands.get(interaction.commandName);
        if (!command) return;
        try {
            await command.execute(interaction);
        } catch (error) {
            console.error(error);
            await interaction.reply({ content: '执行命令时出错!', ephemeral: true });
        }
    } 
    // 处理选择菜单交互
    else if (interaction.isStringSelectMenu()) {
        if (interaction.customId === 'starter') {
            const selected = interaction.values[0];
            let replyContent = '';
            switch(selected) {
                case 'bulbasaur':
                    replyContent = '你选择了妙蛙种子!草/毒属性的种子宝可梦';
                    break;
                case 'charmander':
                    replyContent = '你选择了小火龙!火属性的蜥蜴宝可梦';
                    break;
                case 'squirtle':
                    replyContent = '你选择了杰尼龟!水属性的小海龟宝可梦';
                    break;
            }
            await interaction.reply({ content: replyContent, ephemeral: true });
        }
    }
});

// 登录机器人
client.login('你的机器人令牌');

2. 菜单命令文件 commands/selectStarter.js

const { SlashCommandBuilder, ActionRowBuilder, SelectMenuBuilder, SelectMenuOptionBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('choose-starter')
        .setDescription('选择你的初始宝可梦'),
    async execute(interaction) {
        // 创建菜单选项
        const bulbasaurOption = new SelectMenuOptionBuilder()
            .setLabel('妙蛙种子')
            .setDescription('草/毒属性的种子宝可梦')
            .setValue('bulbasaur');

        const charmanderOption = new SelectMenuOptionBuilder()
            .setLabel('小火龙')
            .setDescription('火属性的蜥蜴宝可梦')
            .setValue('charmander');

        const squirtleOption = new SelectMenuOptionBuilder()
            .setLabel('杰尼龟')
            .setDescription('水属性的小海龟宝可梦')
            .setValue('squirtle');

        // 创建选择菜单
        const selectMenu = new SelectMenuBuilder()
            .setCustomId('starter')
            .setPlaceholder('选择你的初始宝可梦!')
            .addOptions(bulbasaurOption, charmanderOption, squirtleOption);

        // 将菜单放入行动行组件
        const row = new ActionRowBuilder().addComponents(selectMenu);

        // 发送带菜单的消息
        await interaction.reply({
            content: '快来选你的初始宝可梦吧!',
            components: [row]
        });
    }
};

使用步骤

  1. 安装指定版本依赖:npm install discord.js@14.14.1
  2. 替换index.js中的你的机器人令牌为实际Discord机器人令牌
  3. 运行项目:node index.js
  4. 在Discord服务器输入/choose-starter即可调用菜单

内容的提问来源于stack exchange,提问作者Claude

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 22:08:15