Search code examples
javascriptnode.jsdiscord.jsquick.db

My command is not setting my role id when I run it, it never SETS it?


I'm trying to make a mute command, but when trying to set the ROLE ID for my database, it won't set it, does anybody know how to fix this?

What I am using: discord.js, quick.db

Different files are between THREE line of comments, and a large amount of spaces.

// 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("TOKEN");

When I run the setmute command(of course, mentioning a role), then I run my printmute it says "You never set a role.", which is said when the guild doesn't have a role set.


Solution

  • Quick.db data is stored in a file named json.sqlite. When you use require("quick.db"); and you use set(), get() or any QuickDB's method, it will create a json.sqlite file in the current folder (or load it if it's already created). I think

    You need to require QuickDB one time in your index.js file and to add a db property to one of your variables like client. Then you will be able to use client.db.set() and client.db.get() without use require each time you want to use quick.db.

    Or, you can also use my fork of QuickDB (https://github.com/Androz2091/quick.db) which has a db.init("./some_file.sqlite") function to choose a specific file.