从Slash命令提取数字并拼接至指定链接的实现需求
实现/GetProfile Slash命令的方案
1. 配置带数字输入选项的Slash命令
在命令配置中添加一个必填的数字类型参数,让用户触发命令后可通过输入框填写目标ID(效果类似你提到的diet输入选项:点击命令后弹出输入框供填写数字)。
以Discord.js为例,命令配置代码示例:
const { SlashCommandBuilder } = require('discord.js'); const getProfileCommand = new SlashCommandBuilder() .setName('getprofile') .setDescription('获取指定ID的用户资料链接') .addIntegerOption(option => option.setName('userid') .setDescription('用户ID数字') .setRequired(true)); // 设置为必填,确保用户必须输入数字
2. 处理命令交互并返回拼接后的链接
用户触发命令并输入数字后,提取该参数拼接至目标链接末尾,返回格式化的可点击链接。
处理逻辑代码示例:
client.on('interactionCreate', async interaction => { if (!interaction.isChatInputCommand()) return; if (interaction.commandName === 'getprofile') { const userId = interaction.options.getInteger('userid'); const profileUrl = `http://cubestoria.ezyro.com/User/?id=${userId}`; // 返回带指定属性的可点击链接 await interaction.reply({ content: `<a href="${profileUrl}" rel="nofollow noreferrer">${profileUrl}</a>`, allowedMentions: { parse: [] } }); } });
关键说明
- 数字参数设为必填,避免无输入导致的错误
- 返回链接使用HTML格式的
<a>标签,确保包含rel="nofollow noreferrer"属性 - 输入框交互由机器人框架自动处理,只需配置参数类型即可实现类似示例的输入选项效果
内容的提问来源于stack exchange,提问作者user19668663
相关产品推荐
相关产品推荐

