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

v1.0.2

Published

A lightweight and easy to use message tracking framework for discord bots, uses MongoDB.

Downloads

32

Readme

Discord-Messages

  • A lightweight and easy to use messages framework for discord bots, uses MongoDB.
  • If you need help feel free to join our discord server to talk and help you with your code.
  • If you encounter any of those fell free to open an issue in our github repository.
  • TypeScript supported!

Download & Update

You can download it from npm:

npm i discord-messages

You can update to a newer version to receive updates using npm.

npm update discord-messages

Setting Up

First things first, we include the module into the project.

const Messages = require("discord-messages");

After that, you need to provide a valid mongo database url, and set it. You can do so by:

Messages.setURL("mongodb://..."); // You only need to do this ONCE per process.

Examples

Examples assume that you have setted up the module as presented in 'Setting Up' section. Following examples assume that your Discord.Client is called client.

Following examples assume that your client.on("message", message is called message.

Following example contains isolated code which you need to integrate in your own command handler.

Following example assumes that you are able to write asynchronous code (use await).

  • Adding a message to user's database for each Message sent
client.on("message", async (message) => {
  if (!message.guild) return;
  if (message.author.bot) return;

  const AddMessage = await Messages.appendMessage(message.author.id, message.guild.id, 1);
});
  • Rank Command
const target = message.mentions.users.first() || message.author; // Grab the target.

const user = await Messages.fetch(target.id, message.guild.id); // Selects the target from the database.

if (!user) return message.channel.send("Seems like this user does not any messages so far..."); // If there isnt such user in the database, we send a message in general.

message.channel.send(`> **${target.tag}** currently has ${user.data.messages} message(s).`); // We show the message(s) count.
  • Leaderboard Command
const rawLeaderboard = await Messages.fetchLeaderboard(message.guild.id, 10); // We grab top 10 users with most message(s) in the current server.

if (rawLeaderboard.length < 1) return reply("Nobody's in leaderboard yet.");

const leaderboard = await Messages.computeLeaderboard(client, rawLeaderboard, true); // We process the leaderboard.

const lb = leaderboard.map(e => `${e.position}. ${e.username}#${e.discriminator}\nMessages Count: ${e.messages}`); // We map the outputs.

message.channel.send(`**Leaderboard**:\n\n${lb.join("\n\n")}`);

Is time for you to get creative..

Methods

createUser

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

Messages.createUser(<UserID - String>, <GuildID - String>);
  • Output:
Promise<Object>

deleteUser

If the entry exists, it deletes it from database.

Messages.deleteUser(<UserID - String>, <GuildID - String>);
  • Output:
Promise<Object>

appendMessage

It adds a specified amount of messages to the current amount of messages for that user, in that guild. It creates a new user with that amount of messages, if there is no entry for that user.

Messages.appendMessage(<UserID - String>, <GuildID - String>, <Amount - Integer>);
  • Output:
Promise<Boolean>

setMessage

It sets the messages to a specified amount and re-calculates the level.

Messages.setMessages(<UserID - String>, <GuildID - String>, <Amount - Integer>);
  • Output:
Promise<Boolean/Object>

fetch

Retrives selected entry from the database, if it exists.

Messages.fetch(<UserID - String>, <GuildID - String>, <FetchPosition - Boolean>);
  • Output:
Promise<Object>

subtractMessages

It removes a specified amount of messages to the current amount of messages for that user, in that guild.

Messages.subtractMessages(<UserID - String>, <GuildID - String>, <Amount - Integer>);
  • Output:
Promise<Boolean/Object>

resetGuild

It deletes the entire guild's data.

Messages.resetGuild(<GuildID - String>);
  • Output:
Promise<Boolean/Object>

fetchLeaderboard

It gets a specified amount of entries from the database, ordered from higgest to lowest within the specified limit of entries.

Messages.fetchLeaderboard(<GuildID - String>, <Limit - Integer>);
  • Output:
Promise<Array [Objects]>

computeLeaderboard

It returns a new array of object that include messages, guild id, user id, leaderboard position, username and discriminator.

Messages.computeLeaderboard(<Client - Discord.js Client>, <Leaderboard - fetchLeaderboard output>, <fetchUsers - boolean, disabled by default>);
  • Output:
Promise<Array [Objects]>

Enjoying Discord-Messages?

Consider donating: https://www.buymeacoffee.com/lebyydev Thanks 😊

Credits

  • Discord-XP Discord-Messages wouldn't exist without them! Consider to check out Discord-XP package aswell and star the repo!