aboutsummaryrefslogtreecommitdiff
path: root/src/lib/extensions/discord-akairo
diff options
context:
space:
mode:
authorIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-07-23 22:02:44 -0400
committerIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-07-23 22:02:44 -0400
commitb015bec7f66526ec5e959ae99865845f4db4b181 (patch)
tree67538c9549b7e0f7cd6a97e9c82db8d8462a19c7 /src/lib/extensions/discord-akairo
parent5c242f597595b8db71875d92c0afe0a5947442a6 (diff)
downloadtanzanite-b015bec7f66526ec5e959ae99865845f4db4b181.tar.gz
tanzanite-b015bec7f66526ec5e959ae99865845f4db4b181.tar.bz2
tanzanite-b015bec7f66526ec5e959ae99865845f4db4b181.zip
feat: some shit
- fix breaking changes - refactored active punishments into one table - made listeners args have stricter types
Diffstat (limited to 'src/lib/extensions/discord-akairo')
-rw-r--r--src/lib/extensions/discord-akairo/BushClient.ts56
-rw-r--r--src/lib/extensions/discord-akairo/BushClientUtil.ts88
-rw-r--r--src/lib/extensions/discord-akairo/BushCommandHandler.ts41
-rw-r--r--src/lib/extensions/discord-akairo/BushListener.ts5
4 files changed, 76 insertions, 114 deletions
diff --git a/src/lib/extensions/discord-akairo/BushClient.ts b/src/lib/extensions/discord-akairo/BushClient.ts
index 66204ac..8eec3ce 100644
--- a/src/lib/extensions/discord-akairo/BushClient.ts
+++ b/src/lib/extensions/discord-akairo/BushClient.ts
@@ -18,13 +18,11 @@ import { Sequelize } from 'sequelize';
import { contentWithDurationTypeCaster } from '../../../arguments/contentWithDuration';
import { durationTypeCaster } from '../../../arguments/duration';
import { UpdateCacheTask } from '../../../tasks/updateCache';
-import { Ban } from '../../models/Ban';
+import { ActivePunishment } from '../../models/ActivePunishment';
import { Global } from '../../models/Global';
import { Guild as GuildModel } from '../../models/Guild';
import { Level } from '../../models/Level';
import { ModLog } from '../../models/ModLog';
-import { Mute } from '../../models/Mute';
-import { PunishmentRole } from '../../models/PunishmentRole';
import { StickyRole } from '../../models/StickyRole';
import { AllowedMentions } from '../../utils/AllowedMentions';
import { BushCache } from '../../utils/BushCache';
@@ -107,39 +105,23 @@ export class BushClient extends AkairoClient {
public constants = BushConstants;
public cache = BushCache;
public constructor(config: Config) {
- super(
- {
- ownerID: config.owners,
- intents: Object.values(Intents.FLAGS).reduce((acc, p) => acc | p, 0),
- presence: {
- activities: [
- {
- name: 'Beep Boop',
- type: 'WATCHING'
- }
- ],
- status: 'online'
- }
+ super({
+ ownerID: config.owners,
+ intents: Object.values(Intents.FLAGS).reduce((acc, p) => acc | p, 0),
+ presence: {
+ activities: [
+ {
+ name: 'Beep Boop',
+ type: 'WATCHING'
+ }
+ ],
+ status: 'online'
},
- {
- allowedMentions: AllowedMentions.users(), // No everyone or role mentions by default
- intents: Object.values(Intents.FLAGS).reduce((acc, p) => acc | p, 0),
- presence: {
- activities: [
- {
- name: 'Beep Boop',
- type: 'WATCHING'
- }
- ],
- status: 'online'
- }
- }
- );
+ http: { api: 'https://canary.discord.com/api' },
+ allowedMentions: AllowedMentions.users() // No everyone or role mentions by default
+ });
- // Set token
this.token = config.token;
-
- // Set config
this.config = config;
// Create listener handler
@@ -170,7 +152,7 @@ export class BushClient extends AkairoClient {
allowMention: true,
handleEdits: true,
commandUtil: true,
- commandUtilLifetime: 300_000,
+ commandUtilLifetime: 300_000, // 5 minutes
argumentDefaults: {
prompt: {
start: 'Placeholder argument prompt. If you see this please tell my developers.',
@@ -259,11 +241,9 @@ export class BushClient extends AkairoClient {
Global.initModel(this.db);
GuildModel.initModel(this.db, this);
ModLog.initModel(this.db);
- Ban.initModel(this.db);
- Mute.initModel(this.db);
+ ActivePunishment.initModel(this.db);
Level.initModel(this.db);
StickyRole.initModel(this.db);
- PunishmentRole.initModel(this.db);
await this.db.sync({ alter: true }); // Sync all tables to fix everything if updated
await this.console.success('Startup', `Successfully connected to <<database>>.`, false);
} catch (e) {
@@ -277,7 +257,7 @@ export class BushClient extends AkairoClient {
/** Starts the bot */
public async start(): Promise<void> {
- global.client = this;
+ global.client = this; // makes the client a global object
try {
await this._init();
diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts
index 0e3a904..306e049 100644
--- a/src/lib/extensions/discord-akairo/BushClientUtil.ts
+++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts
@@ -2,7 +2,6 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import {
- Ban,
BushCache,
BushClient,
BushConstants,
@@ -14,9 +13,7 @@ import {
Global,
Guild,
ModLog,
- ModLogType,
- Mute,
- PunishmentRole
+ ModLogType
} from '@lib';
import { exec } from 'child_process';
import { ClientUtil } from 'discord-akairo';
@@ -43,8 +40,8 @@ import {
} from 'discord.js';
import got from 'got';
import humanizeDuration from 'humanize-duration';
-import { Op } from 'sequelize';
import { promisify } from 'util';
+import { ActivePunishment, ActivePunishmentType } from '../../models/ActivePunishment';
import { BushNewsChannel } from '../discord.js/BushNewsChannel';
import { BushTextChannel } from '../discord.js/BushTextChannel';
import { BushUserResolvable } from './BushClient';
@@ -96,12 +93,6 @@ interface bushColors {
orange: '#E86100';
}
-interface punishmentModels {
- mute: Mute;
- ban: Ban;
- role: PunishmentRole;
-}
-
interface MojangProfile {
username: string;
uuid: string;
@@ -664,22 +655,21 @@ export class BushClientUtil extends ClientUtil {
}
public async createPunishmentEntry(options: {
- type: 'mute' | 'ban' | 'role';
+ type: 'mute' | 'ban' | 'role' | 'block';
user: BushGuildMemberResolvable;
duration: number;
guild: BushGuildResolvable;
modlog: string;
- role?: Snowflake;
- }): Promise<Mute | Ban | PunishmentRole> {
- const dbModel = this.findPunishmentModel(options.type);
+ extraInfo?: Snowflake;
+ }): Promise<ActivePunishment> {
const expires = options.duration ? new Date(new Date().getTime() + options.duration) : null;
const user = this.client.users.resolveId(options.user);
const guild = this.client.guilds.resolveId(options.guild);
+ const type = this.findTypeEnum(options.type);
- const entry =
- options.type === 'role'
- ? (dbModel as typeof PunishmentRole).build({ user, guild, expires, modlog: options.modlog, role: options.role })
- : dbModel.build({ user, guild, expires, modlog: options.modlog });
+ const entry = options.extraInfo
+ ? ActivePunishment.build({ user, type, guild, expires, modlog: options.modlog, extraInfo: options.extraInfo })
+ : ActivePunishment.build({ user, type, guild, expires, modlog: options.modlog });
return await entry.save().catch((e) => {
this.client.console.error('createPunishmentEntry', e?.stack || e);
return null;
@@ -687,28 +677,23 @@ export class BushClientUtil extends ClientUtil {
}
public async removePunishmentEntry(options: {
- type: 'mute' | 'ban' | 'role';
+ type: 'mute' | 'ban' | 'role' | 'block';
user: BushGuildMemberResolvable;
guild: BushGuildResolvable;
}): Promise<boolean> {
- const dbModel = this.findPunishmentModel(options.type);
const user = this.client.users.resolveId(options.user);
const guild = this.client.guilds.resolveId(options.guild);
+ const type = this.findTypeEnum(options.type);
let success = true;
- const entries = await dbModel
- .findAll({
- // finding all cases of a certain type incase there were duplicates or something
- where: {
- user,
- guild
- }
- })
- .catch((e) => {
- this.client.console.error('removePunishmentEntry', e?.stack || e);
- success = false;
- });
+ const entries = await ActivePunishment.findAll({
+ // finding all cases of a certain type incase there were duplicates or something
+ where: { user, guild, type }
+ }).catch((e) => {
+ this.client.console.error('removePunishmentEntry', e?.stack || e);
+ success = false;
+ });
if (entries) {
entries.forEach(async (entry) => {
await entry.destroy().catch((e) => {
@@ -720,33 +705,14 @@ export class BushClientUtil extends ClientUtil {
return success;
}
- 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;
- case 'ban':
- return Ban;
- case 'role':
- return PunishmentRole;
- default:
- throw 'choose a valid punishment entry type';
- }
+ private findTypeEnum(type: 'mute' | 'ban' | 'role' | 'block') {
+ const typeMap = {
+ ['mute']: ActivePunishmentType.MUTE,
+ ['ban']: ActivePunishmentType.BAN,
+ ['role']: ActivePunishmentType.ROLE,
+ ['block']: ActivePunishmentType.BLOCK
+ };
+ return typeMap[type];
}
public humanizeDuration(duration: number): string {
@@ -782,5 +748,7 @@ export class BushClientUtil extends ClientUtil {
return arrByte[1] + ', ' + arrByte[2] + ', ' + arrByte[3];
}
+ /* eslint-disable @typescript-eslint/no-unused-vars */
public async lockdownChannel(options: { channel: BushTextChannel | BushNewsChannel; moderator: BushUserResolvable }) {}
+ /* eslint-enable @typescript-eslint/no-unused-vars */
}
diff --git a/src/lib/extensions/discord-akairo/BushCommandHandler.ts b/src/lib/extensions/discord-akairo/BushCommandHandler.ts
index 09baf2e..dacd17f 100644
--- a/src/lib/extensions/discord-akairo/BushCommandHandler.ts
+++ b/src/lib/extensions/discord-akairo/BushCommandHandler.ts
@@ -1,15 +1,26 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
-import { Category, CommandHandler, CommandHandlerOptions } from 'discord-akairo';
-import { Collection } from 'discord.js';
+import { Category, CommandHandler, CommandHandlerEvents, CommandHandlerOptions } from 'discord-akairo';
+import { Collection, PermissionString } from 'discord.js';
import { BushConstants } from '../../utils/BushConstants';
import { BushMessage } from '../discord.js/BushMessage';
import { BushClient } from './BushClient';
import { BushCommand } from './BushCommand';
+import { BushSlashMessage } from './BushSlashMessage';
export type BushCommandHandlerOptions = CommandHandlerOptions;
-const CommandHandlerEvents = BushConstants.CommandHandlerEvents;
-const BlockedReasons = BushConstants.BlockedReasons;
+const commandHandlerEvents = BushConstants.CommandHandlerEvents;
+const blockedReasons = BushConstants.BlockedReasons;
+
+export interface BushCommandHandlerEvents extends CommandHandlerEvents {
+ commandBlocked: [message: BushMessage, command: BushCommand, reason: string];
+
+ missingPermissions: [message: BushMessage, command: BushCommand, type: 'client' | 'user', missing: Array<PermissionString>];
+
+ slashBlocked: [message: BushSlashMessage, command: BushCommand, reason: string];
+
+ slashMissingPermissions: [message: BushSlashMessage, command: BushCommand, type: 'client' | 'user', missing: Array<PermissionString>];
+}
export class BushCommandHandler extends CommandHandler {
public declare client: BushClient;
@@ -24,10 +35,10 @@ export class BushCommandHandler extends CommandHandler {
const isOwner = this.client.isOwner(message.author);
if (!isOwner) {
this.emit(
- slash ? CommandHandlerEvents.SLASH_BLOCKED : CommandHandlerEvents.COMMAND_BLOCKED,
+ slash ? commandHandlerEvents.SLASH_BLOCKED : commandHandlerEvents.COMMAND_BLOCKED,
message,
command,
- BlockedReasons.OWNER
+ blockedReasons.OWNER
);
return true;
}
@@ -37,10 +48,10 @@ export class BushCommandHandler extends CommandHandler {
const isSuperUser = this.client.isSuperUser(message.author);
if (!isSuperUser) {
this.emit(
- slash ? CommandHandlerEvents.SLASH_BLOCKED : CommandHandlerEvents.COMMAND_BLOCKED,
+ slash ? commandHandlerEvents.SLASH_BLOCKED : commandHandlerEvents.COMMAND_BLOCKED,
message,
command,
- BlockedReasons.OWNER
+ blockedReasons.OWNER
);
return true;
}
@@ -48,32 +59,32 @@ export class BushCommandHandler extends CommandHandler {
if (command.channel === 'guild' && !message.guild) {
this.emit(
- slash ? CommandHandlerEvents.SLASH_BLOCKED : CommandHandlerEvents.COMMAND_BLOCKED,
+ slash ? commandHandlerEvents.SLASH_BLOCKED : commandHandlerEvents.COMMAND_BLOCKED,
message,
command,
- BlockedReasons.GUILD
+ blockedReasons.GUILD
);
return true;
}
if (command.channel === 'dm' && message.guild) {
this.emit(
- slash ? CommandHandlerEvents.SLASH_BLOCKED : CommandHandlerEvents.COMMAND_BLOCKED,
+ slash ? commandHandlerEvents.SLASH_BLOCKED : commandHandlerEvents.COMMAND_BLOCKED,
message,
command,
- BlockedReasons.DM
+ blockedReasons.DM
);
return true;
}
if (command.restrictedChannels?.length && message.channel) {
if (!command.restrictedChannels.includes(message.channel.id)) {
- this.emit(CommandHandlerEvents.COMMAND_BLOCKED, message, command, BlockedReasons.RESTRICTED_CHANNEL);
+ this.emit(commandHandlerEvents.COMMAND_BLOCKED, message, command, blockedReasons.RESTRICTED_CHANNEL);
return true;
}
}
if (command.restrictedGuilds?.length && message.guild) {
if (!command.restrictedGuilds.includes(message.guild.id)) {
- this.emit(CommandHandlerEvents.COMMAND_BLOCKED, message, command, BlockedReasons.RESTRICTED_GUILD);
+ this.emit(commandHandlerEvents.COMMAND_BLOCKED, message, command, blockedReasons.RESTRICTED_GUILD);
return true;
}
}
@@ -82,7 +93,7 @@ export class BushCommandHandler extends CommandHandler {
}
const reason = this.inhibitorHandler ? await this.inhibitorHandler.test('post', message, command) : null;
if (reason != null) {
- this.emit(CommandHandlerEvents.COMMAND_BLOCKED, message, command, reason);
+ this.emit(commandHandlerEvents.COMMAND_BLOCKED, message, command, reason);
return true;
}
return !!this.runCooldowns(message, command);
diff --git a/src/lib/extensions/discord-akairo/BushListener.ts b/src/lib/extensions/discord-akairo/BushListener.ts
index e555e89..2583e85 100644
--- a/src/lib/extensions/discord-akairo/BushListener.ts
+++ b/src/lib/extensions/discord-akairo/BushListener.ts
@@ -1,6 +1,9 @@
import { Listener } from 'discord-akairo';
+import EventEmitter from 'events';
import { BushClient } from './BushClient';
-
export class BushListener extends Listener {
public declare client: BushClient;
+ public constructor(id: string, options?:{emitter: string|EventEmitter, event: string, type?: 'on'|'once', category?: string}){
+ super(id, options)
+ }
}