iTranslated by AI
Running a Discord Bot on Deno with Discordeno
Discordeno
This is a library that allows you to handle Discord BOTs in Deno.
The documentation is not yet very extensive, but it is updated relatively frequently, and it's a pretty nice library.
While Discord.js is a well-known Discord API library for JS, Discordeno has a significantly different feel, so some caution is required.
A major advantage of running a Discord BOT in Deno is the existence of Deno Deploy, which can be used for free. Now that Heroku has become a paid service, being able to keep a BOT running constantly for free is a huge plus.
Sample Code
The code for the BOT created this time is available in the repository below.
If the explanation is confusing, you should be able to get it working by copying this directly.
Prerequisites
It is preferable if you have experience running Discord BOTs in other languages like Node.js or Python. Having some knowledge of Deno is also helpful.
- Deno 1.28
Also, this article assumes that you are working in VSCode.
Preparing the BOT
I'll just mention some points of caution.
In the Discord BOT settings screen, there is a section called Privileged Gateway Intents. You should, at the very least, check the bottom two options.

Creating the Project
Create a new Deno project. Enable the Deno extension in VSCode, select Deno: Initialize Workspace Configuration, and press the Enter key three times.

Create the following file:
{
"tasks": {
"dev": "deno run -A --watch ./main.ts",
"start": "deno run -A --watch ./main.ts"
}
}
Also, create main.ts and deps.ts.
deps.ts
Set it up as follows:
// discordeno
export * from "https://deno.land/x/discordeno@17.1.0/mod.ts"
// dotenv
export * as dotenv from "https://deno.land/std@0.167.0/dotenv/mod.ts"
Discordeno often has breaking changes between versions, so caution is required.
dotenv is used for loading tokens.
Writing the BOT Code
Sensitive Information
Write the BOT token in .env.local.
DISCORD_TOKEN=hogehoge
Since we want to keep the logic for reading the token separate from the main BOT code, create secret.ts and set it up as follows.
import { dotenv } from "./deps.ts"
dotenv.configSync({
export: true,
path: "./.env.local",
})
export const Secret = {
DISCORD_TOKEN: Deno.env.get("DISCORD_TOKEN")!,
}
Note that if you don't set export to true in configSync, you won't be able to retrieve it with Deno.env.get.
Writing the BOT Code
We will write the BOT code in main.ts.
import { createBot, Intents, startBot } from "./deps.ts"
import { Secret } from "./secret.ts"
const bot = createBot({
token: Secret.DISCORD_TOKEN,
intents: Intents.Guilds | Intents.GuildMessages | Intents.MessageContent,
events: {
ready: (_bot, payload) => {
console.log(`${payload.user.username} is ready!`)
},
},
})
bot.events.messageCreate = (b, message) => {
if (message.content === "!neko") {
b.helpers.sendMessage(message.channelId, {
content: "Nya-n",
})
}
}
await startBot(bot)
After writing this, run the following to start the BOT:
deno task dev
Since the --watch flag is included, it will automatically restart when the code is saved. It's very convenient.
If it works correctly, typing !neko in a server where the BOT is present will result in it replying with "Nya-n".

If you have experience with Discord.js, this might feel a bit different; to send a message back to the channel where the message was received, you use helpers.sendMessage() provided by the Bot object.
Using Slash Commands
Let's try creating a slash command.
In this case, we'll create the command for our own server because it reflects changes quickly.
Save your server ID as GUILD_ID in .env.local and secret.ts.
DISCORD_TOKEN=hogehoge
GUILD_ID=1234
import { dotenv } from "./deps.ts"
dotenv.configSync({
export: true,
path: "./.env.local",
})
export const Secret = {
DISCORD_TOKEN: Deno.env.get("DISCORD_TOKEN")!,
GUILD_ID: Deno.env.get("GUILD_ID")!,
}
Modify main.ts as follows:
- import { createBot, Intents, startBot } from "./deps.ts"
+ import { createBot, Intents, startBot, CreateSlashApplicationCommand, InteractionResponseTypes } from "./deps.ts"
import { Secret } from "./secret.ts"
const bot = createBot({
token: Secret.DISCORD_TOKEN,
intents: Intents.Guilds | Intents.GuildMessages | Intents.MessageContent,
events: {
ready: (_bot, payload) => {
console.log(`${payload.user.username} is ready!`)
},
},
})
+ const nekoCommand: CreateSlashApplicationCommand = {
+ name: "neko",
+ description: "Replies with Nya-n",
+ }
+ await bot.helpers.upsertGuildApplicationCommands(Secret.GUILD_ID, [nekoCommand])
bot.events.messageCreate = (b, message) => {
if (message.content === "!neko") {
b.helpers.sendMessage(message.channelId, {
content: "Nya-n",
})
}
}
+ bot.events.interactionCreate = (b, interaction) => {
+ console.log(interaction.data?.name)
+ }
await startBot(bot)
Once you have added these, the neko slash command should be added to the server where the BOT is located.

Running it as is won't trigger a response, but you should see neko output in Deno.


Try rewriting bot.events.interactionCreate as follows:
// Omitted
bot.events.interactionCreate = (b, interaction) => {
switch (interaction.data?.name) {
case "neko": {
b.helpers.sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
content: "Nya-n!!",
},
})
break
}
default: {
break
}
}
}
Then, when you execute /neko, you should get "Nya-n!!" as a reply.

Deploying to Deno Deploy
Create .gitignore
Create a .gitignore file.
Go to gitignore.io, generate a file with the following settings, and save it as .gitignore.

Configure git
git init
git add .
git commit -m "initial commit"
Execute the above.
Optionally:
git branch -m main
you can do this as well.
Upload to GitHub
Create a repository on GitHub and upload your code.
git remote add origin [Your Repository URL]
git push -u origin master
Deployment
Modifying Secrets
Deno Deploy does not have read permissions for the local file system, so dotenv's configSync will cause a crash.
As a simple workaround, we can wrap it in a try...catch block. This approach might not be the best, so for a proper project, you should probably write code that distinguishes between development and production environments.
// Omitted
try {
dotenv.configSync({
export: true,
path: "./.env.local",
})
} catch {}
// Omitted
Access Deno Deploy, create an account if you don't have one, and sign in.
Select "New Project",

Choose "Deploy from GitHub repository," select the repository you created and main.ts, and give it a suitable name.

Clicking "Link" will connect the repository and start the deployment. Deployment finishes in about 10 seconds. It's lightning fast.
Environment Variable Settings
Configure settings such as the Discord token.
Add variables from "Settings > Environment Variables > Add Variable".

The BOT should automatically restart when the environment variables are updated, so it should be working now.
(If it doesn't work, try accessing the URL provided by Deno Deploy once.)
Conclusion
I noticed there were hardly any articles on Discordeno, so I decided to write a simple guide for creating a BOT. Discordeno's API design is a bit different from other Discord libraries, so it might be challenging to migrate directly, but Deno and Deno Deploy are very convenient, making it a great option to keep in mind.
Discordeno's documentation is not very extensive, but the code is well-commented and there are several templates available, which you should refer to. Be careful, as some code examples in the documentation are outdated and might not work as-is.
Repository for the sample code created this time:
Discussion