109 lines
3.6 KiB
TypeScript
109 lines
3.6 KiB
TypeScript
|
/* eslint-disable no-bitwise */
|
||
|
import { Member } from 'eris';
|
||
|
import { v4 as uuid } from 'uuid';
|
||
|
import moment, { unitOfTime } from 'moment';
|
||
|
import { Client, RichEmbed } from '.';
|
||
|
import { Moderation as ModerationModel, ModerationInterface } from '../models';
|
||
|
import { moderation as channels } from '../configs/channels.json';
|
||
|
|
||
|
export default class Moderation {
|
||
|
public client: Client;
|
||
|
|
||
|
public logChannels: {
|
||
|
modlogs: string
|
||
|
};
|
||
|
|
||
|
constructor(client: Client) {
|
||
|
this.client = client;
|
||
|
this.logChannels.modlogs = channels.modlogs;
|
||
|
}
|
||
|
|
||
|
public checkPermissions(member: Member, moderator: Member): boolean {
|
||
|
if (member.id === moderator.id) return false;
|
||
|
if (member.roles.some((r) => ['662163685439045632', '455972169449734144', '453689940140883988'].includes(r))) return false;
|
||
|
const bit = member.permission.allow;
|
||
|
if ((bit | 8) === bit) return false;
|
||
|
if ((bit | 20) === bit) return false;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Converts some sort of time based duration to milliseconds based on length.
|
||
|
* @param time The time, examples: 2h, 1m, 1w
|
||
|
*/
|
||
|
public convertTimeDurationToMilliseconds(time: string): number {
|
||
|
const lockLength = time.match(/[a-z]+|[^a-z]+/gi);
|
||
|
const length = Number(lockLength[0]);
|
||
|
const unit = lockLength[1] as unitOfTime.Base;
|
||
|
return moment.duration(length, unit).asMilliseconds();
|
||
|
}
|
||
|
|
||
|
public async ban(member: Member, moderator: Member, duration: number, reason?: string): Promise<ModerationInterface> {
|
||
|
await member.ban(7, reason);
|
||
|
const logID = uuid();
|
||
|
const mod = new ModerationModel({
|
||
|
userID: member.id,
|
||
|
logID,
|
||
|
moderatorID: moderator.id,
|
||
|
reason: reason ?? null,
|
||
|
type: 5,
|
||
|
date: new Date(),
|
||
|
});
|
||
|
const now: number = Date.now();
|
||
|
let date: Date;
|
||
|
let processed = true;
|
||
|
if (duration > 0) {
|
||
|
date = new Date(now + duration);
|
||
|
processed = false;
|
||
|
} else date = null;
|
||
|
const expiration = { date, processed };
|
||
|
mod.expiration = expiration;
|
||
|
|
||
|
const embed = new RichEmbed();
|
||
|
embed.setTitle(`Case ${logID} | Ban`);
|
||
|
embed.setAuthor(member.user.username, member.user.avatarURL);
|
||
|
embed.setThumbnail(member.user.avatarURL);
|
||
|
embed.addField('User', `<@${member.id}>`, true);
|
||
|
embed.addField('Moderator', `<@${moderator.id}>`, true);
|
||
|
if (reason) {
|
||
|
embed.addField('Reason', reason, true);
|
||
|
}
|
||
|
if (date) {
|
||
|
embed.addField('Expiration', moment(date).calendar(), true);
|
||
|
}
|
||
|
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
|
||
|
embed.setTimestamp();
|
||
|
this.client.createMessage(this.logChannels.modlogs, { embed });
|
||
|
return mod.save();
|
||
|
}
|
||
|
|
||
|
public async unban(userID: string, moderator: Member, reason?: string): Promise<ModerationInterface> {
|
||
|
this.client.unbanGuildMember(this.client.config.guildID, userID, reason);
|
||
|
const user = await this.client.getRESTUser(userID);
|
||
|
if (!user) throw new Error('Cannot get user.');
|
||
|
const logID = uuid();
|
||
|
const mod = new ModerationModel({
|
||
|
userID,
|
||
|
logID,
|
||
|
moderatorID: moderator.id,
|
||
|
reason: reason ?? null,
|
||
|
type: 4,
|
||
|
date: new Date(),
|
||
|
});
|
||
|
|
||
|
const embed = new RichEmbed();
|
||
|
embed.setTitle(`Case ${logID} | Unban`);
|
||
|
embed.setAuthor(user.username, user.avatarURL);
|
||
|
embed.setThumbnail(user.avatarURL);
|
||
|
embed.addField('User', `<@${user.id}>`, true);
|
||
|
embed.addField('Moderator', `<@${moderator.id}>`, true);
|
||
|
if (reason) {
|
||
|
embed.addField('Reason', reason, true);
|
||
|
}
|
||
|
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
|
||
|
embed.setTimestamp();
|
||
|
this.client.createMessage(this.logChannels.modlogs, { embed });
|
||
|
return mod.save();
|
||
|
}
|
||
|
}
|