|
| 1 | +# Frequently Asked Questions |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Code Examples |
| 6 | + |
| 7 | +## Bot and Bot Client |
| 8 | + |
| 9 | +```javascript |
| 10 | +// Set the bot's "Playing: " status (must be in an event!) |
| 11 | +client.on("ready", () => { |
| 12 | + client.user.setActivity("my code", { type: "WATCHING"}) |
| 13 | +}) |
| 14 | +``` |
| 15 | + |
| 16 | +```javascript |
| 17 | +// Set the bot's online/idle/dnd/invisible status |
| 18 | +client.on("ready", () => { |
| 19 | + client.user.setStatus("online"); |
| 20 | +}); |
| 21 | +``` |
| 22 | + |
| 23 | +```javascript |
| 24 | +// Set the bot's presence (activity and status) |
| 25 | +client.on("ready", () => { |
| 26 | + client.user.setPresence({ |
| 27 | + activities: [{ |
| 28 | + name: "my code", |
| 29 | + type: "WATCHING" |
| 30 | + }], |
| 31 | + status: "idle" |
| 32 | + }) |
| 33 | +}) |
| 34 | +``` |
| 35 | + |
| 36 | +Note: You can find a list of all possible activity types [here](https://discord.js.org/#/docs/main/stable/typedef/ActivityType). |
| 37 | + |
| 38 | +If you want your bot's status to show up as `STREAMING`, you need to provide a Twitch or YouTube URL. |
| 39 | +For `setActivity`, you need to provide an options object, which needs to have the URL, and the type should be set to streaming. |
| 40 | +For `setPresence`, you need to provide the presence data object, which needs to contain the activities array, with the url and type \(Set it to "STREAMING"\). |
| 41 | + |
| 42 | +```javascript |
| 43 | +client.on("ready", () => { |
| 44 | + client.user.setActivity("my code", { type: "STREAMING", url: "https://www.twitch.tv/DiamondLions" }) |
| 45 | +}) |
| 46 | +``` |
| 47 | + |
| 48 | +## Users and Members |
| 49 | + |
| 50 | +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! |
| 51 | + |
| 52 | +```javascript |
| 53 | +// Get a User by ID |
| 54 | +client.users.cache.get("user id here"); |
| 55 | +// Returns <User> |
| 56 | +``` |
| 57 | + |
| 58 | +```javascript |
| 59 | +// Get a Member by ID |
| 60 | +message.guild.members.cache.get("user ID here"); |
| 61 | +// Returns <Member> |
| 62 | +``` |
| 63 | + |
| 64 | +```javascript |
| 65 | +// Get a Member from message Mention |
| 66 | +message.mentions.members.first(); |
| 67 | +// Returns <Member>, if there is a mentioned member |
| 68 | +``` |
| 69 | + |
| 70 | +```javascript |
| 71 | +// Send a Direct Message to a user |
| 72 | +message.author.send("hello"); |
| 73 | +// With Member it works too: |
| 74 | +message.member.send("Heya!"); |
| 75 | +``` |
| 76 | + |
| 77 | +```javascript |
| 78 | +// Mention a user in a message |
| 79 | +message.channel.send(`Hello ${message.author.toString()}, and welcome!`); |
| 80 | +// or |
| 81 | +message.channel.send("Hello " + message.author.toString() + ", and welcome!"); |
| 82 | +``` |
| 83 | + |
| 84 | +```javascript |
| 85 | +// Restrict a command to a specific user by ID |
| 86 | +if (message.content.startsWith(`${prefix}commandname`)) { |
| 87 | + if (message.author.id !== "A user ID") return; |
| 88 | + // Your Command Here |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +```javascript |
| 93 | +message.guild.members.fetch(message.author) |
| 94 | + .then(member => { |
| 95 | + // The member is available here. |
| 96 | + }) |
| 97 | + .catch(() => { |
| 98 | + // When an error occurs. |
| 99 | + }) |
| 100 | +``` |
| 101 | + |
| 102 | +## Channels and Guilds |
| 103 | + |
| 104 | +```javascript |
| 105 | +// Get a Guild by ID |
| 106 | +client.guilds.cache.get("the guild id"); |
| 107 | +// Returns <Guild> |
| 108 | +``` |
| 109 | + |
| 110 | +```javascript |
| 111 | +// Get a Channel by ID |
| 112 | +client.channels.cache.get("the channel id"); |
| 113 | +// Returns <Channel> |
| 114 | +``` |
| 115 | + |
| 116 | +```javascript |
| 117 | +// Get a Channel by Name |
| 118 | +message.guild.channels.cache.find(channel => channel.name === "channel-name"); |
| 119 | +// returns <Channel> |
| 120 | +``` |
| 121 | + |
| 122 | +```javascript |
| 123 | +// Create an invite and send it in the channel |
| 124 | +// You can only create an invite from a GuildChannel |
| 125 | +// Messages can only be sent to a TextChannel |
| 126 | +message.guild.channels.cache.get('<CHANNEL ID>').createInvite().then(invite => |
| 127 | + message.channel.send(invite.url) |
| 128 | +); |
| 129 | +``` |
| 130 | + |
| 131 | +## Messages |
| 132 | + |
| 133 | +```javascript |
| 134 | +// Editing a message the bot sent |
| 135 | +message.channel.send("Test").then(sentMessage => sentMessage.edit("Blah")); |
| 136 | +// message now reads : "Blah" |
| 137 | +``` |
| 138 | + |
| 139 | +```javascript |
| 140 | +message.channel.messages.fetch("352292052538753025") |
| 141 | + .then(message => { |
| 142 | + // do something with it |
| 143 | + // Check if the author of the message is the bot |
| 144 | + if (message.client.user.id !== message.id) return console.log("I'm not the author of that message!"); |
| 145 | + // Edit the message |
| 146 | + message.edit("This fetched message was edited"); |
| 147 | + }); |
| 148 | +``` |
0 commit comments