Creating a Discord bot with Meinu and the Bun JavaScript runtime offers a streamlined and efficient development experience. Meinu simplifies the creation and handling of slash commands, while Bun provides a fast and modern environment for running JavaScript. Here’s how you can set up your bot:
1. Prerequisites
- Discord Account: Ensure you have a Discord account to manage your bot.
- Bun Installed: Install Bun by following the instructions on the official Bun website.
- Code Editor: Use a code editor like Visual Studio Code for writing your code.
2. Create a New Discord Application and Bot
-
Visit the Discord Developer Portal:
- Go to the Discord Developer Portal and log in.
-
Create a New Application:
- Click “New Application,” name it, and click “Create.”
- Add a Bot:
- In your application settings, navigate to the “Bot” tab.
- Click “Add Bot” and confirm.
- Copy the Token provided; you’ll need this for authentication. Keep it secure.
3. Set Up Your Development Environment
-
Initialize a New Bun Project:
- Open your terminal and create a new directory for your project:
mkdir my-discord-bot cd my-discord-bot
- Initialize a new Bun project:
bun init
- Open your terminal and create a new directory for your project:
- Install Meinu:
- Install Meinu:
bash bun i meinu
- Install Meinu:
4. Develop Your Bot
-
Create a
.env
File:- In your project root, create a
.env
file to store your bot’s token:
ReplaceDISCORD_TOKEN=your_bot_token_here
your_bot_token_here
with the token you obtained earlier.
- In your project root, create a
-
Create the Bot File:
-
Create a file named
index.ts
in your project root. -
Add the following code to set up the bot:
// index.ts import { Meinu, Command } from "meinu"; import dotenv from "dotenv"; dotenv.config(); let commands = [ new Command<Meinu>({ name: "ping", description: "Pong!", owners_only: true, // default: false nsfw: true, // default: false }).addHandler("chat_input", async (bot, int) => { const sent = await int.deferReply({ withResponse: true }); if (!sent.resource?.message?.createdTimestamp) return int.editReply( "An error occurred while executing the command." ); const diff = sent.resource?.message?.createdTimestamp - int.createdTimestamp; const content = [ "### 🏓 Pong!", `## ${diff}ms`, ...(bot.isSharding ? [`-# via shard #${bot.shardId}`] : []), ].join("\n"); return int.editReply(content); }), ]; new Meinu({ name: "MyBot", color: "LuminousVividPink", }) .register_commands(commands) .init(); // starts the bot, .init(TOKEN) if `TOKEN` env is not set
This sets up a basic bot that responds to the
/ping
command with “Pong!”.
-
5. Run and Test Your Bot
-
Run the Bot:
- In your terminal, execute:
bun index.ts
This starts your bot.
-
Invite the Bot to Your Server:
- In the Discord Developer Portal, navigate to the “OAuth2” tab of your application.
- Under “OAuth2 URL Generator,” select “bot” as the scope and assign appropriate permissions (e.g., “Send Messages”).
- Use the generated URL to invite your bot to your Discord server.
- Test the Command:
- In your Discord server, type
/ping
. - The bot should respond with “Pong!”.
- In your Discord server, type
6. Further Development
- Add More Commands: Utilize Meinu’s
Command
class to add more commands tailored to your bot’s functionality. - Explore Meinu’s Features: Delve into Meinu’s documentation to discover advanced features like subcommands, permissions, and more.
By following these steps, you can create a Discord bot using Meinu and Bun that enhances your server's interactivity and functionality. Happy coding!