Using Discord.js Features
Learn how to leverage Discord.js features within your meowcord bot.
Introduction
Meowcord is built on top of Discord.js, which means you have access to all of Discord.js's powerful features and objects. This allows you to do anything that Discord.js can do, while still benefiting from meowcord's simplified command system.
Accessing the Discord.js Client
You can access the underlying Discord.js client through the client
property of your Meow instance:
// Access the Discord.js client
const discordClient = bot.discordClient;
// Get the bot's ping
const ping = bot.discordClient.ws.ping;
console.log(`Bot ping: ${ping}ms`);
// Get the number of servers the bot is in
const serverCount = bot.discordClient.guilds.cache.size;
console.log(`Bot is in ${serverCount} servers`);
Working with Discord.js Objects
In your command handlers and event listeners, you'll be working with Discord.js objects like Message, Guild, User, and more. You can use all of the methods and properties these objects provide.
Here are some examples:
bot.basicCommand({
cmdName: "server",
cmdCode: async (message) => {
// Access guild (server) properties
const guild = message.guild;
const embed = {
title: guild.name,
description: `Server information for ${guild.name}`,
thumbnail: { url: guild.iconURL({ dynamic: true }) },
fields: [
{ name: "Owner", value: `<@${guild.ownerId}>`, inline: true },
{ name: "Members", value: guild.memberCount.toString(), inline: true },
{ name: "Created", value: guild.createdAt.toDateString(), inline: true },
{ name: "Channels", value: guild.channels.cache.size.toString(), inline: true },
{ name: "Roles", value: guild.roles.cache.size.toString(), inline: true },
{ name: "Emojis", value: guild.emojis.cache.size.toString(), inline: true }
],
footer: { text: `ID: ${guild.id}` },
color: 0xff1493 // Pink color
};
message.reply({ embeds: [embed] });
}
});
Using Discord.js Collections
Discord.js uses Collections to store and manage data. You can use all of the Collection methods in your meowcord bot:
bot.basicCommand({
cmdName: "members",
cmdCode: async (message) => {
// Get all members with a specific role
const role = message.mentions.roles.first();
if (!role) {
return message.reply("Please mention a role to get its members");
}
const members = role.members.map(member => member.user.tag).join(", ");
if (members.length === 0) {
return message.reply("No members have this role.");
}
message.reply(`Members with the ${role.name} role: ${members}`);
}
});
Creating Rich Embeds
You can use Discord.js's embed functionality to create rich, formatted messages:
bot.basicCommand({
cmdName: "embed",
cmdCode: async (message) => {
const embed = {
title: "Hello from meowcord!",
description: "This is a rich embed message created with Discord.js",
color: 0xff1493, // Pink color
timestamp: new Date(),
footer: {
text: "Powered by meowcord",
icon_url: bot.client.user.displayAvatarURL()
},
thumbnail: {
url: message.author.displayAvatarURL({ dynamic: true })
},
author: {
name: message.author.tag,
icon_url: message.author.displayAvatarURL({ dynamic: true })
},
fields: [
{
name: "Field 1",
value: "This is a field value",
inline: true
},
{
name: "Field 2",
value: "This is another field value",
inline: true
},
{
name: "Field 3",
value: "This is a non-inline field value",
inline: false
}
]
};
message.reply({ embeds: [embed] });
}
});
Using Discord.js Components
You can use Discord.js's message components like buttons and select menus:
bot.basicCommand({
cmdName: "buttons",
cmdCode: async (message) => {
const row = {
type: 1, // Action Row
components: [
{
type: 2, // Button
style: 1, // Primary (blue)
custom_id: "primary_button",
label: "Primary Button"
},
{
type: 2, // Button
style: 3, // Success (green)
custom_id: "success_button",
label: "Success Button"
},
{
type: 2, // Button
style: 4, // Danger (red)
custom_id: "danger_button",
label: "Danger Button"
}
]
};
message.reply({
content: "Here are some buttons:",
components: [row]
});
}
});
// Handle button interactions
bot.on("interactionCreate", async (interaction) => {
if (!interaction.isButton()) return;
const { customId } = interaction;
if (customId === "primary_button") {
await interaction.reply("You clicked the primary button!");
} else if (customId === "success_button") {
await interaction.reply("You clicked the success button!");
} else if (customId === "danger_button") {
await interaction.reply("You clicked the danger button!");
}
});
Discord.js API Reference
For a complete reference of Discord.js features and objects, you can refer to the Discord.js documentation. Any feature available in Discord.js can be used in your meowcord bot.
Complete Example
Here's a complete example that demonstrates using various Discord.js features:
const { Meow, Intents } = require("meowcord");
const { ActivityType } = require("discord.js");
const bot = new Meow({
intents: [
Intents.Guilds,
Intents.GuildMessages,
Intents.MessageContent,
Intents.GuildMembers
],
prefix: "!",
});
bot.start("YOUR_BOT_TOKEN");
// Ready event with Discord.js features
bot.on("ready", () => {
console.log(`Logged in as ${bot.discordClient.user.tag}!`);
console.log(`Bot ping: ${bot.discordClient.ws.ping}ms`);
console.log(`In ${bot.discordClient.guilds.cache.size} guilds`);
// Set a custom status
bot.discordClient.user.setActivity("with Discord.js", { type: ActivityType.Playing });
});
// Server info command using Discord.js objects
bot.basicCommand({
cmdName: "serverinfo",
cmdDescription: "Shows information about the server",
cmdCode: async (message) => {
const guild = message.guild;
const embed = {
title: guild.name,
description: "Server Information",
color: 0xff1493, // Pink color
thumbnail: { url: guild.iconURL({ dynamic: true }) },
fields: [
{ name: "Owner", value: `<@${guild.ownerId}>`, inline: true },
{ name: "Members", value: guild.memberCount.toString(), inline: true },
{ name: "Created", value: guild.createdAt.toDateString(), inline: true },
{ name: "Channels", value: guild.channels.cache.size.toString(), inline: true },
{ name: "Roles", value: guild.roles.cache.size.toString(), inline: true },
{ name: "Emojis", value: guild.emojis.cache.size.toString(), inline: true }
],
footer: { text: `ID: ${guild.id}` }
};
message.reply({ embeds: [embed] });
}
});
// User info command using Discord.js objects
bot.basicCommand({
cmdName: "userinfo",
cmdDescription: "Shows information about a user",
cmdCode: async (message) => {
const user = message.mentions.users.first() || message.author;
const member = message.guild.members.cache.get(user.id);
const embed = {
title: user.tag,
description: "User Information",
color: 0xff1493, // Pink color
thumbnail: { url: user.displayAvatarURL({ dynamic: true }) },
fields: [
{ name: "ID", value: user.id, inline: true },
{ name: "Nickname", value: member.nickname || "None", inline: true },
{ name: "Account Created", value: user.createdAt.toDateString(), inline: true },
{ name: "Joined Server", value: member.joinedAt.toDateString(), inline: true },
{ name: "Roles", value: member.roles.cache.map(role => role.name).join(", "), inline: false }
]
};
message.reply({ embeds: [embed] });
}
});