DEV Community

mmvergara
mmvergara

Posted on

7 2 2 2 2

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay