diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-07-14 13:12:32 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-07-14 13:12:32 -0400 |
commit | 070b5f326d0647e7b105f99811e4bdc915c8652e (patch) | |
tree | 15c5e3366acb9f6056de83924db6dd9db961061a /src/lib/extensions | |
parent | cdb8b0297f806cb3147b3759b0fd234bffbcc3f9 (diff) | |
download | tanzanite-070b5f326d0647e7b105f99811e4bdc915c8652e.tar.gz tanzanite-070b5f326d0647e7b105f99811e4bdc915c8652e.tar.bz2 tanzanite-070b5f326d0647e7b105f99811e4bdc915c8652e.zip |
revamped role command and some other stuff
Diffstat (limited to 'src/lib/extensions')
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 56 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuildMember.ts | 128 |
2 files changed, 154 insertions, 30 deletions
diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index a981d30..b3c9953 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -1,6 +1,23 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +import { + Ban, + BushCache, + BushClient, + BushConstants, + BushGuildMember, + BushGuildMemberResolvable, + BushGuildResolvable, + BushMessage, + BushSlashMessage, + Global, + Guild, + ModLog, + ModLogType, + Mute, + PunishmentRole +} from '@lib'; import { exec } from 'child_process'; import { ClientUtil } from 'discord-akairo'; import { APIMessage } from 'discord-api-types'; @@ -26,20 +43,8 @@ import { } from 'discord.js'; import got from 'got'; import humanizeDuration from 'humanize-duration'; +import Op from 'sequelize/types/lib/operators'; import { promisify } from 'util'; -import { Ban } from '../../models/Ban'; -import { Global } from '../../models/Global'; -import { Guild } from '../../models/Guild'; -import { ModLog, ModLogType } from '../../models/ModLog'; -import { Mute } from '../../models/Mute'; -import { PunishmentRole } from '../../models/PunishmentRole'; -import { BushCache } from '../../utils/BushCache'; -import { BushConstants } from '../../utils/BushConstants'; -import { BushGuildResolvable } from '../discord.js/BushCommandInteraction'; -import { BushGuildMember } from '../discord.js/BushGuildMember'; -import { BushMessage } from '../discord.js/BushMessage'; -import { BushClient, BushGuildMemberResolvable } from './BushClient'; -import { BushSlashMessage } from './BushSlashMessage'; interface hastebinRes { key: string; @@ -87,6 +92,13 @@ interface bushColors { black: '#000000'; orange: '#E86100'; } + +interface punishmentModels { + mute: Mute; + ban: Ban; + role: PunishmentRole; +} + export class BushClientUtil extends ClientUtil { /** The client of this ClientUtil */ public declare readonly client: BushClient; @@ -689,7 +701,23 @@ export class BushClientUtil extends ClientUtil { return success; } - private findPunishmentModel(type: 'mute' | 'ban' | 'role'): typeof Mute | typeof Ban | typeof PunishmentRole { + public async findExpiredEntries<K extends keyof punishmentModels>(type: K): Promise<punishmentModels[K][]> { + const dbModel = this.findPunishmentModel(type); + //@ts-ignore: stfu idc + return await dbModel.findAll({ + where: { + [Op.and]: [ + { + expires: { + [Op.lt]: new Date() // Find all rows with an expiry date before now + } + } + ] + } + }); + } + + private findPunishmentModel<K extends keyof punishmentModels>(type: K): typeof Mute | typeof Ban | typeof PunishmentRole { switch (type) { case 'mute': return Mute; diff --git a/src/lib/extensions/discord.js/BushGuildMember.ts b/src/lib/extensions/discord.js/BushGuildMember.ts index adcae69..8e1c51a 100644 --- a/src/lib/extensions/discord.js/BushGuildMember.ts +++ b/src/lib/extensions/discord.js/BushGuildMember.ts @@ -1,8 +1,9 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ -import { GuildMember, RoleResolvable } from 'discord.js'; +import { GuildMember, Role } from 'discord.js'; import { ModLogType } from '../../models/ModLog'; import { BushClient, BushUserResolvable } from '../discord-akairo/BushClient'; import { BushGuild } from './BushGuild'; +import { BushRole } from './BushRole'; import { BushUser } from './BushUser'; interface BushPunishmentOptions { @@ -14,17 +15,39 @@ interface BushTimedPunishmentOptions extends BushPunishmentOptions { duration?: number; } -interface BushPunishmentRoleOptions extends BushTimedPunishmentOptions { - role: RoleResolvable; +interface AddRoleOptions { + moderator?: BushUserResolvable; + duration?: number; + role: BushRole | Role; + addToModlog: boolean; +} + +interface RemoveRoleOptions { + moderator?: BushUserResolvable; + duration?: number; + role: BushRole | Role; + addToModlog: boolean; } type PunishmentResponse = 'success' | 'error creating modlog entry' | 'failed to dm'; type WarnResponse = PunishmentResponse; -type PunishmentRoleResponse = PunishmentResponse; +type AddRoleResponse = + | PunishmentResponse + | 'user hierarchy' + | 'role managed' + | 'client hierarchy' + | 'error creating role entry' + | 'error adding role'; -type RemovePunishmentRoleResponse = PunishmentResponse; +type RemoveRoleResponse = + | PunishmentResponse + | 'user hierarchy' + | 'role managed' + | 'client hierarchy' + | 'error removing role entry' + | 'error removing role'; type MuteResponse = | PunishmentResponse @@ -67,7 +90,7 @@ export class BushGuildMember extends GuildMember { { type: ModLogType.WARN, user: this, - moderator: options.moderator, + moderator: options.moderator || this.client.user.id, reason: options.reason, guild: this.guild }, @@ -89,12 +112,85 @@ export class BushGuildMember extends GuildMember { return { result: 'success', caseNum }; } - public punishRole(options: BushPunishmentRoleOptions): Promise<PunishmentRoleResponse> { - throw 'not implemented'; + public async addRole(options: AddRoleOptions): Promise<AddRoleResponse> { + const ifShouldAddRole = this.checkIfShouldAddRole(options.role); + if (ifShouldAddRole !== true) return ifShouldAddRole; + + const moderator = this.client.users.cache.get(this.client.users.resolveId(options.moderator || this.client.user)); + + if (options.addToModlog) { + const { log: modlog } = await this.client.util + .createModLogEntry({ + type: options.duration ? ModLogType.TEMP_PUNISHMENT_ROLE : ModLogType.PERM_PUNISHMENT_ROLE, + guild: this.guild, + moderator: moderator.id, + user: this, + reason: 'N/A' + }) + .catch(() => null); + if (!modlog) return 'error creating modlog entry'; + + const punishmentEntrySuccess = await this.client.util + .createPunishmentEntry({ + type: 'role', + user: this, + guild: this.guild, + duration: options.duration, + modlog: modlog.id + }) + .catch(() => null); + if (!punishmentEntrySuccess) return 'error creating role entry'; + } + + const removeRoleSuccess = await this.roles.remove(options.role, `${moderator.tag}`); + if (!removeRoleSuccess) return 'error adding role'; + + return 'success'; + } + + public async removeRole(options: RemoveRoleOptions): Promise<RemoveRoleResponse> { + const ifShouldAddRole = this.checkIfShouldAddRole(options.role); + if (ifShouldAddRole !== true) return ifShouldAddRole; + + const moderator = this.client.users.cache.get(this.client.users.resolveId(options.moderator || this.client.user)); + + if (options.addToModlog) { + const { log: modlog } = await this.client.util + .createModLogEntry({ + type: ModLogType.PERM_PUNISHMENT_ROLE, + guild: this.guild, + moderator: moderator.id, + user: this, + reason: 'N/A' + }) + .catch(() => null); + if (!modlog) return 'error creating modlog entry'; + + const punishmentEntrySuccess = await this.client.util + .removePunishmentEntry({ + type: 'role', + user: this, + guild: this.guild + }) + .catch(() => null); + if (!punishmentEntrySuccess) return 'error removing role entry'; + } + + const removeRoleSuccess = await this.roles.remove(options.role, `${moderator.tag}`); + if (!removeRoleSuccess) return 'error removing role'; + + return 'success'; } - public removePunishRole(options: BushPunishmentRoleOptions): Promise<RemovePunishmentRoleResponse> { - throw 'not implemented'; + private checkIfShouldAddRole(role: BushRole | Role) { + if (this.roles.highest.position <= role.position) { + return `user hierarchy`; + } else if (role.managed) { + return `role managed`; + } else if (this.guild.me.roles.highest.position <= role.position) { + return `client hierarchy`; + } + return true; } public async mute(options: BushTimedPunishmentOptions): Promise<MuteResponse> { @@ -115,7 +211,7 @@ export class BushGuildMember extends GuildMember { if (!muteSuccess) return 'error giving mute role'; // add modlog entry - const modlog = await this.client.util + const { log: modlog } = await this.client.util .createModLogEntry({ type: options.duration ? ModLogType.TEMP_MUTE : ModLogType.PERM_MUTE, user: this, @@ -170,7 +266,7 @@ export class BushGuildMember extends GuildMember { if (!muteSuccess) return 'error removing mute role'; //remove modlog entry - const modlog = await this.client.util + const { log: modlog } = await this.client.util .createModLogEntry({ type: ModLogType.UNMUTE, user: this, @@ -220,7 +316,7 @@ export class BushGuildMember extends GuildMember { if (!kickSuccess) return 'error kicking'; // add modlog entry - const modlog = await this.client.util + const { log: modlog } = await this.client.util .createModLogEntry({ type: ModLogType.KICK, user: this, @@ -256,7 +352,7 @@ export class BushGuildMember extends GuildMember { if (!banSuccess) return 'error banning'; // add modlog entry - const modlog = await this.client.util + const { log: modlog } = await this.client.util .createModLogEntry({ type: options.duration ? ModLogType.TEMP_BAN : ModLogType.PERM_BAN, user: this, @@ -284,11 +380,11 @@ export class BushGuildMember extends GuildMember { return 'success'; } - public isOwner(): boolean { + public get isOwner(): boolean { return this.client.isOwner(this); } - public isSuperUser(): boolean { + public get isSuperUser(): boolean { return this.client.isSuperUser(this); } } |