From 046eb1d33eb9d44f0e43886054f6d342b4f95ebd Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Thu, 8 Sep 2022 20:10:41 -0400 Subject: [PATCH] V0.3.0 - Added cleanRaidCheckpointChannel This function cleans up old messages from followed channels in the specified channel --- config.example.ts | 2 +- src/events/messageCreate.ts | 13 +++++++------ src/functions/_index.ts | 2 ++ src/functions/cleanRaidCheckpointChannel.ts | 19 +++++++++++++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 src/functions/cleanRaidCheckpointChannel.ts diff --git a/config.example.ts b/config.example.ts index bc82651..0e15055 100644 --- a/config.example.ts +++ b/config.example.ts @@ -1,6 +1,6 @@ export const config = { 'name': 'Sweeper Bot', // Name of the bot - 'version': '0.2.4', // Version of the bot + 'version': '0.3.0', // Version of the bot 'token': 'the_bot_token', // Discord API Token for this bot 'localtoken': 'local_testing_token', // Discord API Token for a secondary OPTIONAL testing bot, THIS MUST BE DIFFERENT FROM "token" 'prefix': 's!', // Prefix for all commands diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 709788d..172099c 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -3,20 +3,18 @@ import { // Discordeno deps Bot, botId, + getReactions, // Log4Deno deps log, LT, // Discordeno deps - Message,getReactions, + Message, } from '../../deps.ts'; import commands from '../commands/_index.ts'; import functions from '../functions/_index.ts'; import utils from '../utils.ts'; export const messageCreate = async (bot: Bot, message: Message) => { - // Ignore all other bots - if (message.isFromBot) return; - // Ignore all messages that are not commands if (message.content.indexOf(config.prefix) !== 0) { // Handle @bot messages @@ -29,13 +27,16 @@ export const messageCreate = async (bot: Bot, message: Message) => { } if (config.raidCheckpointChannel.includes(message.channelId)) { - console.log(message); + functions.cleanRaidCheckpointChannel(bot, message.channelId); } // return as we are done handling this command return; } + // Ignore all other bots + if (message.isFromBot) return; + log(LT.LOG, `Handling ${config.prefix}command message: ${utils.jsonStringifyBig(message)}`); // Split into standard command + args format @@ -81,7 +82,7 @@ export const messageCreate = async (bot: Bot, message: Message) => { commands.sendMessage(bot, message, args); } break; - // case 'test': + // case 'emoji-test': // const test = await bot.helpers.getMessage(413640605491658754n, 1016090989816987701n); // console.log(test) // if (test.reactions && test.reactions.length) { diff --git a/src/functions/_index.ts b/src/functions/_index.ts index 5dbc5f9..215627d 100644 --- a/src/functions/_index.ts +++ b/src/functions/_index.ts @@ -1,7 +1,9 @@ import { pollReactions } from './pollReactions.ts'; import { onlyOneReaction } from './onlyOneReaction.ts'; +import { cleanRaidCheckpointChannel } from './cleanRaidCheckpointChannel.ts'; export default { + cleanRaidCheckpointChannel, pollReactions, onlyOneReaction, }; diff --git a/src/functions/cleanRaidCheckpointChannel.ts b/src/functions/cleanRaidCheckpointChannel.ts new file mode 100644 index 0000000..2826578 --- /dev/null +++ b/src/functions/cleanRaidCheckpointChannel.ts @@ -0,0 +1,19 @@ +import { Bot } from "../../deps.ts"; +import utils from "../utils.ts"; + +export const cleanRaidCheckpointChannel = async (bot: Bot, channelId: bigint) => { + try { + // Get messages in channel, sort them oldest=>newest, and filter to messages from followed servers + const messages = (await bot.helpers.getMessages(channelId)).array().sort((a, b) => a.timestamp - b.timestamp).filter(msg => msg.isFromBot && msg.messageReference); + + // Remove most recent message from array + messages.pop(); + + // Delete all other messages + for (const message of messages) { + bot.helpers.deleteMessage(message.channelId, message.id, 'Old Checkpoint Message').catch(e => utils.commonLoggers.messageDeleteError('cleanRaidCheckpointChannel.ts:14', message, e)); + } + } catch (e) { + utils.commonLoggers.messageGetError('cleanRaidCheckpointChannel.ts:17', 'Something broke', e) + } +};