Update examples

This commit is contained in:
Crawl
2017-04-24 08:34:00 +02:00
parent b15e012788
commit bbb5d4c7a1
4 changed files with 36 additions and 35 deletions

View File

@@ -2,29 +2,29 @@
Send a user a link to their avatar Send a user a link to their avatar
*/ */
// import the discord.js module // Import the discord.js module
const Discord = require('discord.js'); const Discord = require('discord.js');
// create an instance of a Discord Client // Create an instance of a Discord Client
const client = new Discord.Client(); const client = new Discord.Client();
// the token of your bot - https://discordapp.com/developers/applications/me // The token of your bot - https://discordapp.com/developers/applications/me
const token = 'your bot token here'; const token = 'your bot token here';
// the ready event is vital, it means that your bot will only start reacting to information // The ready event is vital, it means that your bot will only start reacting to information
// from Discord _after_ ready is emitted. // from Discord _after_ ready is emitted
client.on('ready', () => { client.on('ready', () => {
console.log('I am ready!'); console.log('I am ready!');
}); });
// create an event listener for messages // Create an event listener for messages
client.on('message', message => { client.on('message', message => {
// if the message is "what is my avatar", // If the message is "what is my avatar"
if (message.content === 'what is my avatar') { if (message.content === 'what is my avatar') {
// send the user's avatar URL // Send the user's avatar URL
message.reply(message.author.avatarURL); message.reply(message.author.avatarURL);
} }
}); });
// log our bot in // Log our bot in
client.login(token); client.login(token);

View File

@@ -2,32 +2,33 @@
A bot that welcomes new guild members when they join A bot that welcomes new guild members when they join
*/ */
// import the discord.js module // Import the discord.js module
const Discord = require('discord.js'); const Discord = require('discord.js');
// create an instance of a Discord Client // Create an instance of a Discord Client
const client = new Discord.Client(); const client = new Discord.Client();
// the token of your bot - https://discordapp.com/developers/applications/me // The token of your bot - https://discordapp.com/developers/applications/me
const token = 'your bot token here'; const token = 'your bot token here';
// the ID of the channel in which the bot will greet new users // The ready event is vital, it means that your bot will only start reacting to information
const channelID = 'your channel ID here'; // from Discord _after_ ready is emitted
// the ready event is vital, it means that your bot will only start reacting to information
// from Discord _after_ ready is emitted.
client.on('ready', () => { client.on('ready', () => {
console.log('I am ready!'); console.log('I am ready!');
}); });
// create an event listener for new guild members // Create an event listener for new guild members
client.on('guildMemberAdd', member => { client.on('guildMemberAdd', member => {
// get the channel by its ID // Send the message, mentioning the member
const channel = client.channels.get(channelID); member.guild.defaultChannel.send(`Welcome to the server, ${member}!`);
// send the message, mentioning the member // If you want to send the message to a designated channel on a server instead
channel.send(`Welcome to the server, ${member}!`); // you can do the following:
const channel = member.guild.channels.find('name', 'member-log');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
channel.send(`Welcome to the server, ${member}`);
}); });
// log our bot in // Log our bot in
client.login(token); client.login(token);

View File

@@ -2,29 +2,29 @@
A ping pong bot, whenever you send "ping", it replies "pong". A ping pong bot, whenever you send "ping", it replies "pong".
*/ */
// import the discord.js module // Import the discord.js module
const Discord = require('discord.js'); const Discord = require('discord.js');
// create an instance of a Discord Client // Create an instance of a Discord Client
const client = new Discord.Client(); const client = new Discord.Client();
// the token of your bot - https://discordapp.com/developers/applications/me // The token of your bot - https://discordapp.com/developers/applications/me
const token = 'your bot token here'; const token = 'your bot token here';
// the ready event is vital, it means that your bot will only start reacting to information // The ready event is vital, it means that your bot will only start reacting to information
// from Discord _after_ ready is emitted. // from Discord _after_ ready is emitted
client.on('ready', () => { client.on('ready', () => {
console.log('I am ready!'); console.log('I am ready!');
}); });
// create an event listener for messages // Create an event listener for messages
client.on('message', message => { client.on('message', message => {
// if the message is "ping", // If the message is "ping"
if (message.content === 'ping') { if (message.content === 'ping') {
// send "pong" to the same channel. // Send "pong" to the same channel
message.channel.send('pong'); message.channel.send('pong');
} }
}); });
// log our bot in // Log our bot in
client.login(token); client.login(token);

View File

@@ -2,11 +2,11 @@
Send a message using a webhook Send a message using a webhook
*/ */
// import the discord.js module // Import the discord.js module
const Discord = require('discord.js'); const Discord = require('discord.js');
// create a new webhook // Create a new webhook
const hook = new Discord.WebhookClient('webhook id', 'webhook token'); const hook = new Discord.WebhookClient('webhook id', 'webhook token');
// send a message using the webhook // Send a message using the webhook
hook.send('I am now alive!'); hook.send('I am now alive!');