ApplicationCommandsを使用できる役職を制限するサンプル
ちなみに制限はユーザー単位でも可能
動作のスクリーンショット等
コマンドを使用できる場合の表示
https://gyazo.com/353b0a6169b45cb9151d1d18a47895d5
コマンドを使用できない場合の表示
https://gyazo.com/334554cb40e19219aa2768dbca3a658d
動作例
https://gyazo.com/543f7eb5df9538a93c459be08d3faafd
code:index.js
const { Client, Interaction, CommandInteraction, InteractionWebhook } = require("discord.js");
const { promises: fs } = require("fs");
const path = require("path");
const client = new Client({
intents: 0
});
const GUILD_ID = "750031320205230311";
let rebooting = false;
const pathToBootConfig = path.resolve(__dirname, "./reboot-config.json");
const bootConfig = fs.readFile(pathToBootConfig, "utf-8").then(jsonStr => JSON.parse(jsonStr), () => ({})).finally(() => {
fs.rm(pathToBootConfig, {
force: true
}).catch(err => console.error(err));
});
/**
*
* @param {import("discord.js").Snowflake} guildId
* @param {import("discord.js").Snowflake} userId
* @returns
*/
async function isManager(guildId, userId) {
const guild = await client.guilds.fetch({
guild: guildId,
force: true
});
const member = await guild.members.fetch({
user: userId,
force: true
});
return ROLE_IDS.some(roleId => !!member.roles.resolve(roleId));
}
/**
*
* @param {CommandInteraction} interaction
*/
async function rebootCommand(interaction) {
if (!await isManager(interaction.guildId, interaction.user.id)) {
await interaction.defer();
await setupCommandsInGuilds(GUILDS);
await interaction.followUp("forbidden");
return;
}
if (rebooting) {
await interaction.reply("already rebooting");
return;
}
try {
rebooting = true;
await interaction.defer();
await fs.writeFile(pathToBootConfig, JSON.stringify({
reboot_webhook: {
id: interaction.webhook.id,
token: interaction.webhook.token
}
}), "utf-8");
} catch (err) {
rebooting = false;
throw err;
}
process.exit();
}
/**
*
* @param {CommandInteraction} interaction
*/
async function onCommandInteraction(interaction) {
switch (interaction.commandName) {
case "reboot":
await rebootCommand(interaction);
return;
default:
throw Error();
}
}
/**
*
* @param {Interaction} interaction
*/
async function onInteraction(interaction) {
if (interaction.isCommand()) {
await onCommandInteraction(interaction);
}
}
/**
*
* @param {import("discord.js").Snowflake} guildId
* @param {import("discord.js").Snowflake[]} managerRoleIds
*/
async function setupCommands(guildId, managerRoleIds) {
if (!guildId) {
throw Error();
}
const commands = await client.application.commands.set([
{
name: "reboot",
description: "reboot this bot",
defaultPermission: false,
}
], guildId);
const permissions = managerRoleIds.map(managerRoleId => {
return { id: managerRoleId, type: "ROLE", permission: true };
});
return client.application.commands.permissions.set({
command: command.id,
guild: guildId,
permissions,
});
}));
}
client.once("ready", () => {
client.on("interactionCreate", (interaction) => {
onInteraction(interaction).catch(err => console.error(err));
});
console.log(Logged in as: ${client.user.tag});
bootConfig.then(({ reboot_webhook }) => {
if (!reboot_webhook) {
return;
}
const webhook = new InteractionWebhook(client, reboot_webhook.id, reboot_webhook.token);
return webhook.send({
content: "rebooted"
});
}).catch(err => console.error(err));
setupCommands(GUILD_ID, ROLE_IDS).catch(err => console.error(err));
});
client.login();
code:sh
pm2 start index.js --name bot
環境
discord.js 13.0.0-dev.07017a9.1626869177
pm2 5.1.0