In this page, some very basic, frequently-asked questions are answered. It's important to understand that these examples are generic and will most likely not work if you just copy/paste them in your code. You need to understand these lines, not just blindly shove them in your code.
// Set the bot's "Playing: " status (must be in an event!)client.on("ready", () => {client.user.setActivity("my code", { type: "WATCHING"})})
// Set the bot's online/idle/dnd/invisible statusclient.on("ready", () => {client.user.setStatus("online");});
// Set the bot's presence (activity and status)client.on("ready", () => {client.user.setPresence({activity: {name: 'my code',type: 'WATCHING'},status: 'idle'})})
Note: You can find a list of all possible activity types here.
If you want your bot show up as "streaming" you need to provide a twitch URL in the options object (for setActivity) or game.url
(for setPresence) alongside with the activity type "STREAMING". Streaming non-twitch URLs is currently not supported by the Discord API.
client.on("ready", () => {client.user.setActivity("my code", { type: "STREAMING", url: "https://www.twitch.tv/something" })})
In these examples Guild
is a placeholder for where you get the guild. This can be message.guild
or member.guild
or just guild
depending on the event. Or, you can get the guild by ID (see next section) and use that, too!
// Get a User by IDclient.users.cache.get("user id here");// Returns <User>
// Get a Member by IDmessage.guild.members.cache.get("user ID here");// Returns <Member>
// Get a Member from message Mentionmessage.mentions.members.first();// Returns <Member>
// Send a Direct Message to a usermessage.author.send("hello");// With Member it works too:message.member.send("Heya!");
// Mention a user in a messagemessage.channel.send(`Hello ${user}, and welcome!`);// ormessage.channel.send("Hello " + message.author.toString() + ", and welcome!");
// Restrict a command to a specific user by IDif (message.content.startsWith(prefix + 'commandname')) {if (message.author.id !== 'A user ID') return;// Your Command Here}
message.guild.members.fetch(message.author).then(member => {// The member is available here.});
// Get a Guild by IDclient.guilds.cache.get("the guild id");// Returns <Guild>
// Get a Channel by IDclient.channels.cache.get("the channel id");// Returns <Channel>
// Get a Channel by Namemessage.guild.channels.cache.find(channel => channel.name === "channel-name");// returns <Channel>
// Create an invite and send it in the channel// You can only create an invite from a GuildChannel// Messages can only be sent to a TextChannelmessage.guild.channels.cache.get('<CHANNEL ID>').createInvite().then(invite =>message.channel.send(invite.url));
As of 03/08/2017, there is no more Default Channel in guilds on Discord. The #general default channel can be deleted, and the guild.defaultChannel
property no longer works. As an alternative, for those really wanting to send to what "looks" like the default channel, here's a dirty workaround.
Note: you'll need to npm install long
and then var Long = require("long");
to use the below code.
const getDefaultChannel = (guild) => {// get "original" default channelif(guild.channels.cache.has(guild.id))return guild.channels.cache.get(guild.id)// Check for a "general" channel, which is often default chatconst generalChannel = guild.channels.cache.find(channel => channel.name === "general");if (generalChannel)return generalChannel;// Now we get into the heavy stuff: first channel in order where the bot can speak// hold on to your hats!return guild.channels.cache.filter(c => c.type === "text" &&c.permissionsFor(guild.client.user).has("SEND_MESSAGES")).sort((a, b) => a.position - b.position ||Long.fromString(a.id).sub(Long.fromString(b.id)).toNumber()).first();}// This is called as, for instance:client.on("guildMemberAdd", member => {const channel = getDefaultChannel(member.guild);channel.send(`Welcome ${member} to the server, wooh!`);});
It's very important to note that if the bot has admin perms, their "First writable channel" is the one on top. That could be Rules, Announcements, FAQs, whatever. So if the default channel was deleted and there's no general channel, you're going to annoy a lot of people.
Consider using Enmap for per-guild settings instead (example here) and let server admins choose a channel!
// Editing a message the bot sentmessage.channel.send("Test").then(sentMessage => sentMessage.edit("Blah"));// message now reads : "Blah"
// Fetching a message by ID (Discord.js versions 9 through 11)// note: you can line return right before a "dot" in JS, that is valid.message.channel.messages.fetch({around: "352292052538753025", limit: 1}).then(messages => {const fetchedMsg = messages.first(); // messages is a collection!// do something with itfetchedMsg.edit("This fetched message was edited");});