2020-04-15 15:13:44 -04:00
|
|
|
/* eslint-disable no-bitwise */
|
2020-04-16 17:57:50 -04:00
|
|
|
import { Member, User } from 'eris';
|
2020-04-17 23:05:10 -04:00
|
|
|
import { randomBytes } from 'crypto';
|
2020-04-15 15:13:44 -04:00
|
|
|
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;
|
2020-04-15 15:33:27 -04:00
|
|
|
this.logChannels = {
|
|
|
|
modlogs: channels.modlogs,
|
|
|
|
};
|
2020-04-15 15:13:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:57:50 -04:00
|
|
|
public async ban(user: User, moderator: Member, duration: number, reason?: string): Promise<ModerationInterface> {
|
2020-04-16 22:56:52 -04:00
|
|
|
if (reason && reason.length > 512) throw new Error('Ban reason cannot be longer than 512 characters');
|
2020-04-16 17:57:50 -04:00
|
|
|
await this.client.guilds.get(this.client.config.guildID).banMember(user.id, 7, reason);
|
2020-04-17 23:05:10 -04:00
|
|
|
const logID = randomBytes(2).toString('hex');
|
2020-04-15 15:13:44 -04:00
|
|
|
const mod = new ModerationModel({
|
2020-04-16 17:57:50 -04:00
|
|
|
userID: user.id,
|
2020-04-15 15:13:44 -04:00
|
|
|
logID,
|
|
|
|
moderatorID: moderator.id,
|
2020-04-16 22:56:52 -04:00
|
|
|
reason: reason || null,
|
2020-04-15 15:13:44 -04:00
|
|
|
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`);
|
2020-04-16 17:57:50 -04:00
|
|
|
embed.setColor('#e74c3c');
|
|
|
|
embed.setAuthor(user.username, user.avatarURL);
|
|
|
|
embed.setThumbnail(user.avatarURL);
|
|
|
|
embed.addField('User', `<@${user.id}>`, true);
|
2020-04-15 15:13:44 -04:00
|
|
|
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.');
|
2020-04-17 23:05:10 -04:00
|
|
|
const logID = randomBytes(2).toString('hex');
|
2020-04-15 15:13:44 -04:00
|
|
|
const mod = new ModerationModel({
|
|
|
|
userID,
|
|
|
|
logID,
|
|
|
|
moderatorID: moderator.id,
|
2020-04-16 22:56:52 -04:00
|
|
|
reason: reason || null,
|
2020-04-15 15:13:44 -04:00
|
|
|
type: 4,
|
|
|
|
date: new Date(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const embed = new RichEmbed();
|
|
|
|
embed.setTitle(`Case ${logID} | Unban`);
|
2020-04-16 17:57:50 -04:00
|
|
|
embed.setColor('#1abc9c');
|
2020-04-15 15:13:44 -04:00
|
|
|
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();
|
|
|
|
}
|
2020-04-28 09:24:44 -04:00
|
|
|
|
2020-05-19 17:34:55 -04:00
|
|
|
public async kick(user: Member|User, moderator: Member, reason?: string): Promise<ModerationInterface> {
|
2020-04-28 09:24:44 -04:00
|
|
|
if (reason && reason.length > 512) throw new Error('Kick reason cannot be longer than 512 characters');
|
|
|
|
await this.client.guilds.get(this.client.config.guildID).kickMember(user.id, reason);
|
|
|
|
const logID = randomBytes(2).toString('hex');
|
|
|
|
const mod = new ModerationModel({
|
|
|
|
userID: user.id,
|
|
|
|
logID,
|
|
|
|
moderatorID: moderator.id,
|
|
|
|
reason: reason || null,
|
|
|
|
type: 5,
|
|
|
|
date: new Date(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const embed = new RichEmbed();
|
|
|
|
embed.setTitle(`Case ${logID} | Kick`);
|
|
|
|
embed.setColor('#e74c3c');
|
|
|
|
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();
|
|
|
|
}
|
2020-04-15 15:13:44 -04:00
|
|
|
}
|