你可以让该命令只能由具有管理员角色的人执行吗?

问题描述 投票:0回答:1

我找到了这段代码,但是每个人都可以执行该命令。 我想让它只有具有管理员角色的人才能访问它,但我不知道,因为我刚刚开始编码。

const Discord = require('discord.js');
const client = new Discord.Client({ 
    intents: [
        Discord.GatewayIntentBits.Guilds, 
        Discord.GatewayIntentBits.GuildMessages 
    ] 
});

// Import the token from the config.json file
const { token } = require('./config.json');

client.on('ready', () => {
    console.log(`Bot is ready as: ${client.user.tag}`);
});

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const { commandName } = interaction;

        if (commandName === 'status') {
            if (!interaction.options.getChannel('channel')) {
                return await interaction.reply('You must select a channel!');
            }

            const channel = interaction.options.getChannel('channel');
            const status = interaction.options.getString('status');

            if (status === 'open') {
                channel.setName("🟢︱𝙊𝙥𝙚𝙣");
                await interaction.reply(`Channel name has been changed to Open!`);
            } else if (status === 'close') {
                channel.setName("🔴︱𝘾𝙡𝙤𝙨𝙚");
                await interaction.reply(`Channel name has been changed to Close!`);
            } else {
                await interaction.reply('Status must be either "open" or "close"!');
            }
        }

        if (commandName === 'plus') {
            const channel = interaction.options.getChannel('channel');
            const increaseBy = interaction.options.getInteger('increaseby');

            // Extract the number from the channel name
            let nameParts = channel.name.split(' ');
            let numberPart = parseInt(nameParts.pop(), 10);

            // Increase the number and update the channel name
            if (!isNaN(numberPart)) {
                numberPart += increaseBy;
                nameParts.push(numberPart.toString());
                await channel.setName(nameParts.join(' '));
                await interaction.reply(`Channel name has been updated to **${nameParts.join(' ')}**.`);
            } else {
                await interaction.reply(`Unable to find a number in the channel name **${channel.name}**.`);
            }
        }

        if (commandName === 'rate') {
            let channel = interaction.options.getChannel('channel');
            let rate = interaction.options.getInteger('rate');
    
            if (!channel || !rate) {
                await interaction.reply({ content: 'You must specify a channel and a rate.', ephemeral: true });
                return;
            }
    
            channel.setName(`🏷┊𝑅𝑎𝑡𝑒 :  ${rate}`)
                .then(updated => console.log(`Channel's new name is ${updated.name}`))
                .catch(console.error);
    
            await interaction.reply(`Channel name has been changed to "rate : ${rate}"`);
        }
    });

// Use the imported token to log in
client.login(token);

我想让那里的所有命令只能由管理员角色访问,请帮忙!

我确实尝试过搜索,并使用了我在互联网上找到的一些代码。但这不起作用,只会让情况变得更糟!

javascript discord
1个回答
0
投票
client.on('interactionCreate', async interaction => {
        if (!interaction.isCommand()) return;
    
        const { commandName } = interaction;
    
        // 检查是否是管理员角色
        const isAdmin = interaction.member.roles.cache.some(role => role.name === '管理员');
        if (!isAdmin) {
            return await interaction.reply('只有管理员才能执行此命令!');
        }
    
        // 下面是你的其他命令处理逻辑,不变
    
        // 'status' 命令的处理逻辑
        if (commandName === 'status') {
            // 你的代码逻辑
        }
    
        // 'plus' 命令的处理逻辑
        if (commandName === 'plus') {
            // 你的代码逻辑
        }
    
        // 'rate' 命令的处理逻辑
        if (commandName === 'rate') {
            // 你的代码逻辑
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.