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