소스 검색

Account for using not being present on the server

ghorsington 4 년 전
부모
커밋
758400e246
1개의 변경된 파일32개의 추가작업 그리고 37개의 파일을 삭제
  1. 32 37
      bot/src/plugins/violation.ts

+ 32 - 37
bot/src/plugins/violation.ts

@@ -25,10 +25,11 @@ interface ViolationInfo {
 
 type TimedViolation = Violation & { endsAt: Date };
 
-type StartViolationFunction = (member: GuildMember) => Promise<void>;
-type StopViolationFunction = (member: GuildMember, settings: GuildViolationSettings) => Promise<void>;
+type StartViolationFunction = (member: GuildMember, settings: GuildViolationSettings) => Promise<void>;
+type StopViolationFunction = (guild: Guild, userId: string, settings: GuildViolationSettings) => Promise<void>;
 interface TimedViolationStopHandler {
     type: ObjectType<TimedViolation>;
+    start: StartViolationFunction;
     stop: StopViolationFunction;
     command: string;
 }
@@ -40,8 +41,21 @@ export class ViolationPlugin {
         {
             command: "mute",
             type: Mute,
-            stop: async (member: GuildMember, settings: GuildViolationSettings): Promise<void> => {
+            start: async (member: GuildMember, settings: GuildViolationSettings): Promise<void> => {
                 const muteRoleResolve = await tryDo(member.guild.roles.fetch(settings.muteRoleId));
+                if (!muteRoleResolve.ok || !muteRoleResolve.result) {
+                    logger.error(
+                        "mute: Tried to mute user %s#%s (%s) but mute role ID %s is invalid!",
+                        member.user.username,
+                        member.user.discriminator,
+                        member.user.id,
+                        settings.muteRoleId);
+                    return;
+                }
+                await member.roles.add(muteRoleResolve.result);
+            },
+            stop: async (guild: Guild, userId: string, settings: GuildViolationSettings): Promise<void> => {
+                const muteRoleResolve = await tryDo(guild.roles.fetch(settings.muteRoleId));
 
                 if (!muteRoleResolve.ok || !muteRoleResolve.result) {
                     logger.warn("mute: couldn't find mute role id %s (removed from server?)", settings.muteRoleId);
@@ -49,7 +63,14 @@ export class ViolationPlugin {
                 }
                 const muteRole = muteRoleResolve.result;
 
-                await member.roles.remove(muteRole);
+                const memberResolve = await tryDo(guild.members.fetch(userId));
+
+                if (!memberResolve.ok) {
+                    logger.warn("mute: user %s is not on the server anymore", userId);
+                    return;
+                }
+
+                await memberResolve.result.roles.remove(muteRole);
             }
         }
     ];
@@ -82,35 +103,16 @@ export class ViolationPlugin {
         if (!info.ok)
             return;
 
-        const muteRoleId = info.settings.muteRoleId;
-        const muteRoleResolve = await tryDo(info.guild.roles.fetch(muteRoleId));
-
-        if (!muteRoleResolve.ok || !muteRoleResolve.result) {
-            await message.reply("the mute role ID is invalid! Ping the bot manager!");
-            logger.error(
-                "mute: Tried to mute user %s#%s (%s) but mute role ID %s is invalid!",
-                message.author.username,
-                message.author.discriminator,
-                message.author.id,
-                muteRoleId);
-            return;
-        }
-        const muteRole = muteRoleResolve.result;
-
-        const apply = async (member: GuildMember) => {
-            await member.roles.add(muteRole);
-        };
+        const handler = this.getViolationHandler(Mute);
 
-        const remove = this.getStopHandler(Mute);
-
-        await this.applyTimedViolation(Mute, info, "mute", apply, remove);
+        await this.applyTimedViolation(Mute, info, "mute", handler.start, handler.stop);
         await this.sendViolationMessage(message, info, "User has been muted for server violation");
     }
 
-    private getStopHandler(type: ObjectType<TimedViolation>): StopViolationFunction {
+    private getViolationHandler(type: ObjectType<TimedViolation>): TimedViolationStopHandler {
         for (const handler of this.stopHandlers) {
             if (handler.type == type)
-                return handler.stop;
+                return handler;
         }
         throw new Error("Cannot find stop handler for violation type!");
     }
@@ -143,7 +145,7 @@ export class ViolationPlugin {
             } as unknown as DeepPartial<T>);
             this.jobs[newViolation.id] = scheduleJob(info.endDate, this.scheduleRemoveViolation(type, info.guild.id, info.member.id, remove, command));
         }
-        await apply(info.member);
+        await apply(info.member, info.settings);
     }
 
     private scheduleRemoveViolation<T extends TimedViolation>(type: ObjectType<T>, guildId: string, userId: string, handle: StopViolationFunction, command = "violation") {
@@ -170,7 +172,7 @@ export class ViolationPlugin {
                 return;
             }
 
-            await repo.update({ id: violation.id } as unknown as FindConditions<T>, { valid: true } as unknown as QueryDeepPartialEntity<T>);
+            await repo.update({ id: violation.id } as unknown as FindConditions<T>, { valid: false } as unknown as QueryDeepPartialEntity<T>);
             delete this.jobs[violation.id];
 
             const guild = client.bot.guilds.resolve(guildId);
@@ -179,14 +181,7 @@ export class ViolationPlugin {
                 return;
             }
 
-            const userResolve = await tryDo(guild.members.fetch(userId));
-            if (!userResolve.ok || !userResolve.result) {
-                logger.warn("un-%s: couldn't find user %s (possibly left the server?)", command, userId);
-                return;
-            }
-            const user = userResolve.result;
-
-            await handle(user, settings);
+            await handle(guild, userId, settings);
         };
     }