我的命令在运行时没有设置我的角色ID,它从未设置吗?

时间:2019-11-05 04:43:49

标签: javascript node.js sqlite discord.js

我正在尝试执行静音命令,但是当尝试为我的数据库设置ROLE ID时,它不会设置它,有人知道如何解决此问题吗?

我正在使用的是:discord.js,quick.db

我已经对这个问题进行了一些研究,我可以找到任何可以帮助您的东西!

不同的文件介于三行注释和大量空格之间。

// Setmute.js
//
//
module.exports = {
    name: 'setmute',
    description: 'Mutes a user.',
    aliases: ['setmuterole', 'muterole'],
    usage: 'mention reason',
    cooldown: 5,
    execute(message, args, user) {
        const db = require('quick.db');
        let member = message.guild.member(message.member);
        if (message.mentions.channels.first()) {
            if (message.member.hasPermission('MANAGE_GUILD')) {
                db.set(`${member.guild.id}-muterole`, message.mentions.roles.first().id);
                console.log(message.mentions.roles.first());
            } else if (!message.member.hasPermission('MANAGE_GUILD')) {
                return message.reply('you must have \"Manage Guild\" permmissions to access this command, sorry!');
            }
        };
    }
}

// printmute.js
//
//














module.exports = {
    name: 'printmute',
    description: 'Mutes a user.',
    aliases: ['printmuterole'],
    usage: 'mention reason',
    cooldown: 5,
    execute(message, args, user) {
        const db = require('quick.db');
        let member = message.guild.member(message.member);
        let role = db.get(`${member.guild.id}-muterole`);

        if (!role) return message.reply("You never set a role.");

        console.log(role);
    }
}
// Index.js
//
//






const fs = require('fs');
const Discord = require('discord.js');

const db = require('quick.db');

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

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles){
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}


const cooldowns = new Discord.Collection();

client.once('ready', () => {
    console.log('Ready!');
});




client.on('message', message => {



    let member = message.guild.member(message.member);

    let prefix = db.get(`${member.guild.id}-prefix`);



    if (!prefix) prefix = '?';




    if (!message.content.startsWith(prefix) || message.author.bot) return;




    const args = message.content.slice(prefix.length).split(/ +/);
    const commandName = args.shift().toLowerCase();
    const user = message.mentions.users.first();

    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 !== 'text') {
        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: \`${prefix}${command.name} ${command.usage}\``;
        }

        return message.channel.send(reply);
    }

    if (!cooldowns.has(command.name)) {
        cooldowns.set(command.name, new Discord.Collection());
    }

    const now = Date.now();
    const timestamps = cooldowns.get(command.name);
    const cooldownAmount = (command.cooldown || 3) * 1000;

    if (timestamps.has(message.author.id)) {
        const expirationTime = timestamps.get(message.author.id) + cooldownAmount;

        if (now < expirationTime) {
            const timeLeft = (expirationTime - now) / 1000;
            return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
        }
    }

    timestamps.set(message.author.id, now);
    setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

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


//client.login(nope, no token here! have a great time, try searching somewhere else!);

当我运行setmute命令(当然,提到一个角色)时,然后我运行printmute时,它会显示“您从未设置过角色。”,即在行会中未设置角色时表示。

1 个答案:

答案 0 :(得分:0)

Quick.db数据存储在名为json.sqlite的文件中。当您使用require("quick.db");并使用set()get()或任何QuickDB的方法时,它将在当前文件夹中创建一个json.sqlite文件(如果已创建,则将其加载) 。我认为

您需要在index.js文件中要求一次QuickDB,并向db之类的变量之一添加client属性。这样,您每次想使用quick.db时都可以使用client.db.set()client.db.get(),而无需使用。

或者,您也可以使用具有db.init("./some_file.sqlite")功能的QuickDB(https://github.com/Androz2091/quick.db)分支来选择特定文件。