Meowcord Logo
meowcord
DocsEvent Handling

Event Handling

Learn how to respond to Discord events with meowcord.

Introduction to Events

Events are actions or occurrences that happen in Discord that your bot can respond to. These include things like messages being sent, users joining a server, reactions being added to messages, and more.

Meowcord provides two methods for handling events: client.on() and client.once().

Using client.on()

The client.on() method allows you to listen for an event every time it occurs:

index.js
bot.on("ready", () => {
  console.log(`Logged in as ${bot.client.user.tag}!`);
});

bot.on("guildMemberAdd", (member) => {
  const channel = member.guild.channels.cache.find(ch => ch.name === "welcome");
  if (!channel) return;
  
  channel.send(`Welcome to the server, ${member}!`);
});

Using client.once()

The client.once() method allows you to listen for an event only once:

index.js
bot.once("ready", () => {
  console.log("This will only be logged once when the bot starts");
});

Common Events

Here are some common Discord events you might want to handle:

ready

Emitted when the bot successfully connects to Discord:

index.js
bot.on("ready", () => {
  console.log(`Logged in as ${bot.client.user.tag}!`);
  console.log(`Bot is in ${bot.client.guilds.cache.size} servers`);
});

guildMemberAdd

Emitted when a user joins a server:

index.js
bot.on("guildMemberAdd", (member) => {
  const channel = member.guild.channels.cache.find(ch => ch.name === "welcome");
  if (!channel) return;
  
  channel.send(`Welcome to the server, ${member}!`);
});

messageCreate

Emitted when a message is sent in a channel the bot can see:

index.js
bot.on("messageCreate", (message) => {
  // Ignore messages from bots
  if (message.author.bot) return;
  
  // Ignore messages that don't start with the prefix
  if (!message.content.startsWith(bot.prefix)) return;
  
  console.log(`Received message: ${message.content}`);
});

messageReactionAdd

Emitted when a reaction is added to a message:

index.js
bot.on("messageReactionAdd", (reaction, user) => {
  // Ignore reactions from bots
  if (user.bot) return;
  
  console.log(`${user.tag} reacted with ${reaction.emoji.name}`);
});

Complete Example

Here's a complete example of a bot that handles multiple events:

index.js
const { Meow, Intents } = require("meowcord");
const { ActivityType } = require("discord.js");

const bot = new Meow({
  intents: [
    Intents.Guilds,
    Intents.GuildMessages,
    Intents.MessageContent,
    Intents.GuildMembers,
    Intents.GuildMessageReactions
  ],
  prefix: "!",
});

bot.start("YOUR_BOT_TOKEN");

// Ready event
bot.on("ready", () => {
  console.log(`Logged in as ${bot.discordClient.user.tag}!`);
  console.log(`Bot is in ${bot.discordClient.guilds.cache.size} servers`);
  
  // Set bot status
  bot.discordClient.user.setActivity("with meowcord", { type: ActivityType.Playing });
});

// Member join event
bot.on("guildMemberAdd", (member) => {
  const channel = member.guild.channels.cache.find(ch => ch.name === "welcome");
  if (!channel) return;
  
  channel.send(`Welcome to the server, ${member}!`);
});

// Message event (this is in addition to commands)
bot.on("messageCreate", (message) => {
  // Ignore messages from bots
  if (message.author.bot) return;
  
  // Ignore commands (they'll be handled by the command system)
  if (message.content.startsWith(bot.prefix)) return;
  
  // Respond to specific keywords
  if (message.content.toLowerCase().includes("hello")) {
    message.reply("Hello there!");
  }
});

// Basic command
bot.basicCommand({
  cmdName: "ping",
  cmdCode: async (message) => {
    message.reply("Pong!");
  }
});