0% found this document useful (0 votes)
15 views5 pages

Message

This document is a Discord bot implementation using discord.js that includes various commands for moderation, utility, messaging, giveaways, and more. The bot responds to commands prefixed with '&', providing functionalities such as mute, kick, ban, warn, and giveaway management. It also features a help command that lists available commands and their descriptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Message

This document is a Discord bot implementation using discord.js that includes various commands for moderation, utility, messaging, giveaways, and more. The bot responds to commands prefixed with '&', providing functionalities such as mute, kick, ban, warn, and giveaway management. It also features a help command that lists available commands and their descriptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

const { Client, GatewayIntentBits, Partials, EmbedBuilder, PermissionsBitField } =

require('discord.js');
const ms = require('ms');

const client = new Client({


intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
],
partials: [Partials.Channel]
});

const prefix = '&';

client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
client.user.setActivity('your server', { type: 0 });
});

// Help Command and Command Handler


client.on('messageCreate', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;

const args = message.content.slice(prefix.length).trim().split(/ +/);


const command = args.shift().toLowerCase();

// Help Command
if (command === 'help') {
const embed = new EmbedBuilder()
.setTitle('Bot Command Help')
.setColor('Blue')
.setDescription('Here are the available commands:')
.addFields(
{ name: 'Moderation', value: '`ban`, `kick`, `mute`, `unmute`, `warn`,
`lock`, `unlock`, `purge`, `slowmode`' },
{ name: 'Utility', value: '`userinfo`, `serverinfo`, `avatar`, `addrole`,
`removerole`' },
{ name: 'Messaging', value: '`announce`, `dm`, `spam`' },
{ name: 'Giveaways', value: '`giveaway`, `end`, `reroll`' },
{ name: 'Fun/Other', value: '`profile`, `status`, `activity`' }
)
.setFooter({ text: 'Use &<command> to use a command.' });

message.channel.send({ embeds: [embed] });


return;
}

// Mute Command
if (command === 'mute') {
const member = message.mentions.members.first();
if (!member) return message.reply('Please mention a user to mute!');
const duration = args[1];
if (!duration) return message.reply('Please specify a duration!');

const muteRole = message.guild.roles.cache.find(role => role.name === 'Muted');


if (!muteRole) return message.reply('Muted role not found.');
await member.roles.add(muteRole);
message.reply(`${member.user.tag} has been muted for ${duration}.`);
setTimeout(() => {
member.roles.remove(muteRole);
message.channel.send(`${member.user.tag} has been unmuted.`);
}, ms(duration));
return;
}

// Kick Command
if (command === 'kick') {
const member = message.mentions.members.first();
if (!member) return message.reply('Please mention a user to kick!');
await member.kick();
message.reply(`${member.user.tag} has been kicked.`);
return;
}

// Ban Command
if (command === 'ban') {
const member = message.mentions.members.first();
if (!member) return message.reply('Please mention a user to ban!');
await member.ban();
message.reply(`${member.user.tag} has been banned.`);
return;
}

// Unmute Command
if (command === 'unmute') {
const member = message.mentions.members.first();
if (!member) return message.reply('Please mention a user to unmute!');
const muteRole = message.guild.roles.cache.find(role => role.name === 'Muted');
if (!muteRole) return message.reply('Muted role not found.');
await member.roles.remove(muteRole);
message.reply(`${member.user.tag} has been unmuted.`);
return;
}

// Warn Command
if (command === 'warn') {
const member = message.mentions.members.first();
if (!member) return message.reply('Please mention a user to warn!');
const reason = args.slice(1).join(' ') || 'No reason specified';
message.reply(`${member.user.tag} has been warned. Reason: ${reason}`);
return;
}

// Lock Command
if (command === 'lock') {
const channel = message.channel;
await channel.permissionOverwrites.edit(message.guild.roles.everyone,
{ SEND_MESSAGES: false });
message.reply('The channel has been locked.');
return;
}

// Unlock Command
if (command === 'unlock') {
const channel = message.channel;
await channel.permissionOverwrites.edit(message.guild.roles.everyone,
{ SEND_MESSAGES: true });
message.reply('The channel has been unlocked.');
return;
}

// Purge Command
if (command === 'purge') {
const amount = parseInt(args[0]);
if (isNaN(amount) || amount < 1 || amount > 100) return message.reply('Please
specify a valid number of messages to delete (1-100).');
await message.channel.bulkDelete(amount);
message.reply(`${amount} messages have been deleted.`);
return;
}

// Slowmode Command
if (command === 'slowmode') {
const time = parseInt(args[0]);
if (isNaN(time) || time < 1) return message.reply('Please specify a valid time
in seconds.');
await message.channel.setRateLimitPerUser(time);
message.reply(`Slowmode has been set to ${time} seconds.`);
return;
}

// Messaging Commands
if (command === 'announce') {
if (!message.member.permissions.has(PermissionsBitField.Flags.ManageMessages))
{
return message.reply('You need the "Manage Messages" permission to use this
command.');
}
const channel = message.mentions.channels.first();
const announcement = args.slice(1).join(' ');
if (!channel || !announcement) {
return message.reply('Please mention a channel and provide a message.');
}
const embed = new EmbedBuilder()
.setTitle('Announcement')
.setDescription(announcement)
.setColor('Gold')
.setTimestamp();
channel.send({ embeds: [embed] });
return;
}

if (command === 'dm') {


const user = message.mentions.users.first();
const dmMessage = args.slice(1).join(' ');
if (!user || !dmMessage) {
return message.reply('Please mention a user and provide a message to send.');
}
try {
await user.send(dmMessage);
message.reply(`Successfully sent the message to ${user.tag}`);
} catch (error) {
message.reply('I could not DM this user. They may have DMs disabled.');
}
return;
}

if (command === 'spam') {


const count = parseInt(args[0]);
const spamMessage = args.slice(1).join(' ');
if (isNaN(count) || count <= 0 || count > 100) {
return message.reply('Please provide a valid number between 1 and 100 for the
spam count.');
}
if (!spamMessage) {
return message.reply('Please provide a message to spam.');
}
for (let i = 0; i < count; i++) {
message.channel.send(spamMessage);
}
return;
}

if (command === 'giveaway') {


console.log('Giveaway command triggered');
const duration = args[0];
const winnersCount = parseInt(args[1]);
const prize = args.slice(2).join(' ');

if (!duration || !winnersCount || !prize) {


return message.reply('Usage: !giveaway <duration s/m/h/d> <winners>
<prize>');
}

const ms = require('ms');
const time = ms(duration); // Convert the duration into milliseconds

if (!time) {
return message.reply('Invalid duration format. Please use s, m, h, or d.');
}

const embed = new EmbedBuilder()


.setTitle('🎉 Giveaway 🎉')
.setDescription(`Prize: **${prize}**\nReact with 🎉 to enter!\nEnds in: $
{duration}`)
.setFooter({ text: `Hosted by ${message.author.tag}` })
.setColor('Gold');

// Send the giveaway embed and react with 🎉


const msg = await message.channel.send({ embeds: [embed] });
await msg.react('🎉');

// Set timeout for the giveaway to end after the specified duration
setTimeout(async () => {
const reaction = msg.reactions.cache.get('🎉');
if (!reaction) return message.channel.send('No reactions found.');

const users = await reaction.users.fetch(); // Fetch all users who reacted


const filtered = users.filter(u => !u.bot); // Filter out bot users

if (filtered.size === 0) {
return message.channel.send('No valid entries.');
}
const winners = filtered.random(winnersCount); // Randomly pick winners
from filtered users
message.channel.send(`🎉 Winner(s): ${winners.map(w => w.toString()).join(',
')}\nPrize: ${prize}`);
}, time); // End the giveaway after the specified duration
} // Close the 'giveaway' command block

if (command === 'reroll') {


const msgID = args[0];
try {
const fetched = await message.channel.messages.fetch(msgID);
const reaction = fetched.reactions.cache.get('🎉');
const users = await reaction.users.fetch();
const filtered = users.filter(u => !u.bot);
if (filtered.size === 0) return message.channel.send('No valid entries.');
const winner = filtered.random();
message.channel.send(`🎉 New winner: ${winner}`);
} catch {
message.channel.send('Message not found or invalid.');
}
}
});

client.login(''); //token of your bot

You might also like