Browse Source

Simplify reapplying; clean up violations

ghorsington 4 years ago
parent
commit
5188e66fd2
1 changed files with 26 additions and 23 deletions
  1. 26 23
      bot/src/plugins/violation.ts

+ 26 - 23
bot/src/plugins/violation.ts

@@ -34,14 +34,6 @@ interface TimedViolationStopHandler {
     command: string;
 }
 
-function isTimed(violation: Violation): violation is TimedViolation {
-    return Object.prototype.hasOwnProperty.call(violation, "endsAt") && (violation as unknown as Record<string, unknown>)["endsAt"] !== undefined;
-}
-
-function getViolationType(violation: TimedViolation): ObjectType<TimedViolation> {
-    return (violation as { constructor: ObjectType<TimedViolation> }).constructor;
-}
-
 @Plugin
 export class ViolationPlugin {
     jobs: Record<number, Job> = {};
@@ -109,8 +101,7 @@ export class ViolationPlugin {
         if (!settings)
             return;
 
-        const repo = getRepository(Violation);
-        const activeViolations = await repo.find({
+        const hasActiveViolations = await getRepository(Violation).findOne({
             where: {
                 guildId: member.guild.id,
                 userId: member.id,
@@ -118,21 +109,28 @@ export class ViolationPlugin {
             }
         });
 
-        if (activeViolations.length == 0)
+        if (!hasActiveViolations)
             return;
 
-        for (const violation of activeViolations) {
-            if (!isTimed(violation)) {
-                await repo.update({ id: violation.id }, { valid: false });
+        for (const handler of this.stopHandlers) {
+            const repo = getRepository(handler.type);
+            const activeViolations = await repo.find({
+                where: {
+                    guildId: member.guild.id,
+                    userId: member.id,
+                    valid: true
+                }
+            });
+
+            if (activeViolations.length == 0)
                 continue;
-            }
 
-            const type = getViolationType(violation);
-            const handler = this.getViolationHandler(type);
-            if (violation.endsAt < new Date())
-                await repo.update({ id: violation.id }, { valid: false });
-            else
-                await handler.start(member, settings);
+            for (const violation of activeViolations) {
+                if (violation.endsAt < new Date())
+                    await repo.update({ id: violation.id }, { valid: false });
+                else
+                    await handler.start(member, settings);
+            }
         }
     }
 
@@ -178,7 +176,7 @@ export class ViolationPlugin {
         });
 
         if (existingViolation) {
-            logger.warn("%s: trying to reapply on user %s#%s (%s)", command, info.member.user.id, info.member.user.discriminator, info.member.id);
+            logger.warn("%s: trying to reapply on user %s#%s (%s)", command, info.member.user.username, info.member.user.discriminator, info.member.id);
             await violationRepo.update({ id: existingViolation.id } as unknown as FindConditions<T>, { endsAt: info.endDate } as unknown as QueryDeepPartialEntity<T>);
             const job = this.jobs[existingViolation.id];
             rescheduleJob(job, info.endDate);
@@ -276,6 +274,11 @@ export class ViolationPlugin {
             return { ok: false };
         }
 
+        if (userId == message.author.id) {
+            await message.reply(`cannot ${command} yourself!`);
+            return { ok: false };
+        }
+
         const member = await this.resolveUser(message.guild, userId);
 
         if (!member) {
@@ -325,7 +328,7 @@ export class ViolationPlugin {
             }
         }
         await announceChannel?.send(new MessageEmbed({
-            title: title,
+            title: `${info.dryRun ? "[DRY RUN] " : ""}${title}`,
             color: 4944347,
             timestamp: new Date(),
             footer: {