DEV Community

mmvergara
mmvergara

Posted on

Create a Discord Bot using Next JS

There are a lot of nodejs bot out there but we can actually create one using nextjs and host it for free in vercel!. An actually free discord bot.

How it works

To make things simple this is possible because of discord api



export const discord_api = axios.create({
  baseURL: "https://discord.com/api/",
  timeout: 3000,
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
    "Access-Control-Allow-Headers": "Authorization",
    Authorization: `Bot ${BOT_TOKEN}`,
  },
});


Enter fullscreen mode Exit fullscreen mode

Then for our response it is just need to be a format of APIInteractionResponse and then the bot will respond!


The Boilerplate

Discord Bot

Github Repository

Given all of the simplicity on how it works, there are still few things we need to take care of like, body parsing, interaction, registering the commands, verifying request, creating commands, typing system.

I Created a boilerplate that take care's of all of the underlying stuff and make it so you just have to focus on making commands.

With this template you can just go to ./commands directory and create a ts file there and write your command.

since the execution function can be async we can

  • Connect to a database
  • Fetch data from an API
  • Do complex logic
  • etc...

Invite this Bot to your server


Limitation

This discord bot as you have probably guessed cannot listen for messages or events in the discord server, and is probably limited to slash commands only.


This is one of my first project that actually the public can use. so I'm kind'a proud of it and is looking for ways to improve it. I still have a lot to learn i probably don't even know how discord api works.

Top comments (0)