-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache-messages.ts
More file actions
49 lines (42 loc) · 1.74 KB
/
cache-messages.ts
File metadata and controls
49 lines (42 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { ApplicationCommandOptionType, PermissionFlagsBits, PermissionsBitField } from 'discord.js';
import { fetchAndCachePublicChannelsMessages } from '../../util/cache.js';
import { createCommand } from '../../util/commands.js';
export default createCommand({
data: {
name: 'cache-messages',
description: 'Cache messages in all text channels of the server',
default_member_permissions: new PermissionsBitField(
PermissionFlagsBits.ManageMessages
).toJSON(),
options: [
{
name: 'force',
description: 'Force re-caching even if messages are already cached',
type: ApplicationCommandOptionType.Boolean,
required: false,
},
],
},
execute: async (interaction) => {
await interaction.deferReply();
if (!interaction.guild || !interaction.isChatInputCommand()) {
await interaction.editReply('This command can only be used in a guild.');
return;
}
if (!interaction.memberPermissions?.has(PermissionFlagsBits.ManageMessages)) {
await interaction.editReply('You do not have permission to use this command.');
return;
}
const guild = interaction.guild;
const force = interaction.options.getBoolean('force') ?? false;
await interaction.editReply('Caching messages in all public text channels...');
const { cachedChannels, totalChannels, failedChannels } =
await fetchAndCachePublicChannelsMessages(guild, force);
const failedMessage = failedChannels.length
? `\nFailed to cache messages in the following channels: ${failedChannels.map((id) => `<#${id}>`).join(', ')}`
: '';
await interaction.editReply(
`Cached messages in ${cachedChannels} out of ${totalChannels} text channels.${failedMessage}`
);
},
});