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 | |
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')
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 56 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuildMember.ts | 128 | ||||
-rw-r--r-- | src/lib/models/Ban.ts | 8 | ||||
-rw-r--r-- | src/lib/models/Global.ts | 33 | ||||
-rw-r--r-- | src/lib/models/Guild.ts | 23 | ||||
-rw-r--r-- | src/lib/models/Level.ts | 13 | ||||
-rw-r--r-- | src/lib/models/ModLog.ts | 21 | ||||
-rw-r--r-- | src/lib/models/Mute.ts | 8 | ||||
-rw-r--r-- | src/lib/models/PunishmentRole.ts | 18 |
9 files changed, 251 insertions, 57 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); } } diff --git a/src/lib/models/Ban.ts b/src/lib/models/Ban.ts index 54ca6ae..1bdda6f 100644 --- a/src/lib/models/Ban.ts +++ b/src/lib/models/Ban.ts @@ -5,15 +5,15 @@ import { BaseModel } from './BaseModel'; export interface BanModel { id: string; - user: string; - guild: string; + user: Snowflake; + guild: Snowflake; expires: Date; modlog: string; } export interface BanModelCreationAttributes { id?: string; - user: string; - guild: string; + user: Snowflake; + guild: Snowflake; expires?: Date; modlog: string; } diff --git a/src/lib/models/Global.ts b/src/lib/models/Global.ts index 842f14b..ba77302 100644 --- a/src/lib/models/Global.ts +++ b/src/lib/models/Global.ts @@ -1,5 +1,5 @@ import { Snowflake } from 'discord.js'; -import { DataTypes, Optional, Sequelize } from 'sequelize'; +import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; export interface GlobalModel { @@ -10,17 +10,40 @@ export interface GlobalModel { blacklistedGuilds: Snowflake[]; blacklistedChannels: Snowflake[]; } -export type GlobalModelCreationAttributes = Optional< - GlobalModel, - 'superUsers' | 'disabledCommands' | 'blacklistedUsers' | 'blacklistedGuilds' | 'blacklistedChannels' ->; + +export interface GlobalModelCreationAttributes { + environment: 'production' | 'development'; + superUsers?: Snowflake[]; + disabledCommands?: string[]; + blacklistedUsers?: Snowflake[]; + blacklistedGuilds?: Snowflake[]; + blacklistedChannels?: Snowflake[]; +} export class Global extends BaseModel<GlobalModel, GlobalModelCreationAttributes> implements GlobalModel { + /** + * The bot's environment. + */ environment: 'production' | 'development'; + /** + * Trusted users. + */ superUsers: Snowflake[]; + /** + * Globally disabled commands. + */ disabledCommands: string[]; + /** + * Globally blacklisted users. + */ blacklistedUsers: Snowflake[]; + /** + * Guilds blacklisted from using the bot. + */ blacklistedGuilds: Snowflake[]; + /** + * Channels where the bot is prevented from running. + */ blacklistedChannels: Snowflake[]; static initModel(sequelize: Sequelize): void { Global.init( diff --git a/src/lib/models/Guild.ts b/src/lib/models/Guild.ts index 0fc3413..f6aa1a4 100644 --- a/src/lib/models/Guild.ts +++ b/src/lib/models/Guild.ts @@ -1,5 +1,5 @@ import { Snowflake } from 'discord.js'; -import { DataTypes, Optional, Sequelize } from 'sequelize'; +import { DataTypes, Sequelize } from 'sequelize'; import { BushClient } from '../extensions/discord-akairo/BushClient'; import { BaseModel } from './BaseModel'; @@ -13,14 +13,23 @@ export interface GuildModel { punishmentEnding: string; } -export type GuildModelCreationAttributes = Optional< - GuildModel, - 'prefix' | 'autoPublishChannels' | 'blacklistedChannels' | 'welcomeChannel' | 'muteRole' | 'punishmentEnding' ->; +// export type GuildModelCreationAttributes = Optional< +// GuildModel, +// 'prefix' | 'autoPublishChannels' | 'blacklistedChannels' | 'welcomeChannel' | 'muteRole' | 'punishmentEnding' +// >; +export interface GuildModelCreationAttributes { + id: string; + prefix?: string; + autoPublishChannels?: Snowflake[]; + blacklistedChannels?: Snowflake[]; + welcomeChannel?: Snowflake; + muteRole?: Snowflake; + punishmentEnding?: string; +} export class Guild extends BaseModel<GuildModel, GuildModelCreationAttributes> implements GuildModel { - id: string; - prefix: string; + id!: string; + prefix!: string; autoPublishChannels: string[]; blacklistedChannels: Snowflake[]; welcomeChannel: Snowflake; diff --git a/src/lib/models/Level.ts b/src/lib/models/Level.ts index e1f30f4..b834992 100644 --- a/src/lib/models/Level.ts +++ b/src/lib/models/Level.ts @@ -1,18 +1,25 @@ +import { Snowflake } from 'discord.js'; import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; export interface LevelModel { - id: string; + id: Snowflake; xp: number; } export interface LevelModelCreationAttributes { - id: string; + id: Snowflake; xp?: number; } export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> { - public id: string; + /** + * The user's id. + */ + public id: Snowflake; + /** + * The user's xp. + */ public xp: number; get level(): number { return Level.convertXpToLevel(this.xp); diff --git a/src/lib/models/ModLog.ts b/src/lib/models/ModLog.ts index 6261794..40dc86d 100644 --- a/src/lib/models/ModLog.ts +++ b/src/lib/models/ModLog.ts @@ -38,12 +38,33 @@ export interface ModLogModelCreationAttributes { } export class ModLog extends BaseModel<ModLogModel, ModLogModelCreationAttributes> implements ModLogModel { + /** + * The primary key of the modlog entry. + */ id: string; + /** + * The type of punishment. + */ type: ModLogType; + /** + * The user being punished. + */ user: Snowflake; + /** + * The user carrying out the punishment. + */ moderator: Snowflake; + /** + * The reason the user is getting punished + */ reason: string | null; + /** + * The amount of time the user is getting punished for. + */ duration: number | null; + /** + * The guild the user is getting punished in. + */ guild: Snowflake; static initModel(sequelize: Sequelize): void { diff --git a/src/lib/models/Mute.ts b/src/lib/models/Mute.ts index 71a32e3..4208d02 100644 --- a/src/lib/models/Mute.ts +++ b/src/lib/models/Mute.ts @@ -5,15 +5,15 @@ import { BaseModel } from './BaseModel'; export interface MuteModel { id: string; - user: string; - guild: string; + user: Snowflake; + guild: Snowflake; expires: Date; modlog: string; } export interface MuteModelCreationAttributes { id?: string; - user: string; - guild: string; + user: Snowflake; + guild: Snowflake; expires?: Date; modlog: string; } diff --git a/src/lib/models/PunishmentRole.ts b/src/lib/models/PunishmentRole.ts index 927cf28..0b54f31 100644 --- a/src/lib/models/PunishmentRole.ts +++ b/src/lib/models/PunishmentRole.ts @@ -5,15 +5,17 @@ import { BaseModel } from './BaseModel'; export interface PunishmentRoleModel { id: string; - user: string; - guild: string; + user: Snowflake; + role: Snowflake; + guild: Snowflake; expires: Date; modlog: string; } export interface PunishmentRoleModelCreationAttributes { id?: string; - user: string; - guild: string; + user: Snowflake; + role?: Snowflake; + guild: Snowflake; expires?: Date; modlog: string; } @@ -31,6 +33,10 @@ export class PunishmentRole */ user: Snowflake; /** + * The role added to the user. + */ + role: Snowflake; + /** * The guild they received a role in */ guild: Snowflake; @@ -56,6 +62,10 @@ export class PunishmentRole type: DataTypes.STRING, allowNull: false }, + role: { + type: DataTypes.STRING, + allowNull: false + }, guild: { type: DataTypes.STRING, allowNull: false, |