1.1.4 • Published 3 years ago

discord-mongodb-prefix v1.1.4

Weekly downloads
13
License
MIT
Repository
github
Last release
3 years ago

Discord-Mongodb-Prefix

A lightweight managing package to save custom prefix in db. Intelligent saving ways to lower traffic up to 90%.

If you need help feel free to join our discord server. We will provide you all help ☺

Download

You can download it from npm:

npm i discord-mongodb-prefix

Setting Up

First we include the module into the project (into your main bot file).

const mongopref = require("discord-mongodb-prefix");

After that, you have to provide a valid mongodb url and set the default prefix.

mongopref.setURL("mongodb://..."); //builts a connection with the db
mongopref.setDefaultPrefix("Your default Prefix") ; // Set here your default prefix

Fetching the Prefix

Following examples assume that your Discord.Client is called client.

client.on("message", async (message) => {
  if (!message.guild) return;
  if (message.author.bot) return;
  
  const fetchprefix = await mongopref.fetch(message.guild.id);
  console.log(fetchprefix.prefix) /// will log out the prefix
.........

The Code below will split the given prefix from the message

if (!message.content.startsWith(fetchprefix.prefix)) return;
const args = message.content.slice(fetchprefix.prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
console.log(command);

Change prefix

if(command === "changeprefix"){ /// you can use your command handler too...
  let newprefix = args[0]; // the provided argument. Ex: !changeprefix <newprefix>
  await mongopref.changeprefix(message.guild.id, newprefix); //saves the new prefix on the map
  message.channel.send(`**Successfully changed prefix  to ${newprefix}**`)
}

Whole code

  
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
///////Add this
const mongopref = require("discord-mongodb-prefix");
mongopref.setURL("mongodb://..."); //builts a connection with the db
mongopref.setDefaultPrefix(prefix); // set your default prefix

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

client.on('message', async message => {
    if (message.author.bot) return;

//add this
  const fetchprefix = await mongopref.fetch(message.guild.id);
  console.log(fetchprefix.prefix)
/// add this

  if (!message.content.startsWith(fetchprefix.prefix)) return;
  const args = message.content.slice(fetchprefix.prefix.length).trim().split(/ +/);
  const command = args.shift().toLowerCase();

  if(command === "changeprefix"){ 
    let newprefix = args[0]; // the provided argument. Ex: !changeprefix <newprefix>
    await mongopref.changeprefix(message.guild.id, newprefix); 
    return message.channel.send(`**Successfully change prefix from "${fetchprefix.prefix}" to "${newprefix}"**`)
  }
  if(command === "prefix"){ /// !prefix <guildid>
    if(!args[0]) return message.channel.send(`This Servers prefix is ` +"`" + fetchprefix.prefix+ "`")
    const otherprefix = await mongopref.fetch(args[0]);
    return message.channel.send(`The Server(${args[0]}) prefix is` + " `" + otherprefix.prefix + " .`")
  }
  if(command === "prefixstats"){
    const all = await mongopref.fetchall();
    const stats = new Discord.MessageEmbed()
    .setTitle("Prefix stats")
    .addField("Prefix saved on Map:" , "```" + mongopref.prefix.size + " prefix saved" + "```")
    .addField("Different Prefix:","```" + Object.keys(all).length + " Servers have a another prefix"+ "```")
    .addField("Servers with default prefix:" ,"```" + Number(client.guilds.cache.size-Object.keys(all).length) + " Servers are not saved in db"+ "```")
    .setColor("YELLOW")
    return message.channel.send(stats)
  } 
});
client.login(token);

Is time for you to use the code creative..

Methods

createServer

Creates an entry in database for that Server if it doesnt exist.

mongopref.createGuild(message.guild.id); /// you can also give a another guild id

deleteServer

If the entry exists, it deletes it from database.

mongopref.deleteGuild(message.guild.id); /// you can also give a another guild id

For Advanced Coders

This code will use the mention prefix of the bot or the custom prefix

  const escapeRegex = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');   //// the bot will react to on mention prefix 
  //// and will check with a regex if the fetched prefix is on the message
	const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(fetchprefix.prefix)})\\s*`);
	if (!prefixRegex.test(message.content) || message.author.bot) return;    
	const [, matchedPrefix] = message.content.match(prefixRegex);  
	
  const args = message.content.slice(matchedPrefix.length).trim().split(/ +/);
  const command = args.shift().toLowerCase();

Have fun and feel free to contribute/suggest or contact me on my discord server or per dm on Meister#9667

Bugs, Glitches and Issues

If you encounter any problems fell free to open an issue in our github repository or join the discord server..

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago