I'm writing a Discord bot using TypeScript and Discord.js to implement a game.
As part of the game, I would like certain commands to be secret - they should be restricted to one user only, and the user should not have a role to indicate they have access to this command. I cannot find anything in the guide or the docs about how to accomplish this, or if it is possible.
Here is an example slash command, which is visible only to admins. This works, but I do not know how to make it work for only a specific user.
{
data: new SlashCommandBuilder()
.setName("protect")
.setDescription("Protect a user from all visits")
.setDefaultMemberPermissions(0)
.addUserOption(
(option): SlashCommandUserOption =>
option.setName("target").setDescription("The user you wish to protect").setRequired(true),
),
async execute(interaction: ChatInputCommandInteraction) {
const target = interaction.options.getUser("target");
await interaction.reply(`You have selected ${target?.username}!`);
},
}
In Discord.js, you cannot hide it specifically for the user. You can only make it see the command according to permissions.
However, after the command is used, you can detect the user and decide whether to run the command or not.
For example:
command.js
{
data: new SlashCommandBuilder()
.setName(‘protect’)
.setDescription(‘Protect a user from all visits’)
.setDefaultMemberPermissions(0)
.addUserOption(
(option): SlashCommandUserOption =>
option.setName(‘target’).setDescription(‘The user you wish to protect’).setRequired(true),
),
async execute(interaction: ChatInputCommandInteraction) {
const target = interaction.options.getUser(‘target’);
await interaction.reply(`You have selected ${target?.username}!`);
},
whiteList: [‘111111111’] // User Id
}
interactionCreate.js
{
// ...
if(!command.whiteList.includes(interaction.user.id)) return
// ...
}
// return if the user ID is not in the whitelist.