How to make a Discord clock bot

This is supposed to be a banner

Yes, this is the same banner as before, with just a random extra clock image I found on DuckDuckGo.

My previous post covered the bare basics of a Discord bot (and I didn't even touch the interactions part). This time, let's see how to make a Discord "clock" bot.

What does a clock bot do?

It just monitors a certain voice channel in the server and changes its name to match the time periodically.

Note

Avoid using "clock bots" tbh. I only made this guide because I've seen too many people asking for such bots. Why do people even want a clock bot? Lazy to do math or lookup a timezone?

Prerequisites

  • Everything from my previous guide here.

Getting started

Now that you have all the prerequisites ready, let us begin making the actual bot.

Create a new folder

Create a new folder to make your bot in. Make sure it is an empty folder.

Open the command line.

If you use Windows, this is your command prompt or powershell. If you use GNU Linux, you'd know what a command line is.

Navigate to your folder through

cd <path-to-your-folder>

like

cd /home/mydumbpc/projects/discord-bot

Install Eris

We will be using Eris for this guide. Why? Because it is simple.

npm install --save eris

This should install Eris in your folder. You should now see a package.json file and a node_modules folder in it.

Create your main file

  • Create a file with a .js extension. Name it whatever you want. For ease of explaining, I'll name mine coolbot.js
  • Create another file.
  • Let's name this config.js.

The content should be

export const TOKEN = 'your discord bot token here'

Yes, the token you copied a while ago.

Messing up package.json a bit

  • Open package.json.
  • Enter the following line above your dependencies line.
"type": "module",
  • This line makes sure that your program can use stuff like import and export.

Start coding

Creating your bot file

Inside your coolbot.js,

import { Client } from 'eris'

We first import Eris into your bot. Eris is what we use to connect your bot to Discord.

Next, we import the token you saved in config.js.

import { TOKEN } from './config.js'

Now you have Eris and your token ready. Let's move on to the bot part now.

const myCoolBot = new Client(TOKEN)
  • Here, we are creating a new "Client", which will be used to connect to Discord. It is similar to how you use the Discord app to access chats and stuff.
  • We supply your bot token into this client so that the client can log into your bot's account.
  • The final step would be, starting up!
await myCoolBot.connect()

With this, your bot can finally connect to discord. But how will you know if your bot successfully connected?

myCoolBot.on('ready', () => {  console.log('I am alive!')})

With this, when your bot becomes "ready", you will see a "I am alive!" in your console.

import { Client } from 'eris'import { TOKEN } from './config.js'const myCoolBot = new Client(TOKEN)myCoolBot.on('ready', () => {  console.log('I am alive!')})await myCoolBot.connect()

Let's start the bot!

  • Open your command line like before, and enter the following:
node coolbot.js
  • In your case, replace coolbot.js with whatever you named your bot file as.
  • Tada! Your bot is now alive!
  • You should see "I am alive!" printed in your console.

Now the "clock" part

We first need a channel for the bot to work with. Copy the ID of a voice channel in your server.

Go over to config.js and add this line:

export const CHANNEL = "channel id here"

Enter your channel ID in place of channel id here.

Come back to your main file and...

import { TOKEN, CHANNEL } from './config.js'

After myCoolBot.connect() line, add this.

myCoolBot.editChannel(  CHANNEL,   {    name: `UTC ${new Date().split(" ")[4]}`  })

This makes the bot change the CHANNEL channel's name into something like UTC 12:55:12.

But this happens only once and the bot doesn't change the channel's name when the time updates. For that, we use a setInterval.

setInterval(async () => {  myCoolBot.editChannel(    CHANNEL,     {      name: `UTC ${new Date().split(" ")[4]}`    }  )}, 60000)

This makes the bot repeat this action every 60000 milliseconds, that is, 60 seconds. Why not lesser? Because it ain't nice to spam these actions.

Now your clock bot works!

Complete Code

I know 90% of y'all were waiting for this.

import { Client } from 'eris'import { TOKEN, CHANNEL } from './config.js'const myCoolBot = new Client(TOKEN)myCoolBot.on('ready', () => {  console.log('I am alive!')})await myCoolBot.connect()setInterval(async () => {  myCoolBot.editChannel(    CHANNEL,     {      name: `UTC ${new Date().split(" ")[4]}`    }  )}, 60000)

Conclusion

You made a bot. The end. Now keep experimenting with it.