Skip to content

A Discord Bot built using Discord.js, Node.js and Google Cloud to display Overwatch Stats, memes and more fun commands!

Notifications You must be signed in to change notification settings

Sajantoor/Discord-Overwatch-Stats-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Shitty Robot Idea: The Discord Bot

Screenshot of Hello Message

Learning how to develop a discord bot using Node.js and TypeScript!

Name and icons are inspired and from the YouTube channel Michael Reeves

Table of Contents

About

This Discord Bot has many features from being able to get user and server information, to get Overwatch stats or fun things like posting random memes and beeping like a robot 🤖. The purpose of this project was to learn more about Node.js, TypeScript and the process behind creating a chat bot like a Discord bot. This was the first time I've used Node.js and TypeScript in a project and it was a fun learning experience!

Screenshot of the Help Message

Add this bot to your server!

Technologies

TypeScript

TypeScript is a strong typed JavaScript like programming language that complies to JavaScript. This was the first time I've used TypeScript before.

Node.js

Node.js served as the backend to run the Discord bot.

Discord.js

Discord.js was an open source library that allowed for easy use of the Discord API within a JavaScript environment.

Google Cloud

Google Cloud is the solution used to host the Node.js script in the cloud instead of locally. This is done using Google Cloud's Compute Engine, the script is run in a Linux VM instance in the cloud.

Overwatch API

The Overwatch API was used to fetch the user's or another users stats in the game Overwatch! This allowed users to look up this information within Discord without the need to have to go the Overwatch website. Example usage: !ow pc us super#7181

Memes API

The Memes API grabs memes from Reddit and the bot sends them when the user uses the !memes command!

How It Works

The bot works by connecting to the Discord API using Discord.js and the technologies mentioned above.

// create a new Discord client
const client = new Discord.Client();
const botToken = process.env.DiscordBotToken;

// login to discord and display the bot as online and active
client.login(botToken);
console.log('Logging into bot');

client.once('ready', () => {
    console.log('Bot initialized!');
    // set status to bot of number of servers
    setStatus();
    console.log('Status initialized!');
});

This part of the code attempts to connect to the Discord API, once a connection is established the bot is 'online'.

client.on('message', message => {
    // if message doesn't start with the prefix or it's a bot then ignore message 
    if (!message.content.startsWith(prefix) || message.author.bot) 
        return;

    // parse arguments and the base command
    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift()?.toLowerCase();

    // handle commands
    if (command === "ping") {
      return message.channel.send(`Pong. 🏓 \n**Latency**: ${client.ws.ping}ms`);
     }
 });

This part is the messaging handling. Once the bot recieves a message in a channel it has permissions to read messages, the bot checks if the correct prefix is used (i.e.: the user is runnning a command for this bot) and then parses the string into commands and arguments. Then a series of if / else if statements handle the commands, this is where the bot can respond with a message, reaction, deleting a message or a series of messages or a API fetch and response!

Getting Started

  • Clone or fork this repository.
git clone
  • Install all dependencies with npm
npm install
  • Create a Discord application using the Discord Developer Portal. Go to bot and copy the token, ⚠️ don't give this token to anyone as this gives full control of your bot ⚠️, paste this token in a .env file.
DiscordBotToken = <your token>
  • Add the bot to your server: Go to the OAuth2 section of your application and select bot in the scopes, then select the permissions you want to give the bot. Copy the link and add to your server! You must be administrator of the server to add bots.

  • Run the project as developer

npm run dev
  • Run the project in production
npm run prod

Contributions

If you're new to open source contributions read this guide. Issues and pull requests are welcome! If you have any feature requests post them in the issues as well!