DEV Community

Bernard K
Bernard K

Posted on

4

How to build a LLM chat bot in Discord: The Ultimate Guide

Discord is a free voice and text chat app for iPhone and Android that lets you communicate with your friends and co-workers like never be

How to Build a LLM Chat Bot in Discord: The Ultimate Guide

Discord is a free voice and text chat app for iPhone and Android that lets you communicate with your friends and co-workers like never before. A Discord chat bot is a chatbot that lives in your Discord server, and it can be programmed to do a variety of things, from playing games to sending messages.

In this blog post, we're going to show you how to build a Discord chat bot in just a few minutes. We'll be using the Discord.js library, which makes it easy to interact with the Discord API.

Getting Started

To get started, we need to create a new file called index.js. We're going to start by requiring the Discord.js library and creating a new Discord client:

const Discord = require('discord.js');
const client = new Discord.Client();
Enter fullscreen mode Exit fullscreen mode

Configuration

Next, we need to define a few things:

  • The token that we'll be using to connect to the Discord API. You can find this in your Discord settings.
  • The prefix that we'll be using for our bot's commands.
  • The ID of the channel that our bot should be listening for commands in.
const token = 'your-token-here';
const prefix = '!';
const channelId = 'your-channel-id-here';
Enter fullscreen mode Exit fullscreen mode

Command: Ping

Now that we have our basic configuration set up, we can start writing our bot's commands. Let's start with a simple "ping" command:

client.on('message', message => {
  if (message.content === `${prefix}ping`) {
    message.channel.send('pong!');
  }
});
Enter fullscreen mode Exit fullscreen mode

This command will cause our bot to respond with "pong!" whenever someone types "!ping" in the channel that our bot is listening in.

Command: Rock-Paper-Scissors

Now let's add a more complex command that will play a game of rock-paper-scissors against the user:

client.on('message', message => {
  if (message.content === `${prefix}rps`) {
    const hands = ['rock', 'paper', 'scissors'];
    const hand = hands[Math.floor(Math.random() * hands.length)];
    message.channel.send(`I choose ${hand}!`);
  }
});
Enter fullscreen mode Exit fullscreen mode

This command will have our bot choose a random hand (rock, paper, or scissors) and then reply with what it has chosen.

Command: Website Link

Finally, let's add a command that will give the user a link to our website:

client.on('message', message => {
  if (message.content === `${prefix}website`) {
    message.channel.send('https://www.example.com');
  }
});
Enter fullscreen mode Exit fullscreen mode

This command will send the user a link to our website.


Tags: #tech, #trending, #python

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

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