Frequently Asked Questions

Some very quick code samples for common actions on the bot, users, members, channels and messages!

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.

Code Examples

Bot and Bot Client

// 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 status
client.on("ready", () => {
// Set the bot's presence (activity and status)
client.on("ready", () => {
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: "" })

Users and Members

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 ID
client.users.cache.get("user id here");
// Returns <User>
// Get a Member by ID
message.guild.members.cache.get("user ID here");
// Returns <Member>
// Get a Member from message Mention
// Returns <Member>
// Send a Direct Message to a user"hello");
// With Member it works too:
// Mention a user in a message`Hello ${user}, and welcome!`);
// or"Hello " + + ", and welcome!");
// Restrict a command to a specific user by ID
if (message.content.startsWith(prefix + 'commandname')) {
if ( !== 'A user ID') return;
// Your Command Here
.then(member => {
// The member is available here.

Channels and Guilds

// Get a Guild by ID
client.guilds.cache.get("the guild id");
// Returns <Guild>
// Get a Channel by ID
client.channels.cache.get("the channel id");
// Returns <Channel>
// Get a Channel by Name
message.guild.channels.cache.find(channel => === "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 TextChannel
message.guild.channels.cache.get('<CHANNEL ID>').createInvite().then(invite =>

Default Channel

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 channel
return guild.channels.cache.get(
// Check for a "general" channel, which is often default chat
const generalChannel = guild.channels.cache.find(channel => === "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" &&
.sort((a, b) => a.position - b.position ||
// 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 sent"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.{around: "352292052538753025", limit: 1})
.then(messages => {
const fetchedMsg = messages.first(); // messages is a collection!
// do something with it
fetchedMsg.edit("This fetched message was edited");