import mongoose = require ( 'mongoose' ) ; import { PresenceData , DMChannel , GroupDMChannel , TextChannel , User , Snowflake } from 'discord.js' ; import { Bot } from '..' ; export interface guildObject { guild : string ; logChannel : string ; caseChannel : string ; totalCases : number ; logs : mongoose . Schema . Types . ObjectId [ ] ; staff : mongoose . Schema . Types . ObjectId ; modmailChannel : string ; webhooks : { [ key ] : mongoose . Schema . Types . ObjectId [ ] ; } ; locks : { [ key ] : { until ? : number ; allowOverwrites : string [ ] ; neutralOverwrites : string [ ] ; } ; } ; muted ? : Snowflake [ ] ; } export interface guildDoc extends mongoose . Document , guildObject { } export const guildSchema = new mongoose . Schema ( { guild : String , logChannel : String , caseChannel : String , totalCases : Number , modmailChannel : String , logs : [ mongoose . Schema . Types . ObjectId ] , staff : mongoose . Schema . Types . ObjectId , webhooks : { youtube : [ mongoose . Schema . Types . ObjectId ] } , locks : mongoose . Schema . Types . Mixed , muted : { required : false , type : [ String ] } } ) ; export interface staffObject { guild : string ; admins : { roles : string [ ] ; users : string [ ] ; } ; mods : { roles : string [ ] ; users : string [ ] ; } ; immune : { roles : string [ ] ; users : string [ ] ; } ; } export interface staffDoc extends mongoose . Document , staffObject { } export const staffSchema = new mongoose . Schema ( { guild : String , admins : { roles : [ String ] , users : [ String ] } , mods : { roles : [ String ] , users : [ String ] } , immune : { roles : [ String ] , users : [ String ] } } ) ; export interface prefixObject { guild : string ; prefix : string ; } export interface prefixDoc extends mongoose . Document , prefixObject { } export const prefixSchema = new mongoose . Schema ( { guild : String , prefix : String } ) ; export interface commandsObject { guild : string ; commands : { [ key ] : { _enabled : boolean ; [ key ] : any ; } ; } ; } export interface commandsDoc extends mongoose . Document , commandsObject { } export const commandsSchema = new mongoose . Schema ( { guild : String , commands : mongoose . Schema . Types . Mixed } ) ; export interface filtersObject { guild : string ; filters : { [ key ] : { _enabled : boolean ; [ key ] : any ; } ; } ; } export interface filtersDoc extends mongoose . Document , filtersObject { } export const filtersSchema = new mongoose . Schema ( { guild : String , filters : mongoose . Schema . Types . Mixed } ) ; export interface logObject { guild : string ; action : number ; mod : string ; timestamp : number ; info ? : logStaff | logWebhook | logFilter | logCommand | logPrefix | logMegalog | logMegalogIgnore ; } export interface logDoc extends mongoose . Document , logObject { } export const logSchema = new mongoose . Schema ( { guild : String , action : Number , mod : String , timestamp : Number , info : mongoose . Schema . Types . Mixed } ) ; export enum { number } export enum logTypes { add = 0 , remove = 1 , change = 2 } export enum { number } export enum logActions { staff = 0 , webhook = 1 , filter = 2 , command = 3 , prefix = 4 , megalog = 5 , megalogIgnore = 6 } export interface logStaff { type : logTypes . add | logTypes . remove ; rank : 'admins' | 'mods' | 'immune' ; role ? : string ; user ? : string ; } export interface logWebhook { type : logTypes ; service : string ; webhookID : string ; changedChannel ? : boolean ; changedMessage ? : boolean ; } export interface logFilter { type : logTypes . add | logTypes . remove ; filter : string ; } export interface logCommand { type : logTypes . add | logTypes . remove ; command : string ; } export interface logPrefix { old : string ; new : string ; } export interface logMegalog { type : logTypes . add | logTypes . remove ; functions : string [ ] ; channel ? : string ; } export interface logMegalogIgnore { type : logTypes . add | logTypes . remove ; channel ? : string ; } export interface commandCacheObject { channel : string ; user : string ; command : string ; cache : any ; delete : number ; } export interface commandCacheDoc extends mongoose . Document , commandCacheObject { } export const commandCacheSchema = new mongoose . Schema ( { channel : String , user : String , command : String , cache : mongoose . Schema . Types . Mixed , delete : Number } ) ; export class implements commandCacheObject export class CommandCache { channel : DMChannel | GroupDMChannel | TextChannel ; user : User ; command : string ; cache : any ; delete : number ; doc : commandCacheDoc ; param { commandCacheDoc } commandCacheDoc param { string } channel param { string } user param { string } command param { number } cacheTime param { * } cache memberof constructor ( commandCacheDoc , channel ? , user ? , command ? , cacheTime ? , cache = { } ) { if ( commandCacheDoc ) { this . doc = commandCacheDoc ; var commandCacheObject = commandCacheDoc . toObject ( ) ; this . channel = Bot . client . channels . get ( commandCacheObject . channel ) ; Bot . client . fetchUser ( commandCacheObject . user ) . then ( user => this . user ) ; this . command = commandCacheObject . command ; this . cache = commandCacheObject . cache ; this . delete = commandCacheObject . delete ; } else { this . delete = Date . now ( ) + cacheTime ; this . channel = channel ; this . user = user ; this . command = command ; this . cache = cache ; this . doc = new Bot . database . mainDB . commandCache ( { channel : channel . id , user : user . id , command : command , cache : cache , delete : this . delete } ) ; this . doc . save ( ) ; } } param { number } newCacheTime returns memberof save ( newCacheTime ? ) { this . doc . cache = this . cache ; this . doc . markModified ( 'cache' ) ; if ( newCacheTime ) this . doc . delete = Date . now ( ) + newCacheTime ; return this . doc . save ( ) ; } returns memberof remove ( ) { return this . doc . remove ( ) ; } } export interface userObject { user : string ; commandCooldown : { [ key ] : { [ key ] : number ; } ; } ; } export interface userDoc extends mongoose . Document , userObject { } export const userSchema = new mongoose . Schema ( { user : String , commandCooldown : mongoose . Schema . Types . Mixed } ) ; export class export class UserWrapper { user : User ; commandCooldown : { [ key ] : { [ key ] : number ; } ; } ; doc : userDoc ; param { userDoc } userDoc param { User } user memberof constructor ( userDoc , user ? ) { this . user = user ; if ( userDoc ) { this . doc = userDoc ; var userObject = this . doc . toObject ( ) ; if ( ! this . user ) Bot . client . fetchUser ( userObject . user ) . then ( user => this . user ) ; this . commandCooldown = userObject . commandCooldown ; } else { if ( ! user ) throw new Error ( "Both userDoc and user weren't specified" ) ; this . commandCooldown = { } ; this . doc = new Bot . database . mainDB . users ( { user : user . id , commandCooldown : { } } ) ; } } returns memberof save ( ) { this . doc . commandCooldown = this . commandCooldown ; this . doc . markModified ( 'commandCooldown' ) ; return this . doc . save ( ) ; } returns memberof remove ( ) { return this . doc . remove ( ) ; } param { string } scope param { string } command returns memberof getCooldown ( scope , command ) { if ( ! this . commandCooldown || ! this . commandCooldown [ scope ] || ! this . commandCooldown [ scope ] [ command ] ) return 0 ; return this . commandCooldown [ scope ] [ command ] ; } param { string } scope param { string } command param { number } timestamp param { boolean } save returns memberof setCooldown ( scope , command , timestamp , save = true ) { if ( isNaN ( Number ( scope ) ) && scope != 'dm' && scope != 'global' ) throw new Error ( "scope should be guild id, 'dm' or 'global' but is '" + scope + "'" ) ; if ( ! this . commandCooldown ) this . commandCooldown = { } ; if ( ! this . commandCooldown [ scope ] ) this . commandCooldown [ scope ] = { } ; if ( timestamp ) { this . commandCooldown [ scope ] [ command ] = timestamp ; } else { delete this . commandCooldown [ scope ] [ command ] ; } if ( save ) { this . doc . commandCooldown = this . commandCooldown ; this . doc . markModified ( `commandCooldown.${ scope }.${ command }` ) ; return this . doc . save ( ) ; } } param { string } scope param { boolean } save returns memberof resetCooldown ( scope , save = true ) { if ( ! this . commandCooldown [ scope ] ) return ; delete this . commandCooldown [ scope ] ; if ( save ) { this . doc . commandCooldown = this . commandCooldown ; this . doc . markModified ( 'commandCooldown' ) ; return this . doc . save ( ) ; } } } export interface megalogObject { guild : string ; ignoreChannels : string [ ] ; channelCreate : string ; channelDelete : string ; channelUpdate : string ; ban : string ; unban : string ; memberJoin : string ; memberLeave : string ; nicknameChange : string ; memberRolesChange : string ; guildNameChange : string ; messageDelete : string ; attachmentCache : string ; messageEdit : string ; reactionAdd : string ; reactionRemove : string ; roleCreate : string ; roleDelete : string ; roleUpdate : string ; voiceTransfer : string ; voiceMute : string ; voiceDeaf : string ; } export interface megalogDoc extends mongoose . Document , megalogObject { } export const megalogSchema = new mongoose . Schema ( { guild : String , ignoreChannels : [ String ] , channelCreate : { type : String , required : false } , channelDelete : { type : String , required : false } , channelUpdate : { type : String , required : false } , ban : { type : String , required : false } , unban : { type : String , required : false } , memberJoin : { type : String , required : false } , memberLeave : { type : String , required : false } , nicknameChange : { type : String , required : false } , memberRolesChange : { type : String , required : false } , guildNameChange : { type : String , required : false } , messageDelete : { type : String , required : false } , attachmentCache : { type : String , required : false } , messageEdit : { type : String , required : false } , reactionAdd : { type : String , required : false } , reactionRemove : { type : String , required : false } , roleCreate : { type : String , required : false } , roleDelete : { type : String , required : false } , roleUpdate : { type : String , required : false } , voiceTransfer : { type : String , required : false } , voiceMute : { type : String , required : false } , voiceDeaf : { type : String , required : false } } ) ; export const megalogFunctions = { all : [ 'channelCreate' , 'channelDelete' , 'channelUpdate' , 'ban' , 'unban' , 'memberJoin' , 'memberLeave' , 'nicknameChange' , 'memberRolesChange' , 'guildNameChange' , 'messageDelete' , 'attachmentCache' , 'messageEdit' , 'reactionAdd' , 'reactionRemove' , 'roleCreate' , 'roleDelete' , 'roleUpdate' , 'voiceTransfer' , 'voiceMute' , 'voiceDeaf' ] , channels : [ 'channelCreate' , 'channelDelete' , 'channelUpdate' ] , members : [ 'memberJoin' , 'memberLeave' , 'memberRolesChange' ] , roles : [ 'roleCreate' , 'roleDelete' , 'roleUpdate' ] , voice : [ 'voiceTransfer' , 'voiceMute' , 'voiceDeaf' ] , messages : [ 'messageDelete' , 'attachmentCache' , 'messageEdit' ] , reactions : [ 'reactionAdd' , 'reactionRemove' ] } ; export enum pActionActions { mute = 'mute' , lockChannel = 'lockChannel' , ban = 'ban' , resubWebhook = 'resubWebhook' } export interface pActionObject { from : number ; to : number ; action : pActionActions ; info : pActionMute | pActionBan | pActionLockChannel | pActionResubWebhook ; } ; export interface pActionDoc extends mongoose . Document , pActionObject { } ; export const pActionSchema = new mongoose . Schema ( { from : Number , to : Number , action : String , info : mongoose . Schema . Types . Mixed } ) ; export interface pActionMute { guild : string ; user : string ; case : number ; } export interface pActionBan { guild : string ; user : string ; case : number ; } export interface pActionLockChannel { guild : string ; channel : string ; allowOverwrites : string [ ] ; neutralOverwrites : string [ ] ; } export interface pActionResubWebhook { service : string ; } export enum caseActions { ban = 'ban' , warn = 'warn' , mute = 'mute' , kick = 'kick' , softban = 'softban' , unmute = 'unmute' , unban = 'unban' } ; export const caseActionsArray = [ 'ban' , 'warn' , 'mute' , 'kick' , 'softban' , 'unmute' , 'unban' ] ; export interface caseObject { guild : string ; caseID : number ; user : string ; action : string ; timestamp : number ; duration ? : number ; mod : string ; reason ? : string ; } export interface caseDoc extends mongoose . Document , caseObject { } export const caseSchema = new mongoose . Schema ( { guild : String , caseID : Number , user : String , action : String , timestamp : Number , duration : { type : Number , required : false } , mod : String , reason : { type : String , required : false } , } ) ; export interface mStatsObject { messagesReceived : number ; messagesSend : number ; logs : number ; guildsJoined : number ; guildsLeft : number ; guildsTotal : number ; errorsTotal : number ; bugs : number ; botSuggestions : number ; commandTotal : number ; commands : { [ key ] : { _errors : number ; _resp : number ; _main ? : number ; [ key ] : number ; } ; } ; filters : { [ key ] : number ; } ; webhooks : { [ key ] : { total : number ; created : number ; changed : number ; deleted : number ; } ; } ; ping : { clientAPI : number ; cluster : number ; } ; megalog : { enabled : { channelCreate : number ; channelDelete : number ; channelUpdate : number ; ban : number ; unban : number ; memberJoin : number ; memberLeave : number ; nicknameChange : number ; memberRolesChange : number ; guildNameChange : number ; messageDelete : number ; attachmentCache : number ; messageEdit : number ; reactionAdd : number ; reactionRemove : number ; roleCreate : number ; roleDelete : number ; roleUpdate : number ; voiceTranfer : number ; voiceMute : number ; voiceDeaf : number ; } ; logged : { channelCreate : number ; channelDelete : number ; channelUpdate : number ; ban : number ; unban : number ; memberJoin : number ; memberLeave : number ; nicknameChange : number ; memberRolesChange : number ; guildNameChange : number ; messageDelete : number ; attachmentCache : number ; messageEdit : number ; reactionAdd : number ; reactionRemove : number ; roleCreate : number ; roleDelete : number ; roleUpdate : number ; voiceTranfer : number ; voiceMute : number ; voiceDeaf : number ; } ; } ; } export function createEmptyMStatsObject ( ) { return { messagesReceived : 0 , messagesSend : 0 , logs : 0 , guildsJoined : 0 , guildsLeft : 0 , guildsTotal : 0 , errorsTotal : 0 , bugs : 0 , botSuggestions : 0 , commandTotal : 0 , commands : { } , filters : { } , webhooks : { } , ping : { clientAPI : 0 , cluster : 0 } , megalog : { enabled : { channelCreate : 0 , channelDelete : 0 , channelUpdate : 0 , ban : 0 , unban : 0 , memberJoin : 0 , memberLeave : 0 , nicknameChange : 0 , memberRolesChange : 0 , guildNameChange : 0 , messageDelete : 0 , attachmentCache : 0 , messageEdit : 0 , reactionAdd : 0 , reactionRemove : 0 , roleCreate : 0 , roleDelete : 0 , roleUpdate : 0 , voiceTranfer : 0 , voiceMute : 0 , voiceDeaf : 0 } , logged : { channelCreate : 0 , channelDelete : 0 , channelUpdate : 0 , ban : 0 , unban : 0 , memberJoin : 0 , memberLeave : 0 , nicknameChange : 0 , memberRolesChange : 0 , guildNameChange : 0 , messageDelete : 0 , attachmentCache : 0 , messageEdit : 0 , reactionAdd : 0 , reactionRemove : 0 , roleCreate : 0 , roleDelete : 0 , roleUpdate : 0 , voiceTranfer : 0 , voiceMute : 0 , voiceDeaf : 0 } } } ; } const mStatsSchemaStruc = { messagesReceived : Number , messagesSend : Number , logs : Number , guildsJoined : Number , guildsLeft : Number , guildsTotal : Number , errorsTotal : Number , bugs : Number , botSuggestions : Number , commandTotal : Number , commands : mongoose . Schema . Types . Mixed , filters : mongoose . Schema . Types . Mixed , webhooks : mongoose . Schema . Types . Mixed , ping : { clientAPI : Number , cluster : Number } , megalog : { enabled : { channelCreate : Number , channelDelete : Number , channelUpdate : Number , ban : Number , unban : Number , memberJoin : Number , memberLeave : Number , nicknameChange : Number , memberRolesChange : Number , guildNameChange : Number , messageDelete : Number , attachmentCache : Number , messageEdit : Number , reactionAdd : Number , reactionRemove : Number , roleCreate : Number , roleDelete : Number , roleUpdate : Number , voiceTranfer : Number , voiceMute : Number , voiceDeaf : Number } , logged : { channelCreate : Number , channelDelete : Number , channelUpdate : Number , ban : Number , unban : Number , memberJoin : Number , memberLeave : Number , nicknameChange : Number , memberRolesChange : Number , guildNameChange : Number , messageDelete : Number , attachmentCache : Number , messageEdit : Number , reactionAdd : Number , reactionRemove : Number , roleCreate : Number , roleDelete : Number , roleUpdate : Number , voiceTranfer : Number , voiceMute : Number , voiceDeaf : Number } } } ; export interface mStatsAllTimeObject extends mStatsObject { from : number ; to : number ; } export interface mStatsAllTimeDoc extends mongoose . Document , mStatsAllTimeObject { } var mStatsAllTimeSchemaStruc = mStatsSchemaStruc ; mStatsAllTimeSchemaStruc . from = Number ; mStatsAllTimeSchemaStruc . to = Number ; export const mStatsAllTimeSchema = new mongoose . Schema ( mStatsAllTimeSchemaStruc ) ; export interface mStatsDayObject extends mStatsObject { day : number ; } export interface mStatsDayDoc extends mongoose . Document , mStatsDayObject { } var mStatsDaySchemaStruc = mStatsSchemaStruc ; mStatsDaySchemaStruc . day = Number ; export const mStatsDaySchema = new mongoose . Schema ( mStatsDaySchemaStruc ) ; export interface mStatsHourObject extends mStatsObject { day : number ; hour : number ; } export interface mStatsHourDoc extends mongoose . Document , mStatsDayObject { } var mStatsHourSchemaStruc = mStatsSchemaStruc ; mStatsHourSchemaStruc . day = Number ; mStatsHourSchemaStruc . hour = Number ; export const mStatsHourSchema = new mongoose . Schema ( mStatsHourSchemaStruc ) ; export interface errorObject { first : number ; last : number ; md5 : string ; count : number ; error : any ; } export interface errorDoc extends mongoose . Document , errorObject { } export const errorSchema = new mongoose . Schema ( { first : Number , last : Number , md5 : String , count : Number , error : mongoose . Schema . Types . Mixed } ) ; export interface bugObject { guild ? : string ; user : string ; bug : string ; } export interface bugDoc extends mongoose . Document , bugObject { } export const bugSchema = new mongoose . Schema ( { guild : { type : String , required : false } , user : String , bug : String , } ) ; export interface botSuggestionObject { guild ? : string ; user : string ; suggestion : string ; } export interface botSuggestionDoc extends mongoose . Document , botSuggestionObject { } export const botSuggestionSchema = new mongoose . Schema ( { guild : { type : String , required : false } , user : String , suggestion : String , } ) ; export interface webhookObject { feed : string ; guild : string ; channel : string ; message : string ; } export interface webhookDoc extends mongoose . Document , webhookObject { } export const webhookSchema = new mongoose . Schema ( { feed : String , guild : String , channel : String , message : String } ) ; export interface globalSettingsObject { prefix : string ; presence : PresenceData ; embedColors : { default : number ; help : number ; neutral : number ; negative : number ; warn : number ; positive : number ; } ; botMasters : [ string ] ; commands : { [ key ] : { [ key ] : any ; } ; } ; filters : { [ key ] : { [ key ] : any ; } ; } ; } export interface globalSettingsDoc extends mongoose . Document , globalSettingsObject { } export const globalSettingsSchema = new mongoose . Schema ( { prefix : String , presence : { status : { type : String , required : false } , afk : { type : Boolean , required : false } , game : { name : { type : String , required : false } , url : { type : String , required : false } , type : { type : String , required : false } } } , embedColor : { default : Number , help : Number , neutral : Number , bad : Number , warn : Number , positive : Number } , botMasters : [ String ] , commands : mongoose . Schema . Types . Mixed , filters : mongoose . Schema . Types . Mixed } ) ;