Skip to content

Commit

Permalink
fix(lockdown): Implement timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Crespi committed Mar 26, 2020
1 parent 8beeeaf commit fd226e6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
3 changes: 2 additions & 1 deletion i18n/bot/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,8 @@
"args": {
"channel": "The channel that you want to lock down."
}
}
},
"noSuitingRoleFound": "No suitable role found to start a lockdown!"
}
},
"JOIN_LEAVE_EMBEDS_IS_PREMIUM": "Using an embed as your join/leave message is a premium feature. Premium does not seem to be active on this server. Please contact us to learn more about how to purchase premium.",
Expand Down
35 changes: 32 additions & 3 deletions src/framework/services/Scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { ScheduledAction, ScheduledActionType } from '../models/ScheduledAction'

import { IMService } from './Service';

const SEND_MESSAGES = 0x00000800;
const NOT_SEND_MESSAGES = 0x7ffff7ff;

export class SchedulerService extends IMService {
private scheduledActionTimers: Map<number, NodeJS.Timer> = new Map();
private scheduledActionFunctions: {
Expand Down Expand Up @@ -68,7 +71,7 @@ export class SchedulerService extends IMService {
});
}
};
console.log(`Scheduling timer in ${millisUntilAction} for ${action.id}`);
console.log(`Scheduling timer in ${chalk.blue(millisUntilAction)} for action ${chalk.blue(action.id)}`);
const timer = setTimeout(func, millisUntilAction);
this.scheduledActionTimers.set(action.id, timer);
}
Expand All @@ -86,7 +89,7 @@ export class SchedulerService extends IMService {
private async scheduleScheduledActions() {
let actions = await this.client.db.getScheduledActionsForGuilds(this.client.guilds.map((g) => g.id));
actions = actions.filter((a) => a.date !== null);
console.log(`Scheduling ${chalk.blue(actions.length)} actions from db`);
console.log(`Scheduling ${chalk.blue(actions.length)} actions from DB`);
actions.forEach((action) => this.createTimer(action));
}

Expand All @@ -109,7 +112,33 @@ export class SchedulerService extends IMService {
await member.removeRole(roleId, 'Timed unmute');
}

private async unlock(guild: Guild, { channelId, roleId }: { channelId: string; roleId: string }) {
private async unlock(
guild: Guild,
{ channelId, roleId, wasAllowed }: { channelId: string; roleId: string; wasAllowed: boolean }
) {
console.log('SCHEDULED TASK: UNLOCK', guild.id, channelId, roleId);

let channel = guild.channels.get(channelId);
if (!channel) {
await guild.getRESTChannels();
channel = guild.channels.get(channelId);
}
if (!channel) {
console.error('SCHEDULED TASK: UNLOCK: COULD NOT FIND CHANNEL', channelId);
return;
}

const override = channel.permissionOverwrites.get(roleId);
const newAllow = wasAllowed ? SEND_MESSAGES : 0;

// tslint:disable: no-bitwise
await this.client.editChannelPermission(
channelId,
roleId,
override ? override.allow | newAllow : newAllow,
override ? override.deny & NOT_SEND_MESSAGES : 0,
'role',
'Channel lockdown'
);
}
}
6 changes: 4 additions & 2 deletions src/moderation/commands/mod/lockdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Channel, Message, PermissionOverwrite, Role, TextChannel } from 'eris';
import { Duration } from 'moment';
import moment, { Duration } from 'moment';

import { IMClient } from '../../../client';
import { Command, Context } from '../../../framework/commands/Command';
Expand Down Expand Up @@ -100,6 +100,8 @@ export default class extends Command {
return;
}

// We always add a scheduled actions so that we know what to restore on unlock
// But if the user didn't specify a timeout then we set it to null so they must do it manually
await this.client.scheduler.addScheduledAction(
guild.id,
ScheduledActionType.unlock,
Expand All @@ -108,7 +110,7 @@ export default class extends Command {
roleId: lowestRole.id,
wasAllowed: !!(lowestOverride.allow & SEND_MESSAGES)
},
null,
timeout ? moment().add(timeout).toDate() : null,
'Unlock from timed `!lockdown` command'
);

Expand Down
2 changes: 1 addition & 1 deletion src/moderation/commands/mod/mute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default class extends Command {
guild.id,
ScheduledActionType.unmute,
{ memberId: targetMember.id, roleId: mutedRole },
moment().locale(settings.lang).add(duration).toDate(),
moment().add(duration).toDate(),
'Unmute from timed `!mute` command'
);
}
Expand Down

0 comments on commit fd226e6

Please sign in to comment.