Skip to content

Latest commit

 

History

History
166 lines (122 loc) · 6.45 KB

5. Bot, Meet Redis.md

File metadata and controls

166 lines (122 loc) · 6.45 KB

Bot, meet Redis!

What's going on here?!

Okay so, let's say you want an administrative panel (or a rest api) for your bot. What's the best way to access all that information? Having express embedded in my app? No. Don't be silly! Seperate them! That way, your bot can focus on being a bot, and your web project doesn't interfere with any operations.

What are we going to make?

We're going to use that simple ping-pong bot, and add something called discord.js-redis to it. We don't want to install a redis server on our operating system, but we would like to keep the data in place. So that way we can transfer it to our hosting platform (not heroku) after we think it's good!

Re-creating the bot and Dockerfile

First things first, you'll need to install and create the bot again so let's do that now. Let's setup the project, you can modify everything like the package.json later.

    $ npm init --yes
    $ npm install discord.js spec-tacles/discord.js-redis --save 
    $ touch Dockerfile docker-compose.yml bot.js settings.json

Before we hit the bot, I want to discuss something. We created the Dockerfile in Part 3, that used an image call 'node' from Docker Hub, this time around we're going to go with the alpine image. The reason is it is smaller and gives you more control of what you want (image manipulation libraries, ffmpeg, etc) in your container, so let's do that now.

    FROM alpine:latest
    
    # Setup Work directory.
    WORKDIR /usr/src/bot
    COPY package.json settings.json ./

    # Let's install everything!
    RUN apk add --update \
        && apk add --no-cache nodejs-current nodejs-npm \
        && apk add --no-cache --virtual .build git curl build-base g++ \
        && npm install \
        && apk del .build
    
    # Copy project to our WORKDIR
    COPY . .

    # Let's run it!
    CMD [ "node", "bot.js" ]

The main reason we went this route is for more control as I said, now we will begin modifying and creating a brand new bot with a settings file we touched earlier (settings.json)

    {
        "token": "TOKEN HERE",
        "prefix": "?"
    }

Now that that's over, let's work on the bot a bit from the example in Part 2. I'll explain what's happening within the code itself as it's easier that way.

In the file bot.js, we're going to go ahead and do this:

    /**
        This is the requirement / dependency setup. 
        I did it this way to create a cleaner look.
        Although, absolutely not required.
    */
    const { token, prefix } = require('settings.json');
    const { Client } = require('discord.js');
    const { RedisClient } = require('discord.js-redis');

    /**
        We're now setting up both our Bot client and our Redis
        client here.
    */
    const client = new Client();

    /** 
        Redis client using discord.js-redis.
        When using the constructor for discord.js-redis you can specific options for 
        node-redis as well (such as amount of databases, host, port, password, etc)
    */
    const redis = new RedisClient(client, {});

    /** 
        Fire events! 
    */
    redis.on('ready', () => console.log('Redis ready!'));
    client.on('ready', () => console.log('Discord ready!'));

    /* 
        Message Handler 
    */
    client.on('message', (msg) => {
        if (msg.author.bot) return;

        if (message.content.startsWith(prefix + 'ping')) {
            msg.channel.send('!gnip');
        }
    });

    /**
        Start and login.
    */
    client.login(token);

Okay so, I know that using .startsWith(prefix + command) isn't the best, but just go with it for now. :p


Okay, so now that we have that in order, we still need to create the ``docker-compose`` file. I'll explain each step here like I did with the ``Dockerfile`` in Part 3.

First, open it (duh.). Once inside you'll notice that it's not JSON or XML, it's YAML. YAML is a basic whitespace markup language (so be mindful of your tabs and spaces). I use version 3, but you might need to use version 2 depending on how you installed docker-compose.

    version: '3'
    services:
     bot:
      build: .
      restart: always
      depends_on:
       - redis

Okay, so, what exactly is going on here? We're seperating everything into services (might require some modification on your part) to keep everything seperate.

version: '3' -> This is the docker-compose version, like I said, you might need to change this depending on install method.

services: bot: -> This is our service bot name.

build: . -> This will build our Dockerfile from scratch

restart: always -> If something say breaks on the container-level, or something fails, it will restart, always under any circumstance (other options: "no", on-failure, unless-stopped)

depends_on: -> What our bot will depend and link with, in our case redis.

But Nomsy, we didn't setup redis yet, what's up with that? Okay yeah, we haven't. So let's do that now taking the docker-compose.yml file we have just created:

    version: '3'
    services:
     bot:
      build: .
      restart: always
      depends_on:
       - redis
     redis:
      image: redis:3.0-alpine

Yup, that's it. You can setup and use environmental variables and even where storage goes as well. You can see more here: Compose-file Manual

Build, run.

Now that we have done all of this work to sandbox our bot with it's own database, let's give it a shot!

    $ docker-compose up

You'll notice that you'll be instantly teleported into an installer, then the instance and bot itself. If you want to run it as a "background service", add the option -d after up which daemonizes the container(s) to the background.

That's it! I'll provide more information and add more to the guide at a later date. If you need me I'm on various servers around the net, just ping me (Nomsy).

AUTHORS NOTE: Will add environmental variable setups instead of a configuration file as it's easier to maintain and change and it just works. Will also add PostgreSQL and a rest server setup using this setup in a later tutorial. Will also add modifying and viewing to the redis db to the bot in the future. STAY TUNED!

Thanks for reading! See ya soon!