import { GuildChannel , Snowflake , TextChannel } from 'discord.js' ; import mongoose , { Document } from 'mongoose' ; import Constants from '../../common/constants' ; import { Plugin } from '../../common/plugin' ; import { IContainer , IMessage , ChannelType , ClassType , RoleType } from '../../common/types' ; import { ClassPinModel } from '../../schemas/class.schema' ; export default class StorePinsPlugin extends Plugin { public commandName : string = 'storepins' ; public name : string = 'Pin Plugin' ; public description : string = 'pins messages to the database' ; public usage : string = 'storepins' ; public override pluginAlias = [ ] ; public permission : ChannelType = ChannelType . Staff ; public override pluginChannelName : string = Constants . Channels . Staff . ModCommands ; public override minRoleToRun : RoleType = RoleType . Admin ; public override commandPattern : RegExp = /(confirm)?/ ; private _state : boolean = false ; constructor ( public container ) { super ( ) ; } public async execute ( message , args ) { const classChannels = Array . from ( this . container . classService . getClasses ( ClassType . ALL ) . values ( ) ) ; if ( ! this . _state ) { message . reply ( `Are you sure you want to store *all* the pins in all \`${ classChannels . length }\` class channels?\n` + 'Reply with `!storepins confirm` if you are sure' ) ; this . _state = true ; return ; } const [ subCommand ] = args ; if ( subCommand . toLowerCase ( ) !== 'confirm' ) { message . reply ( 'Ok, canceling.' ) ; this . _state = false ; return ; } message . channel . send ( `Storing pins in \`${ classChannels . length }\` channels. I will let you know when I am done` ) ; if ( ! mongoose . connection . readyState ) { message . channel . send ( 'Error connecting to the DB' ) ; return ; } const allChanPins = ( await Promise . all ( classChannels . map ( ( chan ) => this . _getPinsInChannel ( chan as TextChannel ) ) ) ) . flat ( ) ; await ClassPinModel . insertMany ( allChanPins ) ; message . channel . send ( `Stored \`${ allChanPins . length }\` pins in \`${ classChannels . length }\` channels` ) ; this . _state = false ; } private async _getPinsInChannel ( channel ) { return [ ... ( await channel . messages . fetchPinned ( ) ) . values ( ) ] . map ( ( pin ) => { return { messageContent : pin . content , className : channel . name , date : new Date ( ) , guildID : channel . guild . id , } ; } ) ; } } export interface IClassPin { messageContent : string ; className : string ; date : Date ; guildID : Snowflake ; } export type ClassPinDocument = IClassPin & Document ;