Skip to content

Commit

Permalink
fix(blacklist): fix errors when user is removed from blacklist before…
Browse files Browse the repository at this point in the history
… timer ends
  • Loading branch information
dev-737 committed Apr 30, 2023
1 parent d435436 commit 6f9b698
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 45 deletions.
32 changes: 16 additions & 16 deletions src/Scripts/networkLogs/modActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export async function modActions(moderator: User, action: blacklistUser | unblac
.setTitle('User Blacklisted')
.setDescription(
stripIndents`
${emoji.normal.dotYellow} **User:** ${action.user.tag} (${action.user.id})
${emoji.normal.dotYellow} **Moderator:** ${moderator.tag} (${moderator.id})
${emoji.normal.dotBlue} **User:** ${action.user.tag} (${action.user.id})
${emoji.normal.dotBlue} **Moderator:** ${moderator.tag} (${moderator.id})
`,
)
.addFields(
Expand All @@ -92,8 +92,8 @@ export async function modActions(moderator: User, action: blacklistUser | unblac
.setAuthor({ name: String(guild?.name), iconURL: guild?.iconURL() || undefined })
.setTitle('Server Blacklisted')
.setDescription(stripIndents`
${emoji.normal.dotYellow} **Server:** ${guild?.name} (${guild?.id})
${emoji.normal.dotYellow} **Moderator:** ${moderator.tag} (${moderator.id})
${emoji.normal.dotBlue} **Server:** ${guild?.name} (${guild?.id})
${emoji.normal.dotBlue} **Moderator:** ${moderator.tag} (${moderator.id})
`)
.addFields(
{ name: 'Reason', value: action.reason, inline: true },
Expand All @@ -112,8 +112,8 @@ export async function modActions(moderator: User, action: blacklistUser | unblac
.setTitle('User Unblacklisted')
.setColor(colors('chatbot'))
.setDescription(stripIndents`
${emoji.normal.dotYellow} **User:** ${action.user.tag} (${action.user.id})
${emoji.normal.dotYellow} **Moderator:** ${moderator.tag} (${moderator.id})
${emoji.normal.dotBlue} **User:** ${action.user.tag} (${action.user.id})
${emoji.normal.dotBlue} **Moderator:** ${moderator.tag} (${moderator.id})
`)
.addFields(
{ name: 'Blacklisted For', value: action.blacklistReason || 'Unknown', inline: true },
Expand All @@ -131,9 +131,9 @@ export async function modActions(moderator: User, action: blacklistUser | unblac
.setAuthor({ name: `${server?.name || action.dbGuild.serverName}`, iconURL: server?.iconURL()?.toString() })
.setTitle('Server Unblacklisted')
.setDescription(stripIndents`
${emoji.normal.dotYellow} **Server:** ${server?.name || action.dbGuild.serverName} (${action.dbGuild.serverId})
${emoji.normal.dotYellow} **Moderator:** ${moderator.tag} (${moderator.id})
${emoji.normal.dotYellow} **Timestamp:** <t:${Math.round(action.timestamp.getTime() / 1000)}:R>`)
${emoji.normal.dotBlue} **Server:** ${server?.name || action.dbGuild.serverName} (${action.dbGuild.serverId})
${emoji.normal.dotBlue} **Moderator:** ${moderator.tag} (${moderator.id})
${emoji.normal.dotBlue} **Timestamp:** <t:${Math.round(action.timestamp.getTime() / 1000)}:R>`)
.addFields({ name: 'Reason', value: action.reason })
.setColor(colors('chatbot')),
],
Expand All @@ -148,9 +148,9 @@ export async function modActions(moderator: User, action: blacklistUser | unblac
.setAuthor({ name: String(guild?.name), iconURL: guild?.iconURL() || undefined })
.setTitle('Left Server')
.setDescription(stripIndents`
${emoji.normal.dotYellow} **Server:** ${guild?.name} (${guild?.id})
${emoji.normal.dotYellow} **Moderator:** ${moderator.tag} (${moderator.id})
${emoji.normal.dotYellow} **Reason:** ${action.reason}
${emoji.normal.dotBlue} **Server:** ${guild?.name} (${guild?.id})
${emoji.normal.dotBlue} **Moderator:** ${moderator.tag} (${moderator.id})
${emoji.normal.dotBlue} **Reason:** ${action.reason}
`)
.setColor(colors('chatbot')),
],
Expand All @@ -164,9 +164,9 @@ export async function modActions(moderator: User, action: blacklistUser | unblac
.setAuthor({ name: String(guild?.name), iconURL: guild?.iconURL() || undefined })
.setTitle('Disconnected from Server')
.setDescription(stripIndents`
${emoji.normal.dotYellow} **Server:** ${guild?.name} (${guild?.id})
${emoji.normal.dotYellow} **Moderator:** ${moderator.tag} (${moderator.id})
${emoji.normal.dotYellow} **Reason:** ${action.reason}
${emoji.normal.dotBlue} **Server:** ${guild?.name} (${guild?.id})
${emoji.normal.dotBlue} **Moderator:** ${moderator.tag} (${moderator.id})
${emoji.normal.dotBlue} **Reason:** ${action.reason}
`)
.setColor(colors('chatbot')),
],
Expand All @@ -177,4 +177,4 @@ export async function modActions(moderator: User, action: blacklistUser | unblac
default:
break;
}
}
}
67 changes: 38 additions & 29 deletions src/Utils/functions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,6 @@ export async function addUserBlacklist(moderator: discord.User, user: discord.Us
},
});

// set an unblacklist timer if there is an expire duration
if (expires) {
scheduleJob(`blacklist_user-${user.id}`, expires, async () => {
const tempUser = user as discord.User;
await prisma.blacklistedUsers.delete({ where: { userId: tempUser.id } });

modActions(tempUser.client.user, {
user: tempUser,
action: 'unblacklistUser',
blacklistReason: dbUser.reason,
reason: 'Blacklist expired for user.',
});
});
}

// Send action to logs channel
modActions(moderator, {
user,
Expand Down Expand Up @@ -230,6 +215,25 @@ export async function addUserBlacklist(moderator: discord.User, user: discord.Us
});
}

// set an unblacklist timer if there is an expire duration
if (expires) {
scheduleJob(`blacklist_user-${user.id}`, expires, async () => {
const tempUser = user as discord.User;
const filter = { where: { userId: tempUser.id } };

// only call .delete if the document exists
// or prisma will error
if (await prisma.blacklistedUsers.findFirst(filter)) {
await prisma.blacklistedUsers.delete(filter);
modActions(tempUser.client.user, {
user: tempUser,
action: 'unblacklistUser',
blacklistReason: dbUser.reason,
reason: 'Blacklist expired for user.',
}).catch(() => null);
}
});
}
return dbUser;
}

Expand All @@ -246,20 +250,6 @@ export async function addServerBlacklist(moderator: discord.User, guild: discord
},
});

// set an unblacklist timer if there is an expire duration
if (expires) {
scheduleJob(`blacklist_server-${guild.id}`, expires, async () => {
const tempGuild = guild as discord.Guild;
await prisma.blacklistedServers.delete({ where: { serverId: tempGuild.id } });

modActions(tempGuild.client.user, {
dbGuild,
action: 'unblacklistServer',
timestamp: new Date(),
reason: 'Blacklist expired for user.' });
});
}

// Send action to logs channel
modActions(moderator, {
guild: { id: guild.id, resolved: guild },
Expand All @@ -268,6 +258,25 @@ export async function addServerBlacklist(moderator: discord.User, guild: discord
reason,
}).catch(() => null);

// set an unblacklist timer if there is an expire duration
if (expires) {
scheduleJob(`blacklist_server-${guild.id}`, expires, async () => {
const tempGuild = guild as discord.Guild;
const filter = { where: { serverId: tempGuild.id } };

// only call .delete if the document exists
// or prisma will error
if (await prisma.blacklistedServers.findFirst(filter)) {
await prisma.blacklistedServers.delete(filter);
modActions(tempGuild.client.user, {
dbGuild,
action: 'unblacklistServer',
timestamp: new Date(),
reason: 'Blacklist expired for server.',
}).catch(() => null);
}
});
}
return dbGuild;
}

Expand Down

0 comments on commit 6f9b698

Please sign in to comment.