From 8adc7dfdd0af218e56dfd9b3a061068cb3285073 Mon Sep 17 00:00:00 2001 From: Marcos Leandro Date: Fri, 19 Apr 2024 19:41:17 -0300 Subject: [PATCH] Added ban validation before the message is sent. --- src/command/Ban.ts | 29 +++++++++++++++++++--------- src/library/telegram/context/User.ts | 28 ++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/command/Ban.ts b/src/command/Ban.ts index c88021c..ec4980a 100644 --- a/src/command/Ban.ts +++ b/src/command/Ban.ts @@ -93,9 +93,13 @@ export default class Ban extends Command { * * @returns void */ - private async banByReply(replyToMessage: Message, reason: string): Promise> { - this.saveBan(replyToMessage.getUser(), reason); - return replyToMessage.getUser().ban(); + private async banByReply(replyToMessage: Message, reason: string): Promise { + + if (await replyToMessage.getUser().ban()) { + this.saveBan(replyToMessage.getUser(), reason); + } + + return Promise.resolve(); } /** @@ -106,9 +110,13 @@ export default class Ban extends Command { * * @returns void */ - private async banByMention(mention: User, reason: string): Promise> { - this.saveBan(mention, reason); - return mention.ban(); + private async banByMention(mention: User, reason: string): Promise { + + if (await mention.ban()) { + this.saveBan(mention, reason); + } + + return Promise.resolve(); } /** @@ -120,7 +128,7 @@ export default class Ban extends Command { * @param userId * @param reason */ - private async banByUserId(userId: number, reason: string): Promise|undefined> { + private async banByUserId(userId: number, reason: string): Promise { const user = await UserHelper.getByTelegramId(userId); @@ -133,8 +141,11 @@ export default class Ban extends Command { }; const contextUser = new UserContext(userType, this.context.chat); - this.saveBan(contextUser, reason); - return contextUser.ban(); + if (contextUser.ban()) { + this.saveBan(contextUser, reason); + } + + return Promise.resolve(); } /** diff --git a/src/library/telegram/context/User.ts b/src/library/telegram/context/User.ts index fc43db7..114e3df 100644 --- a/src/library/telegram/context/User.ts +++ b/src/library/telegram/context/User.ts @@ -207,7 +207,7 @@ export default class User { * * @param untilDate */ - public async ban(untilDate?: number): Promise> { + public async ban(untilDate?: number): Promise { const ban = new BanChatMember(); ban @@ -218,7 +218,16 @@ export default class User { ban.setUntilDate(untilDate); } - return ban.post().then((response) => response.json()); + try { + + const response = await ban.post(); + const json = await response.json(); + + return Promise.resolve(json?.ok || false); + + } catch (error) { + return Promise.resolve(false); + } } /** @@ -229,7 +238,7 @@ export default class User { * * @param onlyIfBanned */ - public async unban(onlyIfBanned?: boolean): Promise> { + public async unban(onlyIfBanned?: boolean): Promise { const unban = new UnbanChatMember(); unban @@ -240,7 +249,16 @@ export default class User { unban.setOnlyIfBanned(false); } - return unban.post().then((response) => response.json()); + try { + + const response = await unban.post(); + const json = await response.json(); + + return Promise.resolve(json?.ok || false); + + } catch (error) { + return Promise.resolve(false); + } } /** @@ -249,7 +267,7 @@ export default class User { * @author Marcos Leandro * @since 2023-06-02 */ - public async kick(): Promise> { + public async kick(): Promise { return this.unban(false); }