Home > Net >  DiscordJS MongoDB Custom Prefix
DiscordJS MongoDB Custom Prefix

Time:05-05

I am trying to create a custom prefix command with MongoDB but I do not know how to use it, I have attempted to do it with the code below but it just returns ReferenceError: prefix is not defined

Code:

const db = require('mongoose')

module.exports = async (client, message) => {
    let px = db.fetch(message.guild.id, prefix)
    if (px === null){
        px = '.'
    }
    if (message.author.bot) return;
    if (!message.content.toLowerCase().startsWith(px)) return;
    if (!message.member) message.member = await message.guild.members.fetch(message.member.id);
    if (!message.guild) return;
    const args = message.content.slice(px.length).trim().split(/  /g);
    const cmd = args.shift().toLowerCase();
    if (cmd.length === 0) return;
    const command = client.commands.get(cmd) || client.commands.find((x) => x.aliases && x.aliases.includes(cmd));
    if (command) {
        if (command.timeout) {
            if (Timeout.has(`${message.author.id}${command.name}`)) {
                const embed = new MessageEmbed()
                    .setTitle('You are in timeout!')
                    .setColor(config.colour)
                    .setDescription(`:x: You need to wait **${humanizeDuration(command.timeout, { round: true })}** to use command again`)
                    .setColor('#ff0000')
                return message.channel.send({ embeds: [embed] })
            } else {
                command.run(client, message, args);
                Timeout.add(`${message.author.id}${command.name}`)
                setTimeout(() => {
                    Timeout.delete(`${message.author.id}${command.name}`)
                }, command.timeout);
            }
        } else {
            command.run(client, message, args)
        }
    }
}

Heres a screenshot of what the Database looks like.

CodePudding user response:

you need to initialize the prefix from the server first.

const server = await db.servers.fetch({_id: serverId})
const prefix = server.prefix

CodePudding user response:

you need to define the prefix and initialize it.

let prefix;
let data = await db.findOne({
    guildID: message.guild.id
})

if(data === null) {
    prefix = "."
} else {
    prefix = data.Prefix
}
  • Related