Skip to content

andrerahardjo97/discord-sticky-message-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Discord Sticky Message Bot


Bot preview

License

About

This bot gonna send your stickied message after few message.
This bot is inspired by StickyBot.

Support

I don't offer support about error in installation. I only offer support for bug and error when the code is running.
I will try to keep the code up to date when there's a major release in libraries that this bot use.

Contributing

I only welcome pull requests that fix bug, typo, or broken english.
If you want to add new feature to this bot, just fork this repository.

Requirements

  • Node.js v14 or higher
  • NPM (Included in Node.js installer by default) or Yarn

If you use NPM, delete yarn.lock file.

Getting Started

  1. Clone or download this repository
  2. Go to the folder where you clone or download this repository
  3. Type npm install or yarn install depend on what you use
  4. Rename .env.example to .env and fill that file. Below is the explanation.
DISCORD_TOKEN=(fill your bot token here)
ALLOWED_ROLES_ID=(allowed roles id, just leave it blank if you don't want to use this)
MAX_MESSAGE_COUNT=(how many message before the bot send the message again. Minimum is 5 if you want to comply with Discord ToS)
OWNER=(your user id or someone user id (e.g. server owner))
PREFIX=(command prefix)
  1. Type node index.js to start the bot

If you don't know how to get the bot token, check this guide.
You want to host it? Check this hosting guide.
If you use Heroku, do not commit the .env file and fill the .env like the guide above said.
After you deploy it on Heroku, if it's your first time, don't forget to turn on all the process type. I already include the Procfile file for Heroku in this repository.

Usage

-stick <message that you want to stick>

-unstick

Code Explanation

This is a Gateway Intents.

const client = new Discord.Client({
  ws: {
    intents: [
      "GUILDS",
      "GUILD_MESSAGES",
    ],
  },
});

I define the Gateway Intents because this bot only use message event. There's no use to listen to other event.

Remove the code below (line 23-25) if you gonna use this bot on a bot channel. The code below make the bot not execute the rest of the code if the sender of the message is bot.

if (message.author.bot) {
  return;
}

The code below only run if the message does not have command prefix.

// Check if the message have command prefix or not.
if (message.content.indexOf(process.env.PREFIX) !== 0) {
  // Check if user already stick a message or not.
  if (stickyMessageContent !== "") {
    // Check the channel id, is it same with the channel id where the message is stickied.
    if (message.channel.id === stickyMessageChannel) {
      // Increment message counter.
      messageCount++;
      // Check if it's already hit the maximum message count that is defined.
      if (messageCount === maxMessageCount) {
        // Delete last sticky message.
        await lastStickyMessage.delete();
        // Send new sticky message.
        lastStickyMessage = await message.channel.send(stickyMessageContent);
        // Reset the counter.
        messageCount = 0;
      }
    }
  }

  return;
}

Split the message by space if the message has command prefix.

const args = message.content.slice(1).trim().split(/ +/g);

Get the first element of args, because it's the command name

const command = args.shift().toLowerCase();

Library That I Use

License

GNU GPLv3