DEV Community

Cover image for D-rex, discord bots made easy
Okirshen
Okirshen

Posted on • Edited on

D-rex, discord bots made easy

Let's learn how to build a discord bot Deno style!

Foe this we will need a libary, for this we have the great libary harmony, which let's us interact with the discord API.

But to make things easier we will use D-rex. D-rex is a framework built on harmony that let's u write commands and hooks for messages.

First things first we will need to import the client:

import { app } from 'https://deno.land/x/drex/mod.ts';
Enter fullscreen mode Exit fullscreen mode

Now we that we have the client object we will need to create a client and register our command. Our bot will have a simple ping command:

const client = app(['!']) / creates app and supplies list of prefixes

client.command({{ name: ​'ping'}))
Enter fullscreen mode Exit fullscreen mode

Next we can register aliases, this step is not required

client.command({{ name: ​'ping', aliases: ['p', 'test']})
Enter fullscreen mode Exit fullscreen mode

Now all we need is to create the handler:

const ping = (message) => {
    message.reply('pong from deno and D-rex')
}

client.command({{ name: ​'ping', aliases: ['p', 'test'], handler: ping)
Enter fullscreen mode Exit fullscreen mode

Now we have a client. and a command last step is to start the bot with the API key:

client.listen('your API key')
Enter fullscreen mode Exit fullscreen mode

We did it, now we have a running bot made with deno and js/ts!!

import { app } from 'https://deno.land/x/drex/mod.ts';

const client = app(['!']) / creates app and supplies list of prefixes

const ping = (message) => {
    message.reply('pong from deno and D-rex')
}

client.command({{ name: ​'ping', aliases: ['p', 'test'], handler: ping)

client.listen('your API key')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)