-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lockdown): Add lockdown command
- Loading branch information
Marco (Valandur)
committed
Dec 1, 2019
1 parent
1fb4b5f
commit 8c8d286
Showing
6 changed files
with
161 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { Channel, Message, PermissionOverwrite, Role, TextChannel } from 'eris'; | ||
import { Duration } from 'moment'; | ||
|
||
import { IMClient } from '../../../client'; | ||
import { Command, Context } from '../../../framework/commands/Command'; | ||
import { ScheduledActionType } from '../../../framework/models/ScheduledAction'; | ||
import { ChannelResolver, DurationResolver } from '../../../framework/resolvers'; | ||
import { CommandGroup, ModerationCommand } from '../../../types'; | ||
|
||
const SEND_MESSAGES = 0x00000800; | ||
const NOT_SEND_MESSAGES = 0x7ffff7ff; | ||
|
||
// tslint:disable: no-bitwise | ||
export default class extends Command { | ||
public constructor(client: IMClient) { | ||
super(client, { | ||
name: ModerationCommand.lockdown, | ||
aliases: [], | ||
args: [ | ||
{ | ||
name: 'channel', | ||
resolver: ChannelResolver, | ||
required: false | ||
} | ||
], | ||
flags: [ | ||
{ | ||
name: 'timeout', | ||
resolver: DurationResolver, | ||
short: 't' | ||
} | ||
], | ||
group: CommandGroup.Moderation, | ||
defaultAdminOnly: true, | ||
guildOnly: true | ||
}); | ||
} | ||
|
||
public async action( | ||
message: Message, | ||
[channel]: [Channel], | ||
{ timeout }: { timeout: Duration }, | ||
{ guild, me, settings, t }: Context | ||
): Promise<any> { | ||
channel = channel || message.channel; | ||
|
||
if (!(channel instanceof TextChannel)) { | ||
await this.sendReply(message, t('cmd.lockdown.notATextChannel')); | ||
return; | ||
} | ||
|
||
// Get lowest role that has write permissions | ||
const scheduledUnlockActions = await this.client.scheduler.getScheduledActionsOfType( | ||
guild.id, | ||
ScheduledActionType.unlock | ||
); | ||
const scheduledUnlockAction = scheduledUnlockActions.find(action => action.args.channelId === channel.id); | ||
|
||
if (scheduledUnlockAction) { | ||
const override = channel.permissionOverwrites.get(scheduledUnlockAction.args.roleId); | ||
const newAllow = scheduledUnlockAction.args.wasAllowed ? SEND_MESSAGES : 0; | ||
await this.client.editChannelPermission( | ||
scheduledUnlockAction.args.channelId, | ||
scheduledUnlockAction.args.roleId, | ||
override ? override.allow | newAllow : newAllow, | ||
override ? override.deny & NOT_SEND_MESSAGES : 0, | ||
'role', | ||
'Channel lockdown' | ||
); | ||
await this.client.scheduler.removeScheduledAction(guild.id, scheduledUnlockAction.id); | ||
|
||
await this.sendReply(message, t('cmd.lockdown.channelUnlocked')); | ||
|
||
return; | ||
} | ||
|
||
let lowestRole: Role | null = null; | ||
let lowestOverride: PermissionOverwrite | null = null; | ||
for (const [id, value] of channel.permissionOverwrites) { | ||
if (value.type === 'member') { | ||
continue; | ||
} | ||
|
||
if ((value.deny & SEND_MESSAGES) === SEND_MESSAGES) { | ||
continue; | ||
} | ||
|
||
const role = guild.roles.get(id); | ||
if (lowestRole && lowestRole.position < role.position) { | ||
continue; | ||
} | ||
|
||
lowestRole = role; | ||
lowestOverride = value; | ||
} | ||
|
||
if (!lowestRole) { | ||
await this.sendReply(message, t('cmd.lockdown.noSuitingRoleFound')); | ||
} | ||
|
||
await this.client.scheduler.addScheduledAction( | ||
guild.id, | ||
ScheduledActionType.unlock, | ||
{ | ||
channelId: channel.id, | ||
roleId: lowestRole.id, | ||
wasAllowed: !!(lowestOverride.allow & SEND_MESSAGES) | ||
}, | ||
null, | ||
'Unlock from timed `!lockdown` command' | ||
); | ||
|
||
await this.client.editChannelPermission(channel.id, me.id, SEND_MESSAGES, 0, 'member', 'Channel lockdown'); | ||
await this.client.editChannelPermission( | ||
channel.id, | ||
lowestRole.id, | ||
lowestOverride.allow & NOT_SEND_MESSAGES, | ||
lowestOverride.deny | SEND_MESSAGES, | ||
'role', | ||
'Channel lockdown' | ||
); | ||
|
||
await this.sendReply(message, t('cmd.lockdown.channelLockedDown')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters