Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { config } from '../../modules/config/cfg';
- const KV = new pylon.KVNamespace('clear');
- config.commands.handler.on(
- {
- name: 'clear',
- aliases: ['prune', 'purge'],
- description: 'Delete a specified amount of messages',
- filters: config.modules.permissions.mod
- },
- (args) => ({
- number: args.number() + 1
- }),
- async (message, { number }) => {
- const numberOfDeletedMessages: number = await DeleteClearMessages(
- message.channelId,
- number
- );
- const errorChannel = await discord.getTextChannel(
- config.modules.errors.channel
- );
- if (!errorChannel) throw new Error(config.msg.INVALID_CHANNEL);
- try {
- let response: discord.Message;
- if (numberOfDeletedMessages === 0)
- response = await message?.reply(`No messages deleted.`);
- else
- response = await message?.reply(
- `Deleted the last ${numberOfDeletedMessages} message(s) from this channel.`
- );
- if (!config.modules.mod.commands.clear.enabled) {
- return;
- } else {
- await message?.delete();
- }
- } catch (err) {
- console.error(err);
- errorChannel.sendMessage(
- '\`\`\`css\n' +
- config.msg.DISCORD_API_ERROR +
- ':' +
- err.code +
- '[' +
- err.message +
- ']\n\`\`\`'
- );
- }
- }
- );
- discord.on(discord.Event.MESSAGE_CREATE, (message) =>
- SaveClearMessages(message.channelId, message.id)
- );
- discord.on(discord.Event.MESSAGE_DELETE, async (message) => {
- let messages: string[] =
- (await KV.get(`messages-${message.channelId}`)) ?? [];
- let index: number = messages.findIndex((m) => m === message.id);
- if (index === -1) return;
- messages.splice(index, 1);
- if (messages.length !== 0)
- await KV.put(`messages-${message.channelId}`, messages);
- else await KV.delete(`messages-${message.channelId}`);
- });
- discord.on(discord.Event.CHANNEL_DELETE, async (channel) => {
- try {
- await KV.delete(`messages-${channel.id}`);
- } catch (e) {}
- });
- async function DeleteClearMessages(
- channelId: string,
- nr: number
- ): Promise<number> {
- let messages: string[] | undefined = await KV.get(`messages-${channelId}`);
- if (messages === undefined) return 0;
- const channel = await discord.getGuildTextChannel(channelId);
- let toDeleteMessages: string[] = [];
- for (
- let i = messages.length - (nr < messages.length ? nr : messages.length);
- i < messages.length;
- ++i
- )
- toDeleteMessages.push(messages[i]);
- if (toDeleteMessages.length === 1) {
- try {
- await (await channel?.getMessage(toDeleteMessages[0]))?.delete();
- } catch (_) {}
- } else if (toDeleteMessages.length !== 0) {
- try {
- await channel?.bulkDeleteMessages(toDeleteMessages);
- } catch (_) {}
- }
- const currentMessages: string[] = messages.filter(
- (m) => !toDeleteMessages.includes(m)
- );
- if (currentMessages.length === 0)
- try {
- await KV.delete(`messages-${channelId}`);
- } catch (_) {}
- else {
- await KV.put(`messages-${channelId}`, currentMessages);
- }
- return toDeleteMessages.length;
- }
- export async function SaveClearMessages(
- channelId: string | undefined,
- messageId: string | undefined
- ): Promise<void> {
- if (messageId === undefined || channelId === undefined) return;
- let messages: string[] = (await KV.get(`messages-${channelId}`)) ?? [];
- messages.push(messageId);
- while (JSON.stringify(messages).length > 8192) messages.splice(0, 1);
- await KV.put(`messages-${channelId}`, messages);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement