Discord Bot Snippets
WelcomeBot
Greets new members when they join the server.
client.on('guildMemberAdd', member => {
member.guild.channels.cache.find(c => c.name === 'welcome').send(`Welcome, ${member}!`);
});
PingBot
Replies with 'Pong!' when a user types !ping.
client.on('messageCreate', message => {
if (message.content === '!ping') {
message.reply('Pong!');
}
});
RoleAssigner
Assigns a default role to new users.
client.on('guildMemberAdd', member => {
const role = member.guild.roles.cache.find(r => r.name === 'Member');
if (role) member.roles.add(role);
});
ReactionRole
Adds a role when a user reacts to a message.
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.id === '123456789') {
const role = reaction.message.guild.roles.cache.find(r => r.name === 'Gamer');
if (role) reaction.message.guild.members.cache.get(user.id).roles.add(role);
}
});
BanBot
Bans users with a command (admin only).
client.on('messageCreate', message => {
if (message.content.startsWith('!ban') && message.member.permissions.has('BAN_MEMBERS')) {
const user = message.mentions.users.first();
if (user) message.guild.members.ban(user);
}
});
MusicBot
Joins voice and plays audio from a YouTube link.
// Requires discord.js and @discordjs/voice
// Plays music via yt-dl (not included here for brevity)
UptimeBot
Responds with how long the bot has been online.
const start = Date.now();
client.on('messageCreate', msg => {
if (msg.content === '!uptime') {
const uptime = Math.floor((Date.now() - start) / 1000);
msg.reply(`Uptime: ${uptime} seconds.`);
}
});
PollBot
Creates a quick yes/no poll.
client.on('messageCreate', message => {
if (message.content.startsWith('!poll')) {
message.react('👍');
message.react('👎');
}
});