Skip to content

Latest commit

 

History

History
200 lines (163 loc) · 6.13 KB

frequently-asked-questions.md

File metadata and controls

200 lines (163 loc) · 6.13 KB

Frequently Asked Questions

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", () => {
    client.user.setStatus("online");
});
// Set the bot's presence (activity and status)
client.on("ready", () => {
    client.user.setPresence({
        game: { 
            name: 'my code',
            type: 'WATCHING'
        },
        status: 'idle'
    })
})

Note: You can find a list of all possible acitvity types here.

{% hint style="info" %} 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. {% endhint %}

client.on("ready", () => {
    client.user.setActivity("my code", { type: "STREAMING", url: "https://www.twitch.tv/something" })
})

Users and Members

{% hint style="info" %} 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! {% endhint %}

// Get a User by ID
client.users.get("user id here");
// Returns <User>
// Get a Member by ID
message.guild.members.get("user ID here");
// Returns <Member>
// Get a Member from message Mention
message.mentions.members.first();
// Returns <Member>
// Send a Direct Message to a user
message.author.send("hello");
// With Member it works too:
message.member.send("Heya!");
// Mention a user in a message
message.channel.send(`Hello ${user}, and welcome!`);
// or
message.channel.send("Hello " + message.author.toString() + ", and welcome!");
// Restrict a command to a specific user by ID
if (message.content.startsWith(prefix + 'commandname')) {
    if (message.author.id !== 'A user ID') return;
    // Your Command Here
}
// FETCH a member. Useful if an invisible user sends a message.
message.guild.fetchMember(message.author)
  .then(member => {
    // The member is available here.
  });

// THIS CHANGES IN DISCORD VERSION 12!!!!!
message.guild.members.fetch(message.author)
  .then(member => {
    // The member is available here.
  });

Channels and Guilds

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

Default Channel

{% hint style="info" %} 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. {% endhint %}

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
  if(guild.channels.has(guild.id))
    return guild.channels.get(guild.id)

  // Check for a "general" channel, which is often default chat
  const generalChannel = guild.channels.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
   .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 writeable 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!

Messages

// Editing a message the bot sent
message.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.fetchMessages({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");
  });

// THIS CHANGES IN DISCORD VERSION 12!!!!!
message.channel.messages.fetch({around: "352292052538753025", limit: 1})
  .then(messages => {
    messages.first().edit("This fetched message was edited");
  });