npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

discord-mongodb-prefix

v1.1.4

Published

A lightweight and easy to use db managing package to save prefixes

Downloads

32

Readme

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..