discord bot
// Import the discord.js module
const Discord = require('discord.js');
// Create an instance of a Discord client
const client = new Discord.Client();
/**
* The ready event is vital, it means that only _after_ this will your bot start reacting to information
* received from Discord
*/
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', message => {
// Ignore messages that aren't from a guild
if (!message.guild) return;
// if the message content starts with "!ban"
if (message.content.startsWith('!ban')) {
// Assuming we mention someone in the message, this will return the user
// Read more about mentions over at https://discord.js.org/#/docs/main/master/class/MessageMentions
const user = message.mentions.users.first();
// If we have a user mentioned
if (user) {
// Now we get the member from the user
const member = message.guild.members.resolve(user);
// If the member is in the guild
if (member) {
/**
* Ban the member
* Make sure you run this on a member, not a user!
* There are big differences between a user and a member
* Read more about what ban options there are over at
* https://discord.js.org/#/docs/main/master/class/GuildMember?scrollTo=ban
*/
member
.ban({
reason: 'They were bad!',
})
.then(() => {
// We let the message author know we were able to ban the person
message.channel.send(`Successfully banned ${user.tag}`);
})
.catch(err => {
// An error happened
// This is generally due to the bot not being able to ban the member,
// either due to missing permissions or role hierarchy
message.channel.send('I was unable to ban the member');
// Log the error
console.error(err);
});
} else {
// The mentioned user isn't in this guild
message.channel.send("That user isn't in this guild!");
}
} else {
// Otherwise, if no user was mentioned
message.channel.send("You didn't mention the user to ban!");
}
}
});
// Log our bot in using the token from https://discord.com/developers/applications
client.login('your token here');
4.5
2
import discord
from discord.ext import commands
client = commands.Bot(command_prefix ='')
@client.event
async def on_ready(): #Bot on
print ("Player One, Ready")
@client.event
async def on_message(ctx):
#code
client.run('TOKEN')
Thank you!
2
0
3.86
7
use Discord4J
Thank you!
7
0
3.86
6
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
});
client.login('token');
Thank you!
6
0
4.5
4
python is a good language to learn for discord bots
Thank you!
4
0
4.67
3
Invite your bot: https://discord.com/oauth2/authorize?client_id=BOT_ID_HERE&scope=bot
Thank you!
3
0
Are there any code examples left?
New code examples in category Java