Skip to content

Commit

Permalink
Added ban validation before the message is sent.
Browse files Browse the repository at this point in the history
  • Loading branch information
mleandrojr committed Apr 19, 2024
1 parent 9940961 commit 8adc7df
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
29 changes: 20 additions & 9 deletions src/command/Ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ export default class Ban extends Command {
*
* @returns void
*/
private async banByReply(replyToMessage: Message, reason: string): Promise<Record<string, any>> {
this.saveBan(replyToMessage.getUser(), reason);
return replyToMessage.getUser().ban();
private async banByReply(replyToMessage: Message, reason: string): Promise<void> {

if (await replyToMessage.getUser().ban()) {
this.saveBan(replyToMessage.getUser(), reason);
}

return Promise.resolve();
}

/**
Expand All @@ -106,9 +110,13 @@ export default class Ban extends Command {
*
* @returns void
*/
private async banByMention(mention: User, reason: string): Promise<Record<string, any>> {
this.saveBan(mention, reason);
return mention.ban();
private async banByMention(mention: User, reason: string): Promise<void> {

if (await mention.ban()) {
this.saveBan(mention, reason);
}

return Promise.resolve();
}

/**
Expand All @@ -120,7 +128,7 @@ export default class Ban extends Command {
* @param userId
* @param reason
*/
private async banByUserId(userId: number, reason: string): Promise<Record<string, any>|undefined> {
private async banByUserId(userId: number, reason: string): Promise<void> {

const user = await UserHelper.getByTelegramId(userId);

Expand All @@ -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();
}

/**
Expand Down
28 changes: 23 additions & 5 deletions src/library/telegram/context/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export default class User {
*
* @param untilDate
*/
public async ban(untilDate?: number): Promise<Record<string, any>> {
public async ban(untilDate?: number): Promise<boolean> {

const ban = new BanChatMember();
ban
Expand All @@ -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);
}
}

/**
Expand All @@ -229,7 +238,7 @@ export default class User {
*
* @param onlyIfBanned
*/
public async unban(onlyIfBanned?: boolean): Promise<Record<string, any>> {
public async unban(onlyIfBanned?: boolean): Promise<boolean> {

const unban = new UnbanChatMember();
unban
Expand All @@ -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);
}
}

/**
Expand All @@ -249,7 +267,7 @@ export default class User {
* @author Marcos Leandro
* @since 2023-06-02
*/
public async kick(): Promise<Record<string, any>> {
public async kick(): Promise<boolean> {
return this.unban(false);
}

Expand Down

0 comments on commit 8adc7df

Please sign in to comment.