mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 10:03:31 +01:00
feat: Utilise create-discord-bot (#10013)
* feat: utilise create-discord-bot * chore: hide line numbers * feat: add intents page * feat: add more Node.js variants * refactor: redo page a bit * fix: 👀 * chore: touch up introduction page * chore: touch up what's new * chore: touch up how to contribute * chore: remove enforced locale * chore: Fix typo Co-authored-by: Danial Raza <danialrazafb@gmail.com> * chore: commit suggestions Co-authored-by: Souji <timoqueezle@gmail.com> * chore: address improper capitalisation Co-authored-by: Souji <timoqueezle@gmail.com> * refactor: remove `applications.commands` * refactor: remove unique comment * fix(intents): remove shard comment * docs(intents): add missing info --------- Co-authored-by: Danial Raza <danialrazafb@gmail.com> Co-authored-by: Souji <timoqueezle@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,496 @@
|
||||
---
|
||||
title: Frequently asked questions
|
||||
category: Topics
|
||||
---
|
||||
|
||||
# Frequently asked questions
|
||||
|
||||
## Legend
|
||||
|
||||
- _`client`_ is a placeholder for the <DocsLink type="class" parent="Client" /> object:
|
||||
_`const client = new Client({ intents: [GatewayIntentBits.Guilds] });`_.
|
||||
|
||||
- _`interaction`_ is a placeholder for the <DocsLink type="class" parent="BaseInteraction" />:
|
||||
_`client.on(Events.InteractionCreate, interaction => { ... });`_.
|
||||
|
||||
- _`guild`_ is a placeholder for the <DocsLink type="class" parent="Guild" /> object:
|
||||
_`interaction.guild`_ or _`client.guilds.cache.get('id')`_
|
||||
|
||||
- _`voiceChannel`_ is a placeholder for the <DocsLink type="class" parent="VoiceChannel" />:
|
||||
_`interaction.member.voice.channel`_.
|
||||
|
||||
For a more detailed explanation of the notations commonly used in this guide, the docs, and the support server, see [here](/additional-info/notation.md).
|
||||
|
||||
## Administrative
|
||||
|
||||
### How do I ban a user?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const user = interaction.options.getUser('target');
|
||||
await guild.members.ban(user);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I unban a user?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const user = interaction.options.getUser('target');
|
||||
await guild.members.unban(user);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
Discord validates and resolves user ids for users not on the server in user slash command options. To retrieve and use
|
||||
the full structure from the resulting interaction, you can use the{' '}
|
||||
<DocsLink type="class" parent="CommandInteractionOptionResolver" symbol="getUser" brackets /> method.
|
||||
</Alert>
|
||||
|
||||
### How do I kick a guild member?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const member = interaction.options.getMember('target');
|
||||
await member.kick();
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I timeout a guild member?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const member = interaction.options.getMember('target');
|
||||
await member.timeout(60_000); // Timeout for one minute
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
Timeout durations are measured by the millisecond. The maximum timeout duration you can set is 28 days. To remove a
|
||||
timeout set on a member, pass _`null`_ instead of a timeout duration.
|
||||
</Alert>
|
||||
|
||||
### How do I add a role to a guild member?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const role = interaction.options.getRole('role');
|
||||
const member = interaction.options.getMember('target');
|
||||
await member.roles.add(role);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I check if a guild member has a specific role?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const role = interaction.options.getRole('role');
|
||||
const member = interaction.options.getMember('target');
|
||||
|
||||
if (member.roles.cache.has(role.id) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I limit a command to a single user?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
if (interaction.user.id === 'id') {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Bot Configuration and Utility
|
||||
|
||||
### How do I set my bot's username?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
await client.user.setUsername('username');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I set my bot's avatar?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
await client.user.setAvatar('URL or path');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I set my playing status?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
client.user.setActivity('activity');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I set my status to "Watching/Listening to/Competing in ..."?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { ActivityType } from 'discord.js';
|
||||
|
||||
client.user.setActivity('activity', { type: ActivityType.Watching });
|
||||
client.user.setActivity('activity', { type: ActivityType.Listening });
|
||||
client.user.setActivity('activity', { type: ActivityType.Competing });
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
If you would like to set your activity upon startup, you can use the{' '}
|
||||
<DocsLink type="typedef" parent="ClientOptions" /> object to set the appropriate
|
||||
<DocsLink type="typedef" parent="PresenceData" />.
|
||||
</Alert>
|
||||
|
||||
### How do I make my bot display online/idle/dnd/invisible?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
client.user.setStatus('online');
|
||||
client.user.setStatus('idle');
|
||||
client.user.setStatus('dnd');
|
||||
client.user.setStatus('invisible');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I set both status and activity in one go?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
client.user.setPresence({ activities: [{ name: 'activity' }], status: 'idle' });
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Miscellaneous
|
||||
|
||||
### How do I send a message to a specific channel?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const channel = client.channels.cache.get('id');
|
||||
await channel.send('content');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I create a post in a forum channel?
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
Currently, the only way to get tag ids is programmatically through{' '}
|
||||
<DocsLink type="class" parent="ForumChannel" symbol="availableTags" />.
|
||||
</Alert>
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const channel = client.channels.cache.get('id');
|
||||
|
||||
await channel.threads.create({
|
||||
name: 'Post name',
|
||||
message: { content: 'Message content' },
|
||||
appliedTags: ['tagId', 'anotherTagId'],
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I DM a specific user?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
await client.users.send('id', 'content');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
If you want to send a direct message to the user who sent the interaction, you can use _`interaction.user.send()`_.
|
||||
</Alert>
|
||||
|
||||
### How do I mention a specific user in a message?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const user = interaction.options.getUser('target');
|
||||
await interaction.reply(`Hi, ${user}.`);
|
||||
await interaction.followUp(`Hi, <@${user.id}>.`);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
Mentions in embeds may resolve correctly in embed titles, descriptions and field values but will never notify the
|
||||
user. Other areas do not support mentions at all.
|
||||
</Alert>
|
||||
|
||||
### How do I control which users and/or roles are mentioned in a message?
|
||||
|
||||
Controlling which mentions will send a ping is done via the _`allowedMentions`_ option, which replaces _`disableMentions`_.
|
||||
|
||||
This can be set as a default in <DocsLink type="typedef" parent="ClientOptions" />, and controlled per-message sent by your bot.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
new Client({ allowedMentions: { parse: ['users', 'roles'] } });
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
Even more control can be achieved by listing specific _`users`_ or _`roles`_ to be mentioned by id, e.g.:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
await channel.send({
|
||||
content: '<@123456789012345678> <@987654321098765432> <@&102938475665748392>',
|
||||
allowedMentions: { users: ['123456789012345678'], roles: ['102938475665748392'] },
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I prompt the user for additional input?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
await interaction.reply('Please enter more input.');
|
||||
const filter = (m) => interaction.user.id === m.author.id;
|
||||
|
||||
try {
|
||||
const messages = await interaction.channel.awaitMessages({ filter, time: 60000, max: 1, errors: ['time'] });
|
||||
await interaction.followUp(`You've entered: ${messages.first().content}`);
|
||||
} catch {
|
||||
await interaction.followUp('You did not enter any input!');
|
||||
}
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
If you want to learn more about this syntax or other types of collectors, check out [this dedicated guide page for
|
||||
collectors](/popular-topics/collectors.md)!
|
||||
</Alert>
|
||||
|
||||
### How do I block a user from using my bot?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const blockedUsers = ['id1', 'id2'];
|
||||
|
||||
client.on(Events.InteractionCreate, (interaction) => {
|
||||
if (blockedUsers.includes(interaction.user.id)) return;
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
You do not need to have a constant local variable like _`blockedUsers`_ above. If you have a database system that you
|
||||
use to store ids of blocked users, you can query the database instead.
|
||||
</Alert>
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
client.on(Events.InteractionCreate, async (interaction) => {
|
||||
const blockedUsers = await database.query('SELECT user_id FROM blocked_users;');
|
||||
if (blockedUsers.includes(interaction.user.id)) return;
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
Note that this is just a showcase of how you could do such a check.
|
||||
|
||||
### How do I react to the message my bot sent?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const sentMessage = await interaction.channel.send('My message to react to.');
|
||||
// Unicode emoji
|
||||
await sentMessage.react('👍');
|
||||
|
||||
// Custom emoji
|
||||
await sentMessage.react('123456789012345678');
|
||||
await sentMessage.react('<emoji:123456789012345678>');
|
||||
await sentMessage.react('<a:emoji:123456789012345678>');
|
||||
await sentMessage.react('emoji:123456789012345678');
|
||||
await sentMessage.react('a:emoji:123456789012345678');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
If you want to learn more about reactions, check out [this dedicated guide on
|
||||
reactions](/popular-topics/reactions.md)!
|
||||
</Alert>
|
||||
|
||||
### How do I restart my bot with a command?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
process.exit();
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Warning" type="warning">
|
||||
_`process.exit()`_ will only kill your Node process, but when using [PM2](https://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).
|
||||
</Alert>
|
||||
|
||||
### What is the difference between a User and a GuildMember?
|
||||
|
||||
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 the user is in.
|
||||
|
||||
### How do I find all online members of a guild?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
// First use guild.members.fetch to make sure all members are cached
|
||||
const fetchedMembers = await guild.members.fetch({ withPresences: true });
|
||||
const totalOnline = fetchedMembers.filter((member) => member.presence?.status === 'online');
|
||||
// Now you have a collection with all online member objects in the totalOnline variable
|
||||
console.log(`There are currently ${totalOnline.size} members online in this guild!`);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Warning" type="warning">
|
||||
This only works correctly if you have the _`GuildPresences`_ intent enabled for your application and client. If you
|
||||
want to learn more about intents, check out [this dedicated guide on intents](/popular-topics/intents.md)!
|
||||
</Alert>
|
||||
|
||||
### How do I check which role was added/removed and for which member?
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
// Start by declaring a guildMemberUpdate listener
|
||||
// This code should be placed outside of any other listener callbacks to prevent listener nesting
|
||||
client.on(Events.GuildMemberUpdate, (oldMember, newMember) => {
|
||||
// If the role(s) are present on the old member object but no longer on the new one (i.e role(s) were removed)
|
||||
const removedRoles = oldMember.roles.cache.filter((role) => !newMember.roles.cache.has(role.id));
|
||||
|
||||
if (removedRoles.size > 0) {
|
||||
console.log(`The roles ${removedRoles.map((r) => r.name)} were removed from ${oldMember.displayName}.`);
|
||||
}
|
||||
|
||||
// If the role(s) are present on the new member object but are not on the old one (i.e role(s) were added)
|
||||
const addedRoles = newMember.roles.cache.filter((role) => !oldMember.roles.cache.has(role.id));
|
||||
|
||||
if (addedRoles.size > 0) {
|
||||
console.log(`The roles ${addedRoles.map((r) => r.name)} were added to ${oldMember.displayName}.`);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### How do I check the bot's ping?
|
||||
|
||||
There are two common measurements for bot pings. The first, **websocket heartbeat**, is the average interval of a regularly sent signal indicating the healthy operation of the websocket connection the library receives events over:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
await interaction.reply(`Websocket heartbeat: ${client.ws.ping}ms.`);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
If you're using [sharding](/sharding/), a specific shard's heartbeat can be found on the WebSocketShard instance,
|
||||
accessible at _`client.ws.shards.get(id).ping`_.
|
||||
</Alert>
|
||||
|
||||
The second, **Roundtrip Latency**, describes the amount of time a full API roundtrip (from the creation of the command message to the creation of the response message) takes. You then edit the response to the respective value to avoid needing to send yet another message:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true });
|
||||
await interaction.editReply(`Roundtrip latency: ${sent.createdTimestamp - interaction.createdTimestamp}ms`);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### Why do some emojis behave weirdly?
|
||||
|
||||
If you've tried using [the usual method of retrieving unicode emojis](./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:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js index.js
|
||||
import { emojiCharacters } from './emojiCharacters.js';
|
||||
|
||||
console.log(emojiCharacters.a); // 🇦
|
||||
console.log(emojiCharacters[10]); // 🔟
|
||||
console.log(emojiCharacters['!']); // ❗
|
||||
```
|
||||
|
||||
{/* prettier-ignore */}
|
||||
```js emojiCharacters.js
|
||||
export const emojiCharacters = {
|
||||
a: '🇦', b: '🇧', c: '🇨', d: '🇩',
|
||||
e: '🇪', f: '🇫', g: '🇬', h: '🇭',
|
||||
i: '🇮', j: '🇯', k: '🇰', l: '🇱',
|
||||
m: '🇲', n: '🇳', o: '🇴', p: '🇵',
|
||||
q: '🇶', r: '🇷', s: '🇸', t: '🇹',
|
||||
u: '🇺', v: '🇻', w: '🇼', x: '🇽',
|
||||
y: '🇾', z: '🇿', 0: '0️⃣', 1: '1️⃣',
|
||||
2: '2️⃣', 3: '3️⃣', 4: '4️⃣', 5: '5️⃣',
|
||||
6: '6️⃣', 7: '7️⃣', 8: '8️⃣', 9: '9️⃣',
|
||||
10: '🔟', '#': '#️⃣', '*': '*️⃣',
|
||||
'!': '❗', '?': '❓',
|
||||
};
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
You can use the <kbd>⌃ Control</kbd> <kbd>⌘ Command</kbd> <kbd>Space</kbd> keyboard shortcut to open up an emoji picker that can be used for quick, easy access to all the Unicode emojis available to you.
|
||||
|
||||
On Windows, the shortcut is <kbd>⊞</kbd> <kbd>.</kbd>.
|
||||
|
||||
</Alert>
|
||||
165
apps/guide/src/content/03-topics/02-audit-logs.mdx
Normal file
165
apps/guide/src/content/03-topics/02-audit-logs.mdx
Normal file
@@ -0,0 +1,165 @@
|
||||
---
|
||||
title: Audit logs
|
||||
category: Topics
|
||||
---
|
||||
|
||||
# Audit logs
|
||||
|
||||
## A Quick Background
|
||||
|
||||
Audit logs are an excellent moderation tool offered by Discord to know what happened in a server and usually by whom. Making use of audit logs requires the _`ViewAuditLog`_ permission. Audit logs may be fetched on a server, or they may be received via the gateway event <DocsLink type="class" parent="Client" symbol="e-guildAuditLogEntryCreate"/> which requires the _`GuildModeration`_ intent.
|
||||
|
||||
There are quite a few cases where you may use audit logs. This guide will limit itself to the most common use cases. Feel free to consult the [relevant Discord API page](https://discord.com/developers/docs/resources/audit-log) for more information.
|
||||
|
||||
Keep in mind that these examples explore a straightforward case and are by no means exhaustive. Their purpose is to teach you how audit logs work, and expansion of these examples is likely needed to suit your specific use case.
|
||||
|
||||
## Fetching Audit Logs
|
||||
|
||||
Let's start by glancing at the <DocsLink type="class" parent="Guild" symbol="fetchAuditLogs" brackets /> method and how to work with it. Like many discord.js methods, it returns a [Promise](../additional-info/understanding-async-await) containing the <DocsLink type="class" parent="GuildAuditLogs"/> object. This object has one property, _`entries`_, which holds a [Collection](../additional-info/collections) of <DocsLink type="class" parent="GuildAuditLogsEntry"/> objects, and consequently, the information you want to retrieve.
|
||||
|
||||
Here is the most basic fetch to look at some entries.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const fetchedLogs = await guild.fetchAuditLogs();
|
||||
const firstEntry = fetchedLogs.entries.first();
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
Simple, right? Now, let's look at utilizing its options:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { AuditLogEvent } from 'discord.js';
|
||||
|
||||
const fetchedLogs = await guild.fetchAuditLogs({
|
||||
type: AuditLogEvent.InviteCreate,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
const firstEntry = fetchedLogs.entries.first();
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
This will return the first entry where an invite was created. You used _`limit: 1`_ here to specify only one entry.
|
||||
|
||||
## Receiving Audit Logs
|
||||
|
||||
Audit logs may be received via the gateway event <DocsLink type="class" parent="Client" symbol="e-guildAuditLogEntryCreate"/>.
|
||||
This is the best way to receive audit logs if you want to monitor them. As soon as an audit log entry is created,
|
||||
your application will receive an instance of this event. A common use case is to find out _who_ did the action that
|
||||
caused the audit log event to happen.
|
||||
|
||||
### Who deleted a message?
|
||||
|
||||
One of the most common use cases for audit logs is understanding who deleted a message in a Discord server. If a user deleted another user's message, you can find out who did that as soon as you receive the corresponding audit log event.
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
Messages deleted by their author or bots (excluding bulk deletes) do not generate audit log entries.
|
||||
</Alert>
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js JavaScript
|
||||
import { AuditLogEvent, Events } from 'discord.js';
|
||||
|
||||
client.on(Events.GuildAuditLogEntryCreate, async (auditLog) => {
|
||||
// Define your variables.
|
||||
// The extra information here will be the channel.
|
||||
const { action, extra: channel, executorId, targetId } = auditLog;
|
||||
|
||||
// Check only for deleted messages.
|
||||
if (action !== AuditLogEvent.MessageDelete) return;
|
||||
|
||||
// Ensure the executor is cached.
|
||||
const executor = await client.users.fetch(executorId);
|
||||
|
||||
// Ensure the author whose message was deleted is cached.
|
||||
const target = await client.users.fetch(targetId);
|
||||
|
||||
// Log the output.
|
||||
console.log(`A message by ${target.tag} was deleted by ${executor.tag} in ${channel}.`);
|
||||
});
|
||||
```
|
||||
|
||||
```ts TypeScript
|
||||
import { AuditLogEvent, Events } from 'discord.js';
|
||||
|
||||
client.on(Events.GuildAuditLogEntryCreate, async (auditLog) => {
|
||||
// Define your variables.
|
||||
// The extra information here will be the channel.
|
||||
const { action, extra: channel, executorId, targetId } = auditLog;
|
||||
|
||||
// Check only for deleted messages.
|
||||
if (action !== AuditLogEvent.MessageDelete) return;
|
||||
|
||||
// Ensure the executor is cached. The id definitely exists.
|
||||
const executor = await client.users.fetch(executorId!);
|
||||
|
||||
// Ensure the author whose message was deleted is cached. The id definitely exists.
|
||||
const target = await client.users.fetch(targetId!);
|
||||
|
||||
// Log the output.
|
||||
console.log(`A message by ${target.tag} was deleted by ${executor.tag} in ${channel}.`);
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
With this, you now have a very simple logger telling you who deleted a message authored by another person.
|
||||
|
||||
### Who kicked a user?
|
||||
|
||||
This is very similar to the example above.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js JavaScript
|
||||
import { AuditLogEvent, Events } from 'discord.js';
|
||||
|
||||
client.on(Events.GuildAuditLogEntryCreate, async (auditLog) => {
|
||||
// Define your variables.
|
||||
const { action, executorId, targetId } = auditLog;
|
||||
|
||||
// Check only for kicked users.
|
||||
if (action !== AuditLogEvent.MemberKick) return;
|
||||
|
||||
// Ensure the executor is cached.
|
||||
const executor = await client.users.fetch(executorId);
|
||||
|
||||
// Ensure the kicked guild member is cached.
|
||||
const kickedUser = await client.users.fetch(targetId);
|
||||
|
||||
// Now you can log the output!
|
||||
console.log(`${kickedUser.tag} was kicked by ${executor.tag}.`);
|
||||
});
|
||||
```
|
||||
|
||||
```ts TypeScript
|
||||
import { AuditLogEvent, Events } from 'discord.js';
|
||||
|
||||
client.on(Events.GuildAuditLogEntryCreate, async (auditLog) => {
|
||||
// Define your variables.
|
||||
const { action, executorId, targetId } = auditLog;
|
||||
|
||||
// Check only for kicked users.
|
||||
if (action !== AuditLogEvent.MemberKick) return;
|
||||
|
||||
// Ensure the executor is cached. The id definitely exists.
|
||||
const executor = await client.users.fetch(executorId!);
|
||||
|
||||
// Ensure the kicked guild member is cached. The id definitely exists.
|
||||
const kickedUser = await client.users.fetch(targetId!);
|
||||
|
||||
// Now you can log the output!
|
||||
console.log(`${kickedUser.tag} was kicked by ${executor.tag}.`);
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
If you want to check who banned a user, it's the same example as above except the _`action`_ should be <DiscordAPITypesLink type="enum" parent="AuditLogEvent" symbol="MemberBanAdd" />. You can check the rest of the possible actions on this page.
|
||||
223
apps/guide/src/content/03-topics/03-collectors.mdx
Normal file
223
apps/guide/src/content/03-topics/03-collectors.mdx
Normal file
@@ -0,0 +1,223 @@
|
||||
---
|
||||
title: Collectors
|
||||
category: Topics
|
||||
---
|
||||
|
||||
# Collectors
|
||||
|
||||
## Message collectors
|
||||
|
||||
{/* prettier-ignore */}
|
||||
<DocsLink type="class" parent="Collector">Collectors</DocsLink> are useful to enable your bot to obtain _additional_ input after the first command was sent. An example would be initiating a quiz, where the bot will "await" a correct response from somebody.
|
||||
|
||||
### Basic message collector
|
||||
|
||||
Let's take a look at a basic message collector:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const collectorFilter = (message) => message.content.includes('discord');
|
||||
const collector = interaction.channel.createMessageCollector({ filter: collectorFilter, time: 15_000 });
|
||||
|
||||
collector.on('collect', (message) => {
|
||||
console.log(`Collected ${message.content}`);
|
||||
});
|
||||
|
||||
collector.on('end', (collected) => {
|
||||
console.log(`Collected ${collected.size} messages`);
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
You can provide a _`filter`_ key to the object parameter of <DocsLink type="class" parent="TextChannel" symbol="createMessageCollector" brackets />. The value to this key should be a function that returns a boolean value to indicate if this message should be collected or not. To check for multiple conditions in your filter you can connect them using [logical operators](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Expressions_and_Operators#logical_operators). If you don't provide a filter all messages in the channel the collector was started on will be collected.
|
||||
|
||||
Note that the above example uses [implicit return](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Functions/Arrow_functions#function_body) for the filter function and passes it to the options object using the [object property shorthand](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer#property_definitions) notation.
|
||||
|
||||
If a message passes through the filter, it will trigger the <DocsLink type="class" parent="Collector" symbol="e-collect" /> event for the _`collector`_ you've created. This message is then passed into the event listener as _`collected`_ and the provided function is executed. In the above example, you simply log the message. Once the collector finishes collecting based on the provided end conditions the <DocsLink type="class" parent="Collector" symbol="e-end" /> event emits.
|
||||
|
||||
You can control when a collector ends by supplying additional option keys when creating a collector:
|
||||
|
||||
- _`time`_: Amount of time in milliseconds the collector should run for
|
||||
- _`max`_: Number of messages to successfully pass the filter
|
||||
- _`maxProcessed`_: Number of messages encountered (no matter the filter result)
|
||||
|
||||
The benefit of using an event-based collector over _`awaitMessages()`_ (its promise-based counterpart) is that you can do something directly after each message is collected, rather than just after the collector ended. You can also stop the collector manually by calling <DocsLink type="class" parent="Collector" symbol="stop" brackets />.
|
||||
|
||||
### Await messages
|
||||
|
||||
Using <DocsLink type="class" parent="TextChannel" symbol="awaitMessages" brackets /> can be easier if you understand [Promises](../additional-info/understanding-async-await), and it allows you to have cleaner code overall. It is essentially identical to <DocsLink type="class" parent="TextChannel" symbol="createMessageCollector" brackets />, except promisified. However, the drawback of using this method is that you cannot do things before the Promise is resolved or rejected, either by an error or completion. However, it should do for most purposes, such as awaiting the correct response in a quiz. Instead of taking their example, let's set up a basic quiz command using the _`.awaitMessages()`_ feature.
|
||||
|
||||
First, you'll need some questions and answers to choose from, so here's a basic set:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"question": "What color is the sky?",
|
||||
"answers": ["blue"]
|
||||
},
|
||||
{
|
||||
"question": "How many letters are there in the alphabet?",
|
||||
"answers": ["26", "twenty-six", "twenty six", "twentysix"]
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
The provided set allows for responder error with an array of answers permitted. Ideally, it would be best to place this in a JSON file, which you can call _`quiz.json`_ for simplicity.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import quiz from './quiz.json' assert { type: 'json' };
|
||||
|
||||
// ...
|
||||
|
||||
const item = quiz[Math.floor(Math.random() * quiz.length)];
|
||||
|
||||
const collectorFilter = (response) => {
|
||||
return item.answers.some((answer) => answer.toLowerCase() === response.content.toLowerCase());
|
||||
};
|
||||
|
||||
await interaction.reply({ content: item.question });
|
||||
|
||||
try {
|
||||
const collected = await interaction.channel.awaitMessages({
|
||||
filter: collectorFilter,
|
||||
max: 1,
|
||||
time: 30_000,
|
||||
errors: ['time'],
|
||||
});
|
||||
|
||||
await interaction.followUp(`${collected.first().author} got the correct answer!`);
|
||||
} catch {
|
||||
await interaction.followUp('Looks like nobody got the answer this time.');
|
||||
}
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
If you don't understand how _`.some()`_ works, you can read about it in more detail
|
||||
[here](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/some).
|
||||
</Alert>
|
||||
|
||||
In this filter, you iterate through the answers to find what you want. You would like to ignore the case because simple typos can happen, so you convert each answer to its lowercase form and check if it's equal to the response in lowercase form as well. In the options section, you only want to allow one answer to pass through, hence the _`max: 1`_ setting.
|
||||
|
||||
The filter looks for messages that match one of the answers in the array of possible answers to pass through the collector. The _`max`_ option (the second parameter) specifies that only a maximum of one message can go through the filter successfully before the Promise successfully resolves. The _`errors`_ section specifies that time will cause it to error out, which will cause the Promise to reject if one correct answer is not received within the time limit of one minute. As you can see, there is no _`collect`_ event, so you are limited in that regard.
|
||||
|
||||
## Reaction collectors
|
||||
|
||||
### Basic reaction collector
|
||||
|
||||
These work quite similarly to message collectors, except that you apply them on a message rather than a channel. This example uses the <DocsLink type="class" parent="Message" symbol="createReactionCollector" brackets /> method. The filter will check for the 👍 emoji–in the default skin tone specifically, so be wary of that. It will also check that the person who reacted shares the same id as the author of the original message that the collector was assigned to.
|
||||
|
||||
```js
|
||||
const collectorFilter = (reaction, user) => {
|
||||
return reaction.emoji.name === '👍' && user.id === message.author.id;
|
||||
};
|
||||
|
||||
const collector = message.createReactionCollector({ filter: collectorFilter, time: 15_000 });
|
||||
|
||||
collector.on('collect', (reaction, user) => {
|
||||
console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
|
||||
});
|
||||
|
||||
collector.on('end', (collected) => {
|
||||
console.log(`Collected ${collected.size} items`);
|
||||
});
|
||||
```
|
||||
|
||||
### Await reactions
|
||||
|
||||
<DocsLink type="class" parent="Message" symbol="awaitReactions" brackets /> works almost the same as a reaction collector,
|
||||
except it is Promise-based. The same differences apply as with channel collectors.
|
||||
|
||||
```js
|
||||
const collectorFilter = (reaction, user) => {
|
||||
return reaction.emoji.name === '👍' && user.id === message.author.id;
|
||||
};
|
||||
|
||||
try {
|
||||
const collected = await message.awaitReactions({ filter: collectorFilter, max: 1, time: 60_000, errors: ['time'] });
|
||||
console.log(collected.size);
|
||||
} catch (collected) {
|
||||
console.log(`After a minute, the user did not react.`);
|
||||
}
|
||||
```
|
||||
|
||||
## Interaction collectors
|
||||
|
||||
The third type of collector allows you to collect interactions; such as when users activate a slash command or click on a button in a message.
|
||||
|
||||
### Basic message component collector
|
||||
|
||||
Collecting interactions from message components works similarly to reaction collectors. In the following example, you will check that the interaction came from a button, and that the user clicking the button is the same user that initiated the command.
|
||||
|
||||
One important difference to note with interaction collectors is that Discord expects a response to _all_ interactions within 3 seconds - even ones that you don't want to collect. For this reason, you may wish to _`.deferUpdate()`_ all interactions in your filter, or not use a filter at all and handle this behavior in the _`collect`_ event.
|
||||
|
||||
```js
|
||||
import { ComponentType } from 'discord.js';
|
||||
|
||||
const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button, time: 15_000 });
|
||||
|
||||
collector.on('collect', (i) => {
|
||||
if (i.user.id === interaction.user.id) {
|
||||
await i.reply(`${i.user.id} clicked on the ${i.customId} button.`);
|
||||
} else {
|
||||
await i.reply({ content: `These buttons aren't for you!`, ephemeral: true });
|
||||
}
|
||||
});
|
||||
|
||||
collector.on('end', (collected) => {
|
||||
console.log(`Collected ${collected.size} interactions.`);
|
||||
});
|
||||
```
|
||||
|
||||
### Await message component
|
||||
|
||||
As before, this works similarly to the message component collector, except it is Promise-based.
|
||||
|
||||
Unlike other Promise-based collectors, this method will only ever collect one interaction that passes the filter. If no interactions are collected before the time runs out, the Promise will reject. This behavior aligns with Discord's requirement that actions should immediately receive a response. In this example, you will use _`.deferUpdate()`_ on all interactions in the filter.
|
||||
|
||||
```js
|
||||
import { ComponentType } from 'discord.js';
|
||||
|
||||
const collectorFilter = (i) => {
|
||||
i.deferUpdate();
|
||||
return i.user.id === interaction.user.id;
|
||||
};
|
||||
|
||||
try {
|
||||
const interaction = await message.awaitMessageComponent({
|
||||
filter: collectorFilter,
|
||||
componentType: ComponentType.StringSelect,
|
||||
time: 60_000,
|
||||
});
|
||||
|
||||
await interaction.editReply(`You selected ${interaction.values.join(', ')}!`);
|
||||
} catch (error) {
|
||||
console.log('No interactions were collected.');
|
||||
}
|
||||
```
|
||||
|
||||
### Await modal submit
|
||||
|
||||
If you want to wait for the submission of a modal within the context of another command or button execution, you may find the promisified collector <DocsLink type="class" parent="CommandInteraction" symbol="awaitModalSubmit" brackets /> useful.
|
||||
|
||||
As Discord does not inform you if the user dismisses the modal, supplying a maximum _`time`_ to wait for is crucial:
|
||||
|
||||
```js
|
||||
try {
|
||||
const interaction = await initialInteraction.awaitModalSubmit({ time: 60_000, filter });
|
||||
await interaction.editReply('Thank you for your submission!');
|
||||
} catch (error) {
|
||||
console.log('No modal submit interaction was collected');
|
||||
}
|
||||
```
|
||||
|
||||
For more information on working with modals, see the [modals section of this guide](../interactions/modals).
|
||||
95
apps/guide/src/content/03-topics/04-formatters.mdx
Normal file
95
apps/guide/src/content/03-topics/04-formatters.mdx
Normal file
@@ -0,0 +1,95 @@
|
||||
---
|
||||
title: Formatters
|
||||
category: Topics
|
||||
---
|
||||
|
||||
# Formatters
|
||||
|
||||
discord.js provides the <DocsLink package="formatters" /> package which contains a variety of utilities you can use when writing your Discord bot.
|
||||
|
||||
## Basic Markdown
|
||||
|
||||
These functions format strings into all the different markdown styles supported by Discord.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { bold, italic, strikethrough, underscore, spoiler, quote, blockQuote } from 'discord.js';
|
||||
|
||||
const string = 'Hello!';
|
||||
const boldString = bold(string);
|
||||
const italicString = italic(string);
|
||||
const strikethroughString = strikethrough(string);
|
||||
const underscoreString = underscore(string);
|
||||
const spoilerString = spoiler(string);
|
||||
const quoteString = quote(string);
|
||||
const blockquoteString = blockQuote(string);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Links
|
||||
|
||||
There are also two functions to format hyperlinks. _`hyperlink()`_ will format the URL into a masked markdown link, and _`hideLinkEmbed()`_ will wrap the URL in _`<>`_, preventing it from embedding.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { hyperlink, hideLinkEmbed } from 'discord.js';
|
||||
|
||||
const url = 'https://discord.js.org/';
|
||||
const link = hyperlink('discord.js', url);
|
||||
const hiddenEmbed = hideLinkEmbed(url);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Code blocks
|
||||
|
||||
You can use _`inlineCode()`_ and _`codeBlock()`_ to turn a string into an inline code block or a regular code block with or without syntax highlighting.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { inlineCode, codeBlock } from 'discord.js';
|
||||
|
||||
const jsString = 'const value = true;';
|
||||
const inline = inlineCode(jsString);
|
||||
const codeblock = codeBlock(jsString);
|
||||
const highlighted = codeBlock('js', jsString);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Timestamps
|
||||
|
||||
With _`time()`_, you can format Unix timestamps and dates into a Discord time string.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { time, TimestampStyles } from 'discord.js';
|
||||
|
||||
const date = new Date();
|
||||
const timeString = time(date);
|
||||
const relative = time(date, TimestampStyles.RelativeTime);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Mentions
|
||||
|
||||
_`userMention()`_, _`channelMention()`_, and _`roleMention()`_ all exist to format Snowflakes into mentions.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { channelMention, roleMention, userMention } from 'discord.js';
|
||||
|
||||
const id = '123456789012345678';
|
||||
const channel = channelMention(id);
|
||||
const role = roleMention(id);
|
||||
const user = userMention(id);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
68
apps/guide/src/content/03-topics/05-intents.mdx
Normal file
68
apps/guide/src/content/03-topics/05-intents.mdx
Normal file
@@ -0,0 +1,68 @@
|
||||
---
|
||||
title: Intents
|
||||
category: Topics
|
||||
---
|
||||
|
||||
# Intents
|
||||
|
||||
Intents are an important part of establishing a WebSocket connection, as they define behavior regarding gateway events and impact received data via the REST API.
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import { Client, GatewayIntentBits } from 'discord.js';
|
||||
|
||||
const client = new Client({
|
||||
intents: [GatewayIntentBits.Guilds],
|
||||
});
|
||||
```
|
||||
|
||||
This is the most basic usage of intents for discord.js. By specifying _`GatewayIntentBits.Guilds`_, your bot will receive gateway events regarding guilds. This includes receiving initial information about guilds it is in at startup, such as role data.
|
||||
|
||||
You can find the full list of _`GatewayIntentBits`_ <DiscordAPITypesLink type="enum" parent="GatewayIntentBits">on the documentation</DiscordAPITypesLink> and an explanation of what each intent does [on Discord's API documentation](https://discord.com/developers/docs/topics/gateway#list-of-intents).
|
||||
|
||||
## Considerations
|
||||
|
||||
In discord.js, some intents require an extra bit of consideration.
|
||||
|
||||
### _`GatewayIntentBits.Guilds`_
|
||||
|
||||
discord.js relies heavily on caching in the library. We recommend you set at least the _`GatewayIntentBits.Guilds`_ intent to avoid these pitfalls.
|
||||
|
||||
### _`GatewayIntentBits.GuildMembers`_
|
||||
|
||||
Fetching members in a guild via <DocsLink type="class" parent="GuildMemberManager" symbol="fetch" brackets /> requests them over the gateway. As such, this intent is required and you may receive a timeout error if this intent is not specified.
|
||||
|
||||
<Alert title="Info" type="info">
|
||||
This is a privileged intent. Read on for more information.
|
||||
</Alert>
|
||||
|
||||
### _`GatewayIntentBits.DirectMessages`_
|
||||
|
||||
This intent is required to receive direct messages. In discord.js however, you **must** specify partials as well. See the partials topic on how this is done.
|
||||
|
||||
### _`GatewayIntentBits.MessageContent`_
|
||||
|
||||
Unlike other intents, this only populates user-generated fields. See [Discord's documentation](https://discord.com/developers/docs/topics/gateway#message-content-intent) on what exactly this intent unveils.
|
||||
|
||||
It is a common mistake to not see the message content in a message—this is usually because this intent is not specified.
|
||||
|
||||
<Alert title="Info" type="info">
|
||||
This is a privileged intent. Read on for more information.
|
||||
</Alert>
|
||||
|
||||
## Privileged intents
|
||||
|
||||
Some gateway events are considered privileged. Currently, these are:
|
||||
|
||||
- _`GatewayIntentBits.GuildPresences`_
|
||||
- _`GatewayIntentBits.GuildMembers`_
|
||||
- _`GatewayIntentBits.MessageContent`_
|
||||
|
||||
To use these intents, you will need to enable them in the developer portal. If your bot is in over 75 guilds, you will need to verify it and request usage of your desired intents.
|
||||
|
||||
Carefully think if you need these intents. They are opt-in so users across the platform can enjoy a higher level of privacy. Presences can expose some personal information, such as the games being played and overall online time. You might find that it isn't necessary for your bot to have this level of information about all guild members at all times.
|
||||
|
||||
### Disallowed intents
|
||||
|
||||
Should you receive an error stating you are using disallowed intents, please review your developer dashboard settings for all privileged intents you use. Check the Discord API documentation for up-to-date information.
|
||||
202
apps/guide/src/content/03-topics/06-threads.mdx
Normal file
202
apps/guide/src/content/03-topics/06-threads.mdx
Normal file
@@ -0,0 +1,202 @@
|
||||
---
|
||||
title: Threads
|
||||
category: Topics
|
||||
---
|
||||
|
||||
# Threads
|
||||
|
||||
Threads can be thought of as temporary sub-channels inside an existing channel to help better organize conversations in a busy channel.
|
||||
|
||||
## Thread related gateway events
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
You can use the <DocsLink type="class" parent="BaseChannel" symbol="isThread" brackets /> type guard to make sure a
|
||||
channel is a <DocsLink type="class" parent="ThreadChannel" />!
|
||||
</Alert>
|
||||
|
||||
Threads introduce a number of new gateway events, which are listed below:
|
||||
|
||||
- <DocsLink type="class" parent="Client" symbol="e-threadCreate" />: Emitted whenever a thread is created or when the
|
||||
client user is added to a thread.
|
||||
- <DocsLink type="class" parent="Client" symbol="e-threadDelete" />: Emitted whenever a thread is deleted.
|
||||
- <DocsLink type="class" parent="Client" symbol="e-threadUpdate" />: Emitted whenever a thread is updated (e.g. name
|
||||
change, archive state change, locked state change).
|
||||
- <DocsLink type="class" parent="Client" symbol="e-threadListSync" />: Emitted whenever the client user gains access to
|
||||
a text or announcement channel that contains threads.
|
||||
- <DocsLink type="class" parent="Client" symbol="e-threadMembersUpdate" />: Emitted whenever members are added or
|
||||
removed from a thread. Requires <code>GuildMembers</code> privileged intent.
|
||||
- <DocsLink type="class" parent="Client" symbol="e-threadMemberUpdate" />: Emitted whenever the client user's thread
|
||||
member is updated.
|
||||
|
||||
## Creating and deleting threads
|
||||
|
||||
Threads are created and deleted using the <DocsLink type="class" parent="GuildTextThreadManager" /> of a text or announcement channel.
|
||||
To create a thread, you call the <DocsLink type="class" parent="GuildTextThreadManager" symbol="create" brackets /> method:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { ThreadAutoArchiveDuration } from 'discord.js';
|
||||
|
||||
const thread = await channel.threads.create({
|
||||
name: 'food-talk',
|
||||
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
|
||||
reason: 'Needed a separate thread for food',
|
||||
});
|
||||
|
||||
console.log(`Created thread: ${thread.name}`);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
They can also be created from an existing message with the <DocsLink type="class" parent="Message" symbol="startThread" brackets /> method, but will be "orphaned" if that message is deleted.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=3[22:42]
|
||||
import { ThreadAutoArchiveDuration } from 'discord.js';
|
||||
|
||||
const thread = await message.startThread({
|
||||
name: 'food-talk',
|
||||
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
|
||||
reason: 'Needed a separate thread for food',
|
||||
});
|
||||
|
||||
console.log(`Created thread: ${thread.name}`);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
The created thread and the message it originated from will share the same id. The type of thread created matches the parent channel's type.
|
||||
|
||||
To delete a thread, use the <DocsLink type="class" parent="ThreadChannel" symbol="delete" brackets /> method:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=2
|
||||
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
|
||||
if (thread.manageable) await thread.delete();
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Joining and leaving threads
|
||||
|
||||
To subscribe your client to a thread, use the <DocsLink type="class" parent="ThreadChannel" symbol="join" brackets /> method:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=2
|
||||
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
|
||||
if (thread.joinable) await thread.join();
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
And to leave one, use the <DocsLink type="class" parent="ThreadChannel" symbol="leave" brackets /> method:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=2
|
||||
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
|
||||
await thread.leave();
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Archiving, unarchiving, and locking threads
|
||||
|
||||
A thread can be either active or archived. Changing a thread from archived to active is referred to as unarchiving the thread. Threads that have _`locked`_ set to _`true`_ can only be unarchived by a member with the _`ManageThreads`_ permission.
|
||||
|
||||
Threads are automatically archived after inactivity. "Activity" is defined as sending a message, unarchiving a thread, or changing the auto-archive time.
|
||||
|
||||
To archive or unarchive a thread, use the <DocsLink type="class" parent="ThreadChannel" symbol="setArchived" brackets /> method and pass in a boolean parameter:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=2,3
|
||||
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
|
||||
await thread.setArchived(true); // Archived.
|
||||
await thread.setArchived(false); // Unarchived.
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
This same principle applies to locking and unlocking a thread via the <DocsLink type="class" parent="ThreadChannel" symbol="setLocked" brackets /> method:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=2,3
|
||||
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
|
||||
await thread.setLocked(true); // Locked.
|
||||
await thread.setLocked(false); // Unlocked.
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Private threads
|
||||
|
||||
Public threads are viewable by everyone who can view the parent channel of the thread. Private threads, however, are only viewable to those who are invited or have the _`ManageThreads`_ permission. Private threads can only be created on text channels.
|
||||
|
||||
To create a private thread, use the <DocsLink type="class" parent="GuildTextThreadManager" symbol="create" brackets /> method and pass in _`ChannelType.PrivateThread`_ as the _`type`_:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=1[10:21],6
|
||||
import { ChannelType, ThreadAutoArchiveDuration } from 'discord.js';
|
||||
|
||||
const thread = await channel.threads.create({
|
||||
name: 'mod-talk',
|
||||
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
|
||||
type: ChannelType.PrivateThread,
|
||||
reason: 'Needed a separate thread for moderation',
|
||||
});
|
||||
|
||||
console.log(`Created thread: ${thread.name}`);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Adding and removing members
|
||||
|
||||
You can add members to a thread with the <DocsLink type="class" parent="ThreadMemberManager" symbol="add" brackets /> method. The thread must be unarchived and you must be able to send messages in it.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=2
|
||||
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
|
||||
await thread.members.add('12345678901234567');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
You can remove members from a thread with the <DocsLink type="class" parent="ThreadMemberManager" symbol="remove" brackets /> method. The thread must be unarchived and you must have the _`ManageThreads`_ permission unless the thread is private and you are the owner of it.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=2
|
||||
const thread = channel.threads.cache.find((x) => x.name === 'food-talk');
|
||||
await thread.members.remove('12345678901234567');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Sending messages to threads with webhooks
|
||||
|
||||
It is possible for a webhook built on the parent channel to send messages to the channel's threads. For the purpose of this example, it is assumed a single webhook already exists for that channel. If you wish to learn more about webhooks, see our [webhook guide](./webhooks).
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js focus=4:7
|
||||
const webhooks = await channel.fetchWebhooks();
|
||||
const webhook = webhooks.first();
|
||||
|
||||
await webhook.send({
|
||||
content: "Look ma! I'm in a thread!",
|
||||
threadId: '123456789012345678',
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
And that's it! Now you know all there is to know on working with threads using discord.js!
|
||||
227
apps/guide/src/content/03-topics/07-webhooks.mdx
Normal file
227
apps/guide/src/content/03-topics/07-webhooks.mdx
Normal file
@@ -0,0 +1,227 @@
|
||||
---
|
||||
title: Webhooks
|
||||
category: Topics
|
||||
---
|
||||
|
||||
# Webhooks
|
||||
|
||||
Webhooks can send messages to a text channel without having to log in as a bot. They can also fetch, edit, and delete their own messages. There are a variety of methods in discord.js to interact with webhooks. In this section, you will learn how to create, fetch, edit, and use webhooks.
|
||||
|
||||
## What is a webhook
|
||||
|
||||
Webhooks are a utility used to send messages to text channels without needing a Discord application. Webhooks are useful for allowing something to send messages without requiring a Discord application. You can also directly edit or delete messages you sent through the webhook. There are two structures to make use of this functionality: <DocsLink type="class" parent="Webhook" /> and <DocsLink type="class" parent="WebhookClient" />. _`WebhookClient`_ is an extended version of a _`Webhook`_, which allows you to send messages through it without needing a bot client.
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
If you would like to read about using webhooks through the API without discord.js, you can read about them
|
||||
[here](https://discord.com/developers/docs/resources/webhook).
|
||||
</Alert>
|
||||
|
||||
## Detecting webhook messages
|
||||
|
||||
Bots receive webhook messages in a text channel as usual. You can detect if a webhook sent the message by checking if the _`Message.webhookId`_ is not _`null`_. In this example, we return if a webhook sent the message.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
if (message.webhookId) return;
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
If you would like to get the webhook object that sent the message, you can use <DocsLink type="class" parent="Message" symbol="fetchWebhook" brackets />.
|
||||
|
||||
## Fetching webhooks
|
||||
|
||||
<Alert title="Tip" type="info">
|
||||
Webhook fetching will always make use of collections and promises. If you do not understand either concept, revise
|
||||
them, and then come back to this section. You can read about collections [here](../additional-info/collections), and
|
||||
promises [here](../additional-info/understanding-async-await) and
|
||||
[here](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Using_promises).
|
||||
</Alert>
|
||||
|
||||
### Fetching all webhooks of a guild
|
||||
|
||||
If you would like to get all webhooks of a guild, you can use the <DocsLink type="class" parent="Guild" symbol="fetchWebhooks" brackets /> method. This will return a promise which will resolve into a collection of webhooks.
|
||||
|
||||
### Fetching webhooks of a channel
|
||||
|
||||
Webhooks belonging to a channel can be fetched using the <DocsLink type="class" parent="TextChannel" symbol="fetchWebhooks" brackets /> method. This will return a promise which will resolve into a collection of webhooks. A collection will be returned even if the channel contains a single webhook. If you are certain the channel contains a single webhook, you can use the <DocsLink package="collection" type="Class" parent="Collection" symbol="first" brackets /> method on the collection to get the webhook.
|
||||
|
||||
### Fetching a single webhook
|
||||
|
||||
#### Using client
|
||||
|
||||
You can fetch a specific webhook using its _`id`_ with the <DocsLink type="class" parent="Client" symbol="fetchWebhook" brackets /> method. You can obtain the webhook id by looking at its URL: the number after _`https://discord.com/api/webhooks/`_ is the _`id`_ and the part after that is the _`token`_.
|
||||
|
||||
#### Using the WebhookClient constructor
|
||||
|
||||
If you are not using a bot client, you can get a webhook by creating a new instance of _`WebhookClient`_ and passing the _`id`_ and _`token`_ into the constructor. These credentials do not require you to have a bot application, but it also offers limited information instead of fetching it using an authorized client.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const webhookClient = new WebhookClient({ id: 'id', token: 'token' });
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
You can also pass in just a _`url`_:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const webhookClient = new WebhookClient({ url: 'https://discord.com/api/webhooks/id/token' });
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Creating webhooks
|
||||
|
||||
### Creating webhooks through server settings
|
||||
|
||||
You can create webhooks directly through the Discord client. Go to Server Settings, and you will see an _`Integrations`_ tab.
|
||||
|
||||

|
||||
|
||||
If you already have created a webhook, the webhooks tab will look like this; you will need to click the _`View Webhooks`_ button.
|
||||
|
||||

|
||||
|
||||
Once you are there, click on the _`Create Webhook`_ / _`New Webhook`_ button; this will create a webhook. From here, you can edit the channel, the name, and the avatar. Copy the link, the first part is the id, and the second is the token.
|
||||
|
||||

|
||||
|
||||
### Creating webhooks with discord.js
|
||||
|
||||
Webhooks can be created with the <DocsLink type="class" parent="TextChannel" symbol="createWebhook" brackets /> method.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
channel
|
||||
.createWebhook({ name: 'Username', avatar: 'https://guide.discordjs.dev/assets/discordjs.png' })
|
||||
.then((webhook) => console.log(`Created webhook ${webhook}`))
|
||||
.catch(console.error);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Editing webhooks
|
||||
|
||||
You can edit Webhooks and WebhookClients to change their name, avatar, and channel using <DocsLink type="class" parent="Webhook" symbol="edit" brackets />.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
webhook
|
||||
.edit({ name: 'Username', avatar: 'https://guide.discordjs.dev/assets/discordjs.png', channel: '123456789012345678' })
|
||||
.then((webhook) => console.log(`Edited webhook ${webhook}`))
|
||||
.catch(console.error);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
## Using webhooks
|
||||
|
||||
Webhooks can send messages to text channels, as well as fetch, edit, and delete their own. These methods are the same for both _`Webhook`_ and _`WebhookClient`_.
|
||||
|
||||
### Sending messages
|
||||
|
||||
Webhooks, like bots, can send up to 10 embeds per message. They can also send attachments and normal content. The <DocsLink type="class" parent="Webhook" symbol="send" brackets /> method is very similar to the method used for sending a message to a text channel. Webhooks can also choose how the username and avatar will appear when they send the message.
|
||||
|
||||
Example using a _`WebhookClient`_:
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { EmbedBuilder, WebhookClient } from 'discord.js';
|
||||
import config from './config.json' assert { type: 'json' };
|
||||
const { webhookId, webhookToken } = config;
|
||||
|
||||
const webhookClient = new WebhookClient({ id: webhookId, token: webhookToken });
|
||||
const embed = new EmbedBuilder().setTitle('Some Title').setColor(0x00ffff);
|
||||
|
||||
await webhookClient.send({
|
||||
content: 'Webhook test',
|
||||
username: 'some-username',
|
||||
avatarURL: 'https://guide.discordjs.dev/assets/discordjs.png',
|
||||
embeds: [embed],
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
Try to find a webhook your bot knows the token for. This makes sure your bot can execute the webhook later on.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
import { Client, EmbedBuilder, Events, GatewayIntentBits } from 'discord.js';
|
||||
import config from './config.json' assert { type: 'json' };
|
||||
const { token } = config;
|
||||
|
||||
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
|
||||
const embed = new EmbedBuilder().setTitle('Some Title').setColor(0x00ffff);
|
||||
|
||||
client.once(Events.ClientReady, async () => {
|
||||
const channel = client.channels.cache.get('123456789012345678');
|
||||
|
||||
try {
|
||||
const webhooks = await channel.fetchWebhooks();
|
||||
const webhook = webhooks.find((wh) => wh.token);
|
||||
if (!webhook) return console.log('No webhook was found that I can use!');
|
||||
|
||||
await webhook.send({
|
||||
content: 'Webhook test',
|
||||
username: 'some-username',
|
||||
avatarURL: 'https://guide.discordjs.dev/assets/discordjs.png',
|
||||
embeds: [embed],
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error trying to send a message: ', error);
|
||||
}
|
||||
});
|
||||
|
||||
client.login(token);
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### Fetching messages
|
||||
|
||||
You can use <DocsLink type="class" parent="Webhook" symbol="fetchMessage" brackets /> to fetch messages previously sent by the Webhook.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const message = await webhookClient.fetchMessage('123456789012345678');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### Editing messages
|
||||
|
||||
You can use <DocsLink type="class" parent="Webhook" symbol="editMessage" brackets /> to edit messages previously sent by the Webhook.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
const message = await webhook.editMessage('123456789012345678', {
|
||||
content: 'Edited!',
|
||||
embeds: [embed],
|
||||
});
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
|
||||
### Deleting messages
|
||||
|
||||
You can use <DocsLink type="class" parent="Webhook" symbol="deleteMessage" brackets /> to delete messages previously sent by the webhook.
|
||||
|
||||
<CH.Code>
|
||||
|
||||
```js
|
||||
await webhookClient.deleteMessage('123456789012345678');
|
||||
```
|
||||
|
||||
</CH.Code>
|
||||
Reference in New Issue
Block a user