【问题标题】:I want to add PermLvl to my index.js -- DISCORD.JS我想将 PermLvl 添加到我的 index.js -- DISCORD.JS
【发布时间】:2020-12-03 22:58:03
【问题描述】:

所以,最近我开始制作自己的机器人,我也不是最有经验的人,但我知道我在做什么。我脱离了 Discord.js 指南中提供的基本 Discord.js 索引,只是在其中添加了我自己的代码,而没有实际接触命令处理程序。

对于某些命令,我​​希望有一个仅具有该 perm 级别的 perm 级别

permlvl 1 = 管理消息 ^ 不是实际代码只是一个例子,这样只有具有该perm级别的用户才能使用它。

这是我的 index.js 目前。:

    const Discord = require('discord.js');
const config = require('./commands/config.json');
const fs = require('fs');
const { dir } = require('console');

const client = new Discord.Client();
client.commands = new Discord.Collection();




const Fun = fs.readdirSync(`./commands/Fun/`).filter(file => file.endsWith('.js'));
for (const file of Fun) {
    const command = require(`./commands/Fun/${file}`);
    client.commands.set(command.name, command);
}


const General = fs.readdirSync(`./commands/General/`).filter(file => file.endsWith('.js'));
for (const file of General) {
    const command = require(`./commands/General/${file}`);
    client.commands.set(command.name, command);
}


const Information = fs.readdirSync(`./commands/Information/`).filter(file => file.endsWith('.js'));
for (const file of Information) {
    const command = require(`./commands/Information/${file}`);
    client.commands.set(command.name, command);
}

client.elevation = message => {
    if (message.channel.type === 'dm') return;
    let permlvl = 0;
    if (message.member.hasPermission("MANAGE_MESSAGES")) permlvl = 1;
    if (message.member.hasPermission("BAN_MEMBERS")) permlvl = 2;
    if (message.member.hasPermission("MANAGE_GUILD")) permlvl = 3;
    if (message.member.id === message.guild.ownerID) permlvl = 4;
    if (message.author.id === config.devID) permlvl = 5;
    return permlvl;
  };


client.on("ready", () => {
    console.log(`${client.user.tag} has started, with ${client.users.cache.size} users, in ${client.channels.cache.size} channels of ${client.guilds.cache.size} guilds.`); 
    client.user.setActivity(`Serving ${client.users.cache.size} users in ${client.guilds.cache.size} server.`, { url: 'https://www.twitch.tv/discordsstream', type: 'STREAMING' });
)};

client.on('message', message => {
    if (!message.content.startsWith(config.prefix) || message.author.bot) return;

    const args = message.content.slice(config.prefix.length).trim().split(/ +/);
    const commandName = args.shift().toLowerCase();


        const command = client.commands.get(commandName)
        || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

    if (!command) return;


    if (command.guildOnly && message.channel.type === 'dm') {
        return message.reply('I can\'t execute that command inside DMs!');
    }


    if (command.args && !args.length) {
        let reply = `You didn't provide any arguments, ${message.author}!`;

        if (command.usage) {
            reply += `\nThe proper usage would be: \`${config.prefix}${command.name} ${command.usage}\``;
        }

        return message.channel.send(reply);
    }

try {
    command.execute(message, args);
} catch (error) {
    console.error(error);
    message.reply('there was an error trying to execute that command!');
}
});

除了我的其他 client.on 和 client.on 中的一些主要代码和底部的东西。这与索引中的命令处理程序有关,这是实际命令中的配置:

module.exports = {
name: 'ping',
description: 'See the bots response time!',
usage: '',
guildOnly: false,
permLevel: 0,
aliases: ['responsetime', 'pong'],
execute(message, args, async) {
}

问题是,它在运行命令时出现 0 个错误,但是 permlvl 不起作用,

我尝试将“say”命令添加为仅 permlvl 1,即

        if (message.member.hasPermission("MANAGE_MESSAGES")) permlvl = 1;

但会员仍然可以使用它。 -- 他们也没有角色的权限。

我在配置中尝试了一些东西,我尝试用 AwesomeCommandHandler 替换它,在该代码中它不起作用,所以我恢复了。我搜索了一些 discord.js 网站,还有一些 Stack Questions,但我在哪里找不到答案,如果有人可以帮助我,无论是找到答案还是给出答案,都很好。

【问题讨论】:

    标签: node.js discord bots discord.js


    【解决方案1】:

    您还需要检查用户permLevel是否高于或等于命令一:

        const Discord = require('discord.js');
    const config = require('./commands/config.json');
    const fs = require('fs');
    const { dir } = require('console');
    
    const client = new Discord.Client();
    client.commands = new Discord.Collection();
    
    
    
    
    const Fun = fs.readdirSync(`./commands/Fun/`).filter(file => file.endsWith('.js'));
    for (const file of Fun) {
        const command = require(`./commands/Fun/${file}`);
        client.commands.set(command.name, command);
    }
    
    
    const General = fs.readdirSync(`./commands/General/`).filter(file => file.endsWith('.js'));
    for (const file of General) {
        const command = require(`./commands/General/${file}`);
        client.commands.set(command.name, command);
    }
    
    
    const Information = fs.readdirSync(`./commands/Information/`).filter(file => file.endsWith('.js'));
    for (const file of Information) {
        const command = require(`./commands/Information/${file}`);
        client.commands.set(command.name, command);
    }
    
    client.elevation = message => {
        if (message.channel.type === 'dm') return;
        let permlvl = 0;
        if (message.member.hasPermission("MANAGE_MESSAGES")) permlvl = 1;
        if (message.member.hasPermission("BAN_MEMBERS")) permlvl = 2;
        if (message.member.hasPermission("MANAGE_GUILD")) permlvl = 3;
        if (message.member.id === message.guild.ownerID) permlvl = 4;
        if (message.author.id === config.devID) permlvl = 5;
        return permlvl;
      };
    
    
    client.on("ready", () => {
        console.log(`${client.user.tag} has started, with ${client.users.cache.size} users, in ${client.channels.cache.size} channels of ${client.guilds.cache.size} guilds.`); 
        client.user.setActivity(`Serving ${client.users.cache.size} users in ${client.guilds.cache.size} server.`, { url: 'https://www.twitch.tv/discordsstream', type: 'STREAMING' });
    )};
    
    client.on('message', message => {
        if (!message.content.startsWith(config.prefix) || message.author.bot) return;
    
        const args = message.content.slice(config.prefix.length).trim().split(/ +/);
        const commandName = args.shift().toLowerCase();
    
    
            const command = client.commands.get(commandName)
            || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
    
        if (!command) return;
    
    
        if (command.guildOnly && message.channel.type === 'dm') {
            return message.reply('I can\'t execute that command inside DMs!');
        }
    
    
        if (command.args && !args.length) {
            let reply = `You didn't provide any arguments, ${message.author}!`;
    
            if (command.usage) {
                reply += `\nThe proper usage would be: \`${config.prefix}${command.name} ${command.usage}\``;
            }
    
            return message.channel.send(reply);
        }
        
        const userPermLevel = client.elevation(message);
        const commandPermLevel = command.permLevel;
        if(commandPermLevel > userPermLevel){
            return message.channel.send('You do not have required permissions');
        }
    
    try {
        command.execute(message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      相关资源
      最近更新 更多