Skip to content

Commit dba0040

Browse files
authored
feat: Merge common-questions and Misc pages into FAQ (discordjs#501)
1 parent e5eb25f commit dba0040

File tree

5 files changed

+173
-215
lines changed

5 files changed

+173
-215
lines changed

guide/.vuepress/sidebar.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ module.exports = {
7474
{
7575
title: 'Popular Topics',
7676
children: [
77+
'/popular-topics/faq',
7778
'/popular-topics/embeds',
7879
'/popular-topics/errors',
7980
'/popular-topics/permissions',
@@ -83,8 +84,6 @@ module.exports = {
8384
'/popular-topics/partials',
8485
'/popular-topics/intents',
8586
'/popular-topics/canvas',
86-
'/popular-topics/common-questions',
87-
'/popular-topics/miscellaneous-examples',
8887
'/popular-topics/webhooks',
8988
'/popular-topics/audit-logs',
9089
],

guide/additional-info/rest-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ If you've followed the tutorial, you should have something like this:
179179
</discord-message>
180180
</div>
181181

182-
Now, let's just make this an [embed](/popular-topics/miscellaneous-examples.md#sending-an-embed).
182+
Now, let's just make this an [embed](/popular-topics/embeds.md).
183183

184184
We are also going to be defining a utility function at the top of our file so that our embed doesn't error when the field value is over 1024 characters. Here is a bit of code to do that:
185185

guide/popular-topics/common-questions.md renamed to guide/popular-topics/faq.md

Lines changed: 170 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# Common questions
1+
# Frequently asked Questions
22

33
## Legend
44

55
* `<client>` is a placeholder for the Client object, such as `const client = new Discord.Client();`.
66
* `<message>` is a placeholder for the Message object, such as `client.on('message', message => { ... });`.
77
* `<guild>` is a placeholder for the Guild object, such as `<message>.guild` or <branch version="12.x" inline>`<client>.guilds.cache.get('<id>')`</branch><branch version="11.x" inline>`<client>.guilds.get('<id>')`</branch>.
8+
* `<voiceChannel>` is a placeholder for the VoiceChannel object, such as <branch version="11.x" inline>`<message>.member.voiceChannel`</branch><branch version="12.x" inline>`<message>.member.voice.channel`</branch>
89

910
For a more detailed explanation on the notations commonly used in this guide, the docs, and the support server, see [here](/additional-info/notation.md).
1011

@@ -131,17 +132,17 @@ if (<message>.author.id === '<id>') {
131132
}
132133
```
133134

134-
## Bot Configuration
135+
## Bot Configuration and Utility
135136

136-
### How do I set my username?
137+
### How do I set my bot's username?
137138

138139
<!-- eslint-skip -->
139140

140141
```js
141142
<client>.user.setUsername('<username>');
142143
```
143144

144-
### How do I set my avatar?
145+
### How do I set my bot's avatar?
145146

146147
<!-- eslint-skip -->
147148

@@ -169,11 +170,11 @@ if (<message>.author.id === '<id>') {
169170
<branch version="11.x">
170171

171172
::: tip
172-
If you would like to set your activity upon startup, you must place the `<client>.user.setActivity()` method in a `ready` event listener (`<client>.on('ready', () => {});`).
173+
If you would like to set your activity upon startup, you must place the `<client>.user.setActivity()` method call inside a `ready` event listener (`<client>.on('ready', () => {});`).
173174
:::
174175

175176
::: warning
176-
`<client>.user.setActivity()` will only work in v11.3 and above. You can check your version with `npm ls discord.js` and update with `npm install discord.js`. You can still use `<client>.user.setGame()`, but it is deprecated as of v11.3, and has been removed in v12.
177+
`<client>.user.setActivity()` will only work in v11.3 and above. You can still use `<client>.user.setGame()`, but it is deprecated as of v11.3, and has been removed in v12.
177178
:::
178179

179180
</branch>
@@ -185,9 +186,72 @@ If you would like to set your activity upon startup, you can use the `ClientOpti
185186

186187
</branch>
187188

189+
### How do I make my bot display online/idle/dnd/invisible?
190+
191+
<!-- eslint-skip -->
192+
193+
```js
194+
<client>.user.setStatus('online');
195+
<client>.user.setStatus('idle');
196+
<client>.user.setStatus('dnd');
197+
<client>.user.setStatus('invisible');
198+
```
199+
200+
### How do I set both status and activity in one go?
201+
202+
<!-- eslint-skip -->
203+
204+
```js
205+
<client>.user.setPresence({ activity: { name: '<activity>' }, status: 'idle' });
206+
```
207+
208+
<branch version="11.x">
209+
210+
::: warning
211+
the `activity` key will only work in v11.3 and above. You can still use the `game` key instead, but it is deprecated as of v11.3, and has been removed in v12.
212+
:::
213+
214+
</branch>
215+
216+
### How do I add a mention prefix to my bot?
217+
218+
```js
219+
const Discord = require('discord.js');
220+
221+
const client = new Discord.Client();
222+
const prefix = '!';
223+
224+
const escapeRegex = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
225+
226+
client.on('message', message => {
227+
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(prefix)})\\s*`);
228+
if (!prefixRegex.test(message.content)) return;
229+
230+
const [, matchedPrefix] = message.content.match(prefixRegex);
231+
const args = message.content.slice(matchedPrefix.length).trim().split(/ +/);
232+
const command = args.shift().toLowerCase();
233+
234+
if (command === 'ping') {
235+
message.channel.send('Pong!');
236+
} else if (command === 'prefix') {
237+
message.reply(`you can either ping me or use \`${prefix}\` as my prefix.`);
238+
}
239+
});
240+
241+
client.login('your-token-goes-here');
242+
```
243+
244+
::: tip
245+
The `escapeRegex` function is used to convert special characters into literal characters by escaping them, so that they don't terminate the pattern within the [Regular Expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)!
246+
:::
247+
248+
::: tip
249+
If you aren't familiar with the syntax used on the `const [, matchedPrefix] = ...` line, that's called "array destructuring". Feel free to read more about it in the [ES6 syntax](/additional-info/es6-syntax.md#array-destructuring) guide!
250+
:::
251+
188252
## Miscellaneous
189253

190-
### How do I send a message to a certain channel?
254+
### How do I send a message to a specific channel?
191255

192256
<branch version="11.x">
193257

@@ -237,7 +301,7 @@ user.send('<content>');
237301
If you want to DM the user who sent the message, you can use `<message>.author.send()`.
238302
:::
239303

240-
### How do I tag a certain user in a message?
304+
### How do I mention a certain user in a message?
241305

242306
<!-- eslint-skip -->
243307

@@ -248,11 +312,7 @@ const user = <message>.mentions.users.first();
248312
```
249313

250314
::: tip
251-
If you want to tag the user who sent the message, you can use `<message>.reply()`. For example: `<message>.reply('hi.')` would result in `@User, hi.`. If you want to insert the tag elsewhere, you can store `<message>.author` as your `user` variable and use the original example.
252-
:::
253-
254-
::: tip
255-
Tags inside certain areas of an embed may display correctly, but will not actually ping the user. Tags inside other certain areas of an embed will display the raw string instead (e.g. `<@123456789012345678>`).
315+
Mentions in embeds may resolve correctly but will never notify the user. Some areas of embeds do not support mentions at all.
256316
:::
257317

258318
### How do I prompt the user for additional input?
@@ -297,7 +357,7 @@ Tags inside certain areas of an embed may display correctly, but will not actual
297357
</branch>
298358

299359
::: tip
300-
If you want to learn more about this syntax or want to learn about reaction collectors as well, check out [this dedicated guide page for collectors](/popular-topics/collectors.md)!
360+
If you want to learn more about this syntax or reaction collectors, check out [this dedicated guide page for collectors](/popular-topics/collectors.md)!
301361
:::
302362

303363
### How do I react to the message my bot sent?
@@ -315,7 +375,7 @@ If you want to learn more about this syntax or want to learn about reaction coll
315375
If you want to learn more about reactions, check out [this dedicated guide on reactions](/popular-topics/reactions.md)!
316376
:::
317377

318-
### How do I create a restart command?
378+
### How do I restart my bot with a command?
319379

320380
```js
321381
process.exit();
@@ -325,28 +385,22 @@ process.exit();
325385
`process.exit()` will only kill your Node process, but when using [PM2](http://pm2.keymetrics.io/), it will restart the process whenever it gets killed. You can read our guide on PM2 [here](/improving-dev-environment/pm2.md).
326386
:::
327387

328-
::: warning
329-
Be sure to [limit this to your own ID](/popular-topics/common-questions.md#how-do-i-limit-a-command-to-a-single-user) so that other users can't restart your bot!
330-
:::
331-
332388
### What is the difference between a User and a GuildMember?
333389

334-
A lot of users get confused as to what the difference between Users and GuildMembers is. The simple answer is that a User represents a global Discord user and a GuildMember represents a Discord user on a specific server. That means only GuildMembers can have permissions, roles, and nicknames, for example, because all of these things are server-bound information that could be different on each server that user is in.
335-
336-
### How do I find all online members?
390+
A User represents a global Discord user and a GuildMember represents a Discord user on a specific server. That means only GuildMembers can have permissions, roles, and nicknames, for example, because all of these things are server-bound information that could be different on each server that user is in.
337391

338-
Assuming the process is to be done for the guild the message is sent in.
392+
### How do I find all online members of a guild?
339393

340394
<branch version="11.x">
341395

342396
<!-- eslint-skip -->
343397

344398
```js
345399
// First we use fetchMembers to make sure all members are cached
346-
<message>.guild.fetchMembers().then(fetchedGuild => {
400+
<guild>.fetchMembers().then(fetchedGuild => {
347401
const totalOnline = fetchedGuild.members.filter(member => member.presence.status === 'online');
348402
// We now have a collection with all online member objects in the totalOnline variable
349-
<message>.channel.send(`There are currently ${totalOnline.size} members online in this guild!`);
403+
console.log(`There are currently ${totalOnline.size} members online in this guild!`)
350404
});
351405
```
352406

@@ -357,10 +411,10 @@ Assuming the process is to be done for the guild the message is sent in.
357411

358412
```js
359413
// First we use guild.members.fetch to make sure all members are cached
360-
<message>.guild.members.fetch().then(fetchedMembers => {
414+
<guild>.members.fetch().then(fetchedMembers => {
361415
const totalOnline = fetchedMembers.filter(member => member.presence.status === 'online');
362416
// We now have a collection with all online member objects in the totalOnline variable
363-
<message>.channel.send(`There are currently ${totalOnline.size} members online in this guild!`);
417+
console.log(`There are currently ${totalOnline.size} members online in this guild!`)
364418
});
365419
```
366420

@@ -404,3 +458,92 @@ Assuming the process is to be done for the guild the message is sent in.
404458
```
405459

406460
</branch>
461+
462+
### How do I play music from YouTube?
463+
464+
For this to work you need to have `ytdl-core` installed.
465+
466+
```bash
467+
npm install --save ytdl-core
468+
```
469+
470+
Additionally you may need the following:
471+
472+
```bash
473+
npm install --save @discordjs/opus # opus engine (if missing)
474+
sudo apt-get install ffmpeg # ffmpeg debian/ubuntu
475+
npm install ffmpeg-static # ffmpeg windows
476+
```
477+
478+
<branch version="11.x">
479+
480+
<!-- eslint-skip -->
481+
482+
```js
483+
// ...
484+
const ytdl = require('ytdl-core');
485+
486+
<voiceChannel>.join().then(connection => {
487+
const stream = ytdl('<youtubelink>', { filter: 'audioonly' });
488+
const dispatcher = connection.playStream(stream);
489+
490+
dispatcher.on('end', () => voiceChannel.leave());
491+
})
492+
493+
```
494+
495+
</branch>
496+
<branch version="12.x">
497+
498+
<!-- eslint-skip -->
499+
500+
```js
501+
// ...
502+
const ytdl = require('ytdl-core');
503+
504+
<voiceChannel>.join().then(connection => {
505+
const stream = ytdl('<youtubelink>', { filter: 'audioonly' });
506+
const dispatcher = connection.play(stream);
507+
508+
dispatcher.on('finish', () => voiceChannel.leave());
509+
})
510+
511+
```
512+
513+
</branch>
514+
515+
### Why do some emojis behave weirdly?
516+
517+
If you've tried using [the usual method of retrieving unicode emojis](/popular-topics/reactions.md#unicode-emojis), you may have noticed that some characters don't provide the expected results. Here's a short snippet that'll help with that issue. You can toss this into a file of its own and use it anywhere you need! Alternatively feel free to simply copy-paste the characters from below:
518+
519+
```js
520+
// emojiCharacters.js
521+
module.exports = {
522+
a: '🇦', b: '🇧', c: '🇨', d: '🇩',
523+
e: '🇪', f: '🇫', g: '🇬', h: '🇭',
524+
i: '🇮', j: '🇯', k: '🇰', l: '🇱',
525+
m: '🇲', n: '🇳', o: '🇴', p: '🇵',
526+
q: '🇶', r: '🇷', s: '🇸', t: '🇹',
527+
u: '🇺', v: '🇻', w: '🇼', x: '🇽',
528+
y: '🇾', z: '🇿', 0: '0️⃣', 1: '1️⃣',
529+
2: '2️⃣', 3: '3️⃣', 4: '4️⃣', 5: '5️⃣',
530+
6: '6️⃣', 7: '7️⃣', 8: '8️⃣', 9: '9️⃣',
531+
10: '🔟', '#': '#️⃣', '*': '*️⃣',
532+
'!': '', '?': '',
533+
};
534+
```
535+
536+
<!-- eslint-skip -->
537+
538+
```js
539+
// index.js
540+
const emojiCharacters = require('./emojiCharacters');
541+
542+
console.log(emojiCharacters.a); // 🇦
543+
console.log(emojiCharacters[10]); // 🔟
544+
console.log(emojiCharacters['!']); //
545+
```
546+
547+
::: tip
548+
On Windows, you may be able to use the `Win + .` keyboard shortcut to open up an emoji picker can be used for quick, easy access to all the unicode emojis available to you. Some of the emojis listed above may not be represented there, though (e.g the 0-9 emojis).
549+
:::

0 commit comments

Comments
 (0)