Localy
Prerequisites
Before starting, ensure you have the following installed on your system:
- Node.js (version 16.9.0 or higher, as required by Discord.js libraries):
- Download and install it from Node.js Official Site.
- npm (Node.js package manager, installed with Node.js).
- A code editor (e.g., VS Code).
Tip: To check if Node.js and npm are installed, run these commands in a terminal:
node -v
npm -v
Set Up Your Bot
Step 1: Create a New Directory
- Open a terminal or file explorer.
- Create a folder for your bot project (e.g.,
oceanic-bot). - Navigate into the folder:
cd oceanic-bot
Step 2: Initialize a Node.js Project
- Run the following command to create a
package.jsonfile:npm init -y
Install Oceanic.js
Run the following command in the terminal to install Oceanic.js:
npm install oceanic.js
Create Your Bot Script
Step 1: Create a bot.js File
- Inside your project folder, create a file named
bot.js.
Step 2: Write Your Bot Code
Add this example code to your bot.js file:
const { Client } = require("oceanic.js");
// Create a new client instance
const client = new Client({
auth: "YOUR_BOT_TOKEN", // Replace with your bot token
gateway: {
intents: ["GUILDS", "GUILD_MESSAGES"] // Adjust intents based on your bot's needs
}
});
// When the bot is ready
client.on("ready", () => {
console.log(`Logged in as ${client.user.username}!`);
});
// Respond to messages
client.on("messageCreate", (message) => {
if (message.content === "!ping") {
message.channel.createMessage("Pong!");
}
});
// Login to Discord
client.connect();
Important: Replace
YOUR_BOT_TOKENwith your actual bot token from the Discord Developer Portal.
5. Run Your Bot Locally
Step 1: Open the Terminal
-
Windows:
- Press
Win + Rto open the Run dialog. - Type
cmdand press Enter to open the Command Prompt. - Alternatively, you can search for "Command Prompt" or "Terminal" in the Start menu.
- Press
-
macOS:
- Open Spotlight Search by pressing
Cmd + Space. - Type "Terminal" and press Enter.
- Alternatively, navigate to Applications > Utilities > Terminal.
- Open Spotlight Search by pressing
-
Linux:
- Use the shortcut
Ctrl + Alt + Tto open the terminal. - Or search for "Terminal" in your application menu.
- Use the shortcut
Step 2: Navigate to Your Bot Directory
Once the terminal is open, navigate to the folder where your bot files are located. For example:
cd path/to/oceanic-bot
Replace path/to/oceanic-bot with the actual path to your project folder.
Step 3: Start the Bot
Run the following command:
node bot.js
If the bot starts successfully, you’ll see a message like:
Logged in as YourBotName!
Step 2: Verify It's Running
- You should see a log message like:
Logged in as YourBotName! - Test the bot in Discord by typing
!pingin a channel where the bot is active. It should reply with "Pong!".
Troubleshooting Tips
Error: Cannot Find Module
- Ensure you installed Oceanic.js correctly:
npm install oceanic.js
Bot Not Responding
- Check that the bot token in
bot.jsis correct. - Ensure the bot is invited to your Discord server with the correct permissions.
Update Dependencies
- Update npm packages regularly to avoid compatibility issues:
npm update