Discord
These examples demonstrate how to implement fundamental functionalities for a Discord bot using the Discord.js client. For a comprehensive list of available functions, refer to the Discord Reference, and explore the DiscordJS Documentation.
Example: Bot Initialization & Custom Status
// Function to load the Discord bot
function loader() {
// Cancel any existing status update events
this.cancelevents('updatestatus');
// Create a new instance of the bot client
const discord = new Discord('Bot Game Server');
// Configure event listeners for the bot
this.handleListeners(discord);
// Log in with the bot client
}
function handleListeners(discord) {
const self = this;
// Triggered when the bot successfully connects to Discord
discord.on('ready', () => {
// Schedule a status update event
self.scheduleevent(0, 'updatestatus', discord);
// Log a confirmation message in console
echo('Discord bot - ready');
});
}
// Function to update the bot's status
function onUpdateStatus(discord) {
// Generate dynamic status messages
const texts = ['Ruins', 'Enchanted forests', 'Game Server', 'Cool Status'];
// Append player count to status if players are present
const countPlayers = `${Server.playercount} Player${Server.playercount !== 1 ? 's' : ''}`;
if (Server.playercount > 0) {
texts.push(countPlayers);
}
// Randomly select a status message for variety
const randomIndex = Math.floor(Math.random() * texts.length);
// Define Discord activity types for rich presence
const ActivityTypes = {
PLAYING: 1, // Game activity
STREAMING: 2, // Streaming activity
LISTENING: 3, // Listening activity
WATCHING: 4, // Watching activity
};
// Update bot's Discord presence with a random status
discord.client.user.setPresence({
activities: [{
name: texts[randomIndex],
type: ActivityTypes.LISTENING
}],
status: 'idle', // Set bot status to idle
});
// Schedule the next status update
this.scheduleevent(5, 'updatestatus', discord);
}
Example: Basic Command Handling
// Function to handle bot commands
function handleCommands(discord) {
discord.on('messageCreate', (message) => {
// Respond to ping command
if (message.content.startsWith('!ping')) {
message.reply(`Pong! 🏓`);
}
});
}