Moderation Examples
Auto-Moderation System
const { ModerationUtil, LoggerUtil } = require('@zerrodevs/discord-bot-utils');
client.on('messageCreate', async message => {
if (message.author.bot) return;
// Check for inappropriate content
if (message.content.includes('bad-word')) {
const success = await ModerationUtil.timeout(
message.member,
300, // 5 minutes
'Using inappropriate language'
);
if (success) {
await LoggerUtil.log('info', 'User timed out for inappropriate language', {
user: message.author.tag,
content: message.content
});
}
}
});
Message Cleanup System
// Clear messages with specific content
const deleted = await ModerationUtil.clearMessages(channel, 100, {
contains: 'spam'
});
// Clear messages from specific user
const userDeleted = await ModerationUtil.clearMessages(channel, 50, {
user: targetUser
});
Interaction Examples
Advanced Poll System
const { InteractionUtil } = require('@zerrodevs/discord-bot-utils');
// Create a timed poll
async function createTimedPoll(channel, duration = 3600000) { // 1 hour
const poll = InteractionUtil.createPoll(
'What\'s your favorite programming language?',
['JavaScript', 'Python', 'Java', 'C++']
);
const message = await channel.send({
embeds: [poll.embed],
components: poll.components
});
const votes = new Map();
const userVotes = new Map();
const collector = message.createMessageComponentCollector({
time: duration
});
collector.on('collect', async interaction => {
// Handle vote logic
});
collector.on('end', async () => {
await message.edit({
content: '🔒 Poll has ended!',
components: []
});
});
}
Logging Examples
Comprehensive Logging Setup
const { LoggerUtil } = require('@zerrodevs/discord-bot-utils');
// Initialize logger
await LoggerUtil.initialize({
logDirectory: 'logs',
webhookUrl: process.env.LOG_WEBHOOK_URL,
errorWebhookUrl: process.env.ERROR_WEBHOOK_URL,
maxLogAge: 7 // days
});
// Log different types of events
await LoggerUtil.log('info', 'Bot started successfully');
await LoggerUtil.log('warning', 'Rate limit approaching');
await LoggerUtil.error(new Error('Database connection failed'));
// Command logging wrapper
function logCommand(command) {
return async (interaction) => {
try {
await command(interaction);
await LoggerUtil.log('info', `Command executed: ${interaction.commandName}`, {
user: interaction.user.tag,
guild: interaction.guild?.name
});
} catch (error) {
await LoggerUtil.error(error, {
command: interaction.commandName,
user: interaction.user.tag
});
}
};
}
Complete Bot Example
Check out our GitHub repository for complete bot examples including:
- Moderation bot with auto-moderation
- Interactive poll system
- Logging implementation
- Command handling system