Skip to content

Commit

Permalink
add blocked check
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Aug 25, 2024
1 parent 5171b79 commit 65761b7
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/BaseClient/UtilModules/requestHandler/channels/addReaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default async (msg: Discord.Message<true>, emoji: string) => {
return e;
}

if (await hasBlocked(msg.author)) return undefined;

const resolvedEmoji = Discord.resolvePartialEmoji(emoji) as Discord.PartialEmoji;
if (!resolvedEmoji) {
const e = requestHandlerError(`Invalid Emoji ${emoji}`, []);
Expand All @@ -46,6 +48,8 @@ export default async (msg: Discord.Message<true>, emoji: string) => {
: (resolvedEmoji.name as string),
)
.catch((e) => {
saveBlocked(e.message, msg.author);

error(msg.guild, e);
return e as Discord.DiscordAPIError;
});
Expand All @@ -64,3 +68,50 @@ export const isReactable = (msg: Discord.Message<true>, emoji: string, me: Disco
(emoji.includes(':')
? msg.channel.permissionsFor(me).has(Discord.PermissionFlagsBits.UseExternalEmojis)
: true);

/**
* Checks if the user has blocked the bot.
*
* @param user - The user object or an object with `id` and `client` properties.
* @returns A boolean indicating whether the user has blocked the bot.
*/
const hasBlocked = async (user: Discord.User | { id: string; client: Discord.Client<true> }) => {
const u = await user.client.util.DataBase.blockingUsers.findUnique({
where: { userId: user.id },
});

if (!u) return false;

if (Number(u.created) < Date.now() - 2592000000) {
user.client.util.DataBase.blockingUsers
.delete({
where: { userId: user.id },
})
.then();

return false;
}

return true;
};

/**
* Saves the blocked reaction error and updates the blocking user in the database.
*
* @param error - The error message.
* @param user - The user who triggered the error.
*/
const saveBlocked = async (
error: string,
user: Discord.User | { id: string; client: Discord.Client<true> },
) => {
if (!error.includes('Reaction blocked')) return;

user.client.util.DataBase.blockingUsers
.upsert({
where: { userId: user.id },
create: { userId: user.id, created: Date.now() },
update: { created: Date.now() },
})
.then();
};

0 comments on commit 65761b7

Please sign in to comment.