import { Message , Guild , TextChannel } from 'discord.js' ; import { commandInterface } from '../../commands' ; import { permLevels } from '../../utils/permissions' ; import { Bot } from '../..' ; import { sendError } from '../../utils/messages' ; import { permToString , durationToString , stringToDuration , stringToChannel } from '../../utils/parsers' ; import { durations } from '../../utils/time' ; import { staffObject , guildObject } from '../../database/schemas' ; param { TextChannel } channel returns async function getIDsToOverwrite ( channel ) { let denyIDs = [ ] ; let allowIDs = [ ] ; for ( const overwrite of channel . permissionOverwrites . array ( ) ) { denyIDs . push ( overwrite . id ) ; } let staffDoc = await Bot . database . findStaffDoc ( channel . guild . id ) ; if ( staffDoc ) { let staffObject = staffDoc . toObject ( ) ; allowIDs = allowIDs . concat ( staffObject . admins . users ) . concat ( staffObject . mods . users ) . concat ( staffObject . immune . users ) . concat ( staffObject . admins . roles ) . concat ( staffObject . mods . roles ) . concat ( staffObject . immune . roles ) ; denyIDs = denyIDs . filter ( id => ! allowIDs . includes ( id ) ) ; } let allowOverwrites = [ ] ; let neutralOverwrites = [ ] ; for ( let i = 0 ; i < allowIDs . length ; ) { let permOverwrite = channel . permissionOverwrites . get ( allowIDs [ i ] ) ; if ( permOverwrite && ( permOverwrite . allowed . has ( 'SEND_MESSAGES' ) || permOverwrite . denied . has ( 'SEND_MESSAGES' ) ) ) { allowIDs . splice ( i , 1 ) ; } else { neutralOverwrites . push ( allowIDs [ i ] ) ; i ++ ; } } for ( let i = 0 ; i < denyIDs . length ; ) { let permOverwrite = channel . permissionOverwrites . get ( denyIDs [ i ] ) ; if ( permOverwrite && permOverwrite . denied . has ( 'SEND_MESSAGES' ) ) { denyIDs . splice ( i , 1 ) ; continue ; } if ( permOverwrite && permOverwrite . allowed . has ( 'SEND_MESSAGES' ) ) { allowOverwrites . push ( denyIDs [ i ] ) ; } else { neutralOverwrites . push ( denyIDs [ i ] ) ; } i ++ ; } return { allow : allowIDs , deny : denyIDs , allowOverwrites : allowOverwrites , neutralOverwrites : neutralOverwrites } ; } var command = { name : 'lock' , path : '' , dm : false , permLevel : permLevels . mod , togglable : false , cooldownLocal : durations . second , help : { shortDescription : 'Lock a channel for normal members' , longDescription : 'Removes the write permissions of all normal members. Immune get excluded.' , usages : [ '{command} [channel]' , '{command} [channel] [time]' ] , examples : [ '{command} #general' , '{command} #general 2h10m' ] } , run : async ( message , args , permLevel , dm , requestTime ) => { try { if ( args . length == 0 ) { message . channel . send ( await Bot . commands . getHelpEmbed ( command , message . guild ) ) ; Bot . mStats . logMessageSend ( ) ; return false ; } if ( ! message . guild . me . hasPermission ( 'MANAGE_CHANNELS' ) ) { message . channel . send ( 'I don\'t have the `Manage Channels` permission' ) ; Bot . mStats . logMessageSend ( ) ; return false ; } let argIndex = 0 ; let argsArray = args . split ( ' ' ) . filter ( x => x . length != 0 ) ; let channel = stringToChannel ( message . guild , argsArray [ argIndex ] , true , false ) ; if ( ! channel ) { message . channel . send ( 'Couldn\'t find specified channel' ) ; Bot . mStats . logMessageSend ( ) ; return false ; } if ( ! ( channel instanceof TextChannel ) ) { message . channel . send ( 'You can only lock text channels' ) ; Bot . mStats . logMessageSend ( ) ; return false ; } argIndex ++ ; let time = stringToDuration ( argsArray [ argIndex ] ) ; let timeString = time ? durationToString ( time ) : 'an indefinite time' ; let overwrites = await getIDsToOverwrite ( channel ) ; let query = { guild : message . guild . id } ; query [ `locks.${ channel . id }` ] = { $exists : true } ; let existingLock = await Bot . database . mainDB . guilds . findOne ( query , [ `locks.${ channel . id }.allowOverwrites` , `locks.${ channel . id }.neutralOverwrites` ] ) . exec ( ) ; if ( ! existingLock ) { if ( ! overwrites . allow . length && ! overwrites . deny . length ) { Bot . mStats . logResponseTime ( command . name , requestTime ) ; message . channel . send ( `:x: **${ channel } already has role perms that are considered locked**` ) ; Bot . mStats . logCommandUsage ( command . name , 'failedCreate' ) ; Bot . mStats . logMessageSend ( ) ; return ; } for ( const id of overwrites . deny ) channel . overwritePermissions ( id , { SEND_MESSAGES : false } ) ; for ( const id of overwrites . allow ) channel . overwritePermissions ( id , { SEND_MESSAGES : true } ) ; let updateDoc = { } ; updateDoc [ `locks.${ channel . id }` ] = { allowOverwrites : overwrites . allowOverwrites , neutralOverwrites : overwrites . neutralOverwrites } ; if ( time ) updateDoc [ `locks.${ channel . id }` ] . until = message . createdTimestamp + time ; Bot . database . mainDB . guilds . updateOne ( { guild : message . guild . id } , updateDoc ) . exec ( ) ; } else { let updateDoc = { } ; if ( time ) { updateDoc [ `locks.${ channel . id }.until` ] = message . createdTimestamp + time ; } else { updateDoc [ '$unset' ] = { } ; updateDoc [ '$unset' ] [ `locks.${ channel . id }.until` ] = "" ; Bot . pActions . removeLockChannel ( message . guild . id , channel . id ) ; } Bot . database . mainDB . guilds . updateOne ( { guild : message . guild . id } , updateDoc ) . exec ( ) ; let guildObject = existingLock . toObject ( ) ; overwrites . allowOverwrites = guildObject . locks [ channel . id ] . allowOverwrites ; overwrites . neutralOverwrites = guildObject . locks [ channel . id ] . neutralOverwrites ; } if ( time ) Bot . pActions . addLockChannel ( message . guild . id , channel . id , overwrites . allowOverwrites , overwrites . neutralOverwrites , message . createdTimestamp + time , message . createdTimestamp ) ; if ( channel . id != message . channel . id ) { channel . send ( `Channel has been locked for ${ timeString }` ) ; Bot . mStats . logMessageSend ( ) ; } Bot . mStats . logResponseTime ( command . name , requestTime ) ; if ( existingLock ) { message . channel . send ( `:white_check_mark: **Lock time for ${ channel } has been changed to ${ timeString }**` ) ; Bot . mStats . logCommandUsage ( command . name , 'change' ) ; } else { message . channel . send ( `:white_check_mark: **${ channel } has been locked for ${ timeString }**` ) ; Bot . mStats . logCommandUsage ( command . name , 'create' ) ; } Bot . mStats . logMessageSend ( ) ; return true ; } catch ( e ) { sendError ( message . channel , e ) ; Bot . mStats . logError ( e , command . name ) ; return false ; } } } ; export default command ;