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: Custom Status & Discord Initialization

// Function to load the Discord bot
function loader() {
    // Get the temporary bot client
    const tempClient = Discord.get('Bot Game Server');

    // Cancel any existing status update events
    this.cancelevents('updatestatus');

    // Destroy the temporary client if it exists
    if (tempClient) {
        tempClient.destroy();
    }

    // Create a new instance of the bot client
    this.client = new Discord('Bot Game Server');

    // Handle client listeners
    this.handleListeners(this.client);

    // Log in with the bot client
}

function handleListeners(client) {
    const self = this;

    // Listener for when the client is ready
    client.on('ready', () => {
        // Schedule a status update event
        self.scheduleevent(0, 'updatestatus', client);

        // Log a confirmation message in console
        echo('Discord bot - ready');
    });
}

// Function to update the bot's status
function onUpdateStatus(client) {
    // Get the player count and prepare status messages
    const countPlayers = `${Server.playercount} Player${Server.playercount !== 1 ? 's' : ''}`;
    const texts = ['Ruins', 'Enchanted forests', 'Game Server', 'Cool Status'];

    // Include the player count if there are players on the server
    if (Server.playercount > 0) texts.push(countPlayers);

    // Select a random status message
    const randomIndex = Math.floor(Math.random() * texts.length);

    // Defines the activity types for Discord presences.
    const ActivityTypes = {
        PLAYING: 1,
        STREAMING: 2,
        LISTENING: 3,
        WATCHING: 4,
    };

    // Set the bot's status on Discord
    client.client.user.setPresence({
        activities: [{ name: texts[randomIndex], type: ActivityTypes.LISTENING }],
        status: 'idle',
    });

    // Schedule the next status update
    this.scheduleevent(5, 'updatestatus', client);
}

Example: Commands

// Function to handle bot commands
function handleCommands(client) {
    client.on('messageCreate', (message) => {
        // Check if the message starts with the ping command
        if (message.content.startsWith('!ping')) {
            // Reply with "Pong!"
            message.reply(`Pong!`);
        }
    });
}