Yeah 👩‍💻👨‍💻, A Discord AI Powered Bot 🎮 Baby!

Blumareks
Voice Tech Podcast
Published in
6 min readOct 19, 2020

This blog post introduces a Discord chatbot powered with AI. You will use free of charge AI based chatbot engine with Watson Assistant from IBM Cloud. Optionally you will run the code as Serverless, alternatively on your spare Raspberry Pi, or with help of Containers in the cloud.

Get your Discord bot powered with AI chatbot engine from IBM Watson for free!

When my son started to moderate a Discord channel for a video blogger, and started to design a bot to improve UX of their channel, I decided to give a hint to Discord users on how to augment their bots with AI. Free of charge!

So join me in this adventure:

  • STEP 1. connecting to Discord service
  • STEP 2. getting the Ping-Pong BOT in Node.js
  • STEP 2.a making your program a container :-)
  • STEP 3. getting some advance chatbot AI features with IBM Watson Assistant

The next steps I plan to cover in the follow up blog posts:

  • STEP 4. Connecting AI based chatbot backend to your Discord server
  • STEP 5. going Serverless in the Cloud — have you heard about Serverless? try off-loading your server with Serverless — deploying BOT for FREE in the Clouds — with the Serverless 400,000 GBsec free tier per month every month!
  • STEP 6. alternatively going locally hosted — and deploy your chatbot on your spare Raspberry Pi
  • STEP 7. alternatively going with a chatbot microservice in the Container

First setting up Discord…

I owe you an explanation. Due to all the bad things have been happening around us I turned to e-sports. I played with my son Fortnite, who was then a super skilled player. But he stopped and the last year I got hooked to Apex Legends. And in order to co-op perfectly with teams I started to use Discord. But you need some kind of moderating (especially as you are a noob player), since you can get really told where you might be coming from.

Grinding challenges, while sometimes being offended by other users — The Moderator Bot please!

So I have an account and a server with Discord service. And in order to start moderating with a bot you are going to have a small journey here with me.

Let’s try to get in the developer setting of Discord. Simply login to your account, or create a new one, and then navigate to https://discordapp.com/developers/applications/

Then after making your new Discord server, or working with the old one (you need to have admin rights though), you will add the bot there.

Adding your bot

Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com

You might want to use the following URL to add the bot to your server:

https://discord.com/oauth2/authorize?client_id=<YOUR-BOTS-CLIENT-ID>&scope=bot

The Client Id can be taken from the General Information tab — as shown on the below screenshot.

Your BOT’s CLIENT ID is shown here.

After you click authorize you should see the bot in the list of the users:

Authorize your bot to be used on your Discord server.

2. The Discord Node JavaScript library

If you are new to JavaScript, no worries 😉 This blog post would show you enough to try Node (the server-side version of JavaScript) out.

mkdir mybot
cd mybot
npm install discord.js

(npm stands for Node Package Manager — see here https://docs.npmjs.com/cli/install for more information).

then using your fav code editor, and create the file index.js with the example content (following the explanations from here: https://discord.js.org/#/docs/main/stable/general/welcome)

const Discord = require('discord.js'); 
const client = new Discord.Client();
client.on('ready', () => {
console.log('Logged in as ${client.user.tag}!');
});
client.on('message', msg => { if (msg.content === 'ping')
{
msg.reply('pong');
} });
client.login('token');

Now you can replace the token with the token of your bot — you can find the token id here. (Attention — you do not want to share the token with anybody!)

And then run it:

node index.js

As soon as it runs and you get the bot online:

Bot is on-line!

And now you can ping the bot in the channel.

PING — PONG exchange with the bot

Congratulations! You spun your first Discord bot.

2.a Instead of doing native Node.js go with Containers

Alternatively you can omit installing Node or npm, and just run everything in a container with Docker Desktop (well, you need to install the Docker): https://www.docker.com/products/docker-desktop

I thought it would be super cool to put it in the container, if later you want to keep running in the Cloud.

So I added / altered two lines in the above code of index.js and added the Dockerfile.

const Discord = require('discord.js');
const client = new Discord.Client();
const Token = process.env.token;
client.on('ready', () => {
console.log('Logged in as ${client.user.tag}!');
});
client.on('message', msg => { if (msg.content === 'ping')
{
msg.reply('Pong!');
}});
client.login(Token);

Also add Dockerfile

FROM node:10 
ENV NODE_CONTAINER_VERSION=1.0.0
# Create directory for application
WORKDIR /data/bot-app
# Install dependencies
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "node", "index.js" ]

See all the files here: https://github.com/blumareks/discord-bot

you might want to clone the reposiotry from the github or download it:

git clone https://github.com/blumareks/discord-bot.git
cd discord-bot

To start building your container you would need to use Docker Desktop, and build the image with the following command:

docker build -t <YOUR_DOCKER_ID>/node-container .

Now you can start it with passing the Token as the environment variable:

docker run -e token=”<YOUR_TOKEN>” -d <YOUR_DOCKER_ID>/node-container

You can check if the container is running simply execute the following:

docker logs <ID_OF_YOUR_CONTAINER>

To stop it simply type:

docker container list

This command would produce a list of containers (hopefully one that you spun!).

docker container stop <ID_CONTAINER>
docker container rm <ID_CONTAINER>

eventually you can remove your container (and also images)

docker image list
docker image rm <ID_IMAGE>

Now let’s add AI based chatbot backend.

3. Adding the AI powered chatbot backend

Before we move on you can create the AI based chatbot engine. What you need that is the free Lite Account in IBM Cloud. You will be able to access the bot there.

Try Watson Assistant yourself for free — just use this link to invoke IBM Cloud registration/signup/login page (this URL gives an author = me some brownie points): https://ibm.biz/Bd2CUa

Steps to secure your account and get the are pretty simple:

  1. provide and confirm your email, and some details…
  2. and then simply go to Catalog and pick Watson Assistant
Picking Watson Assistant from IBM Cloud Catalog.

Now you can create and test the AI powered engine from IBM.

See some examples here:

https://developer.ibm.com/tutorials/create-your-first-assistant-powered-chatbot/

https://github.com/watson-developer-cloud/assistant-simple

See also more information here: https://developer.ibm.com/tutorials/create-your-first-assistant-powered-chatbot/

You can find more on how to hook the Watson Assistant with your bot and Discord from this github here with the free examples and watch the recording of the webinar here: https://www.crowdcast.io/e/discord-chatbot-with-ibm

The code used in this app is here: https://github.com/blumareks/discord-bot

There will be the next part to the webinar and this blog post… stay tuned — especially since I am going to promote my book here on using Serverless and AI with Swift and Apache OpenWhisk:

If you like it please clap. You can subscribe to my Twitter account here: https://twitter.com/blumareks

Something just for you

--

--

Blumareks
Voice Tech Podcast

I am a technology advocate for autonomous robots, AI, mobile and Internet of Things - with a view from both the enterprise and a robotics startup founder.