Getting the AI powered bot to interact with a Discord user.

Discord Bot on your voice channel

Blumareks
5 min readNov 17, 2020

This blog post is about starting a discord bot on your voice channel. How to play sounds and audio files, this is the first step. The second step is to make the connection to the cloud and provision the AI based services like TTS for providing additional functionality.

If you are following the adventure with the project of launching the AI powered bot, now it is a perfect time to start to use the functionality of the voice channels in Discord space.

This blog post will let you know on how to:

  • play an audio file in the Discord voice channel by a prompt to a bot;
  • it is a necessary introduction to respond with a short text from AI service that converts Text-to-Speech (Watson Text To Speech or TTS for short).

This blog post content was presented during a webinar. Follow this link to view the recording: https://www.crowdcast.io/e/discord-chatbot-with-ibm-3

A Bot is playing an audio file

Starting from the previous simple solution of having a bot in a Docker container — (refer to the previous blog post here: (a friendly link): https://medium.com/voice-tech-podcast/yeah-a-discord-ai-powered-bot-baby-d9cb73b4775d?source=friends_link&sk=5fd7e544211b3f64b8e5ca476dd547c9

Start by cloning the github repository:

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

Now you need to install the missing audio component required by Discord.js:

npm install discord.js @discordjs/opus 

This command would create an additional entry in the package.json — please refer to the following listing here:

{
"dependencies": {
"@discordjs/opus": "^0.3.3",
"discord.js": "^12.4.1"
}
}

Since the index.js is going to use some of the FFMPEG functionality of the underling operating system in your Docker container, you need to add couple steps in the Dockerfile:

#################################################################
# instead of starting with node:12 you need to install FFMPEG. #
#################################################################
FROM ubuntu:20.04ENV DEBIAN_FRONTEND noninteractive# Initial update and some basics.
# This odd double update seems necessary to get curl to download without 404 errors.
RUN apt-get update --fix-missing && \
apt-get install -y wget && \
apt-get update && \
apt-get install -y curl && \
apt-get update
# Get ffmpeg
RUN apt-get update --fix-missing && \
apt-get install -y software-properties-common && \
add-apt-repository -y ppa:savoury1/multimedia && \
apt-get update --fix-missing && \
apt-get install -y ffmpeg
# install nodejs and npm
# based on https://github.com/nodejs/docker-node
ENV NODE_VERSION 12.19.0
RUN curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz" \
&& tar -xzf "node-v$NODE_VERSION-linux-x64.tar.gz" -C /usr/local --strip-components=1 \
&& rm "node-v$NODE_VERSION-linux-x64.tar.gz"
#################################################################
# previously you started with the next line of getting the node #
# node:12 #
#################################################################
ENV NODE_CONTAINER_VERSION=1.0.0# Create directory for application
WORKDIR /data/bot-app
# Install dependencies
COPY package*.json ./
RUN npm installCOPY . .CMD [ "node", "index.js" ]

Now you need also to add few lines of code that are responsible for getting you the functionality for playing the audio files (the original index.js is just a bit longer):

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', async message => {// Join the same voice channel of the author of the message
if (message.member.voice.channel) {
const connection = await message.member.voice.channel.join();
if(message.content === 'ping'){
const dispatcher = connection.play('https://cdn.glitch.com/60a14f49-0846-4bad-b84a-e5f018c2130d%2Fmsn_alert.mp3?1506640405402');
//connection.play('https://cdn.glitch.com/60a14f49-0846-4bad-b84a-e5f018c2130d%2Fmsn_alert.mp3?1506640405402', { volume: 0.1 });
dispatcher.on('start', () => {
console.log('audio is now playing!');
})
.catch(err => {
console.log('error:', err);
});
// Always remember to handle errors appropriately!
//dispatcher.destroy();
//Leave voice channel
//connection.disconnect();
}
}
})
client.login(Token);

After you are done with the index.js file, you can build your container image. Brace yourself — the first docker build will take a bit longer due to the need of building a lot of the basic libraries for the Docker container.

You can just run this:

docker build -t <Your-Docker-Id>/node-discord-tts:1.0 .

After the moment (well, it took me some good time when first running it to grab a coffee/tea), you can run your container locally — see the following blog post on running it on IBM Cloud for free: https://blumareks.medium.com/always-on-discord-bot-in-the-cloud-%EF%B8%8F-with-the-ai-powered-chatbot-4c18503a1c49

Use the following command to run your container with a Discord bot for voice channels:

docker run -e token="<put here your bot token from Discord - see the screenshot>" -d blumareks/node-discord-tts:1.2
Copying the token of your bot for using it to start your container

Before you will hear a sound from your bot — you need yourself to join a voice channel. For example you can join a Voice Lounge channel. It is needed to fulfill this condition in our code in index.js:

const connection = await message.member.voice.channel.join();

And now you can quickly type ping in any text channels to hear a nice chime from the bot:

Get into the voice channel — so to be ready to get your bot on the platform — type ‘ping’ in a text channel
Typing a ping message in a text channel

Congratulations! You made your container playing an audio file for you… now you can even play some background music in your Lounge 🎸channel🎙

Summary

What is next? I want to cover in the intermediate session some additional functionalities for using our Discord AI bot containers in the cloud, and AI dialog for VOICE Discord channels and creating some voice interactions with our chatbot using Watson TTS service to get our bot to speak. Is that even possible — join me on Crowdcast for my free webinar (or watch the recording to learn more): https://www.crowdcast.io/e/discord-chatbot-with-ibm-part3

Finally cleaning after webinar

Post work cleaning up of your system

docker container list
docker container stop <container id>
docker container rm <container id>
docker image list
docker image rm <image id>....everything cleaned (size of the image with our bot in Node.js is 930MB!) - just saying!

When you wrap up, you can clean up your system. Please list the containers

Then stop the one you created for our bot for our bot.

You can now delete the cluster, and then the image associated, if it is needed.

Finally join me for the next webinars. Look for more information on my Twitter here: https://twitter.com/blumareks

--

--

Blumareks

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.