Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mleandrojr/mslovelace_bot
Browse files Browse the repository at this point in the history
  • Loading branch information
mleandrojr committed Jul 5, 2023
2 parents 850858e + 25f57b9 commit 413fba6
Show file tree
Hide file tree
Showing 38 changed files with 1,433 additions and 156 deletions.
19 changes: 19 additions & 0 deletions sql/update_2023-07-04_01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE `ada`.`bans` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`chat_id` INT UNSIGNED NOT NULL,
`reason` VARCHAR(50) NULL,
`date` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `idx_user_id_chat_id` (`chat_id` ASC, `user_id` ASC) VISIBLE,
INDEX `fk_bans_user_id_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `fk_bans_user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ada`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bans_chat_id`
FOREIGN KEY (`chat_id`)
REFERENCES `ada`.`chats` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
6 changes: 3 additions & 3 deletions src/action/AdaShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ export default class AdaShield extends Action {
*/
private async updateRelationship(): Promise<void> {

const user = await UserHelper.getUserByTelegramId(this.context.newChatMember!.getId());
const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const user = await UserHelper.getByTelegramId(this.context.newChatMember!.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
const relUserChat = new RelUsersChats();
relUserChat
.update()
.set("joined", 0)
.where("user_id").equal(user.id)
.and("chat_id").equal(chat.id);
.and("chat_id").equal(chat!.id);

return relUserChat.execute();
}
Expand Down
4 changes: 2 additions & 2 deletions src/action/AskToAsk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export default class AskToAsk extends Action {
return;
}

const chat = await ChatHelper.getChatByTelegramId(
const chat = await ChatHelper.getByTelegramId(
this.context.chat.getId()
);

if (!chat.warn_ask_to_ask) {
if (!chat?.warn_ask_to_ask) {
return;
}
if (this.context.message.getReplyToMessage()) {
Expand Down
2 changes: 1 addition & 1 deletion src/action/Captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Captcha extends Action {
return;
}

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat?.id) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/action/Greetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class Greetings extends Action {
return;
}

this.chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
this.chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!this.chat || !this.chat?.id) {
return;
}
Expand All @@ -81,7 +81,7 @@ export default class Greetings extends Action {
return;
}

this.user = await UserHelper.getUserByTelegramId(this.context.user.getId());
this.user = await UserHelper.getByTelegramId(this.context.user.getId());
if (!await this.isUserJoined()) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/action/LeftChatMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export default class LeftChatMember extends Action {
return;
}

const user = await UserHelper.getUserByTelegramId(
const user = await UserHelper.getByTelegramId(
this.context.user.getId()
);

const chat = await ChatHelper.getChatByTelegramId(
const chat = await ChatHelper.getByTelegramId(
this.context.chat.getId()
);

Expand Down
2 changes: 1 addition & 1 deletion src/action/NewChatMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class NewChatMember extends Action {
return;
}

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat?.id) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/action/Ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Ping extends Action {
return;
}

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/action/SaveMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ export default class saveUserAndChat extends Action {
}

const contextUser = this.context.newChatMember || this.context.leftChatMember || this.context.user;
const user = await UserHelper.getUserByTelegramId(contextUser.getId());
const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const user = await UserHelper.getByTelegramId(contextUser.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());

switch (this.context.type) {

case "editedMessage":
this.updateMessage(user, chat);
this.updateMessage(user, chat!);
break;

default:
this.saveNewMessage(user, chat);
this.saveNewMessage(user, chat!);
}

return Promise.resolve();
Expand Down
6 changes: 3 additions & 3 deletions src/action/SaveUserAndChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export default class saveUserAndChat extends Action {
public async run(): Promise<void> {

const contextUser = this.context.newChatMember || this.context.leftChatMember || this.context.user;
const user = await UserHelper.getUserByTelegramId(contextUser.getId());
const user = await UserHelper.getByTelegramId(contextUser.getId());
const userId = user === null ? await UserHelper.createUser(contextUser) : user.id;

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chatId = chat === null ? await ChatHelper.createChat(this.context.chat) : chat.id;
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
const chatId = chat === null ? await ChatHelper.createChat(this.context.chat) : chat!.id;

UserHelper.updateUser(contextUser);
ChatHelper.updateChat(this.context.chat);
Expand Down
4 changes: 2 additions & 2 deletions src/callback/CaptchaConfirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export default class CaptchaConfirmation extends Callback {
return;
}

const user = await UserHelper.getUserByTelegramId(
const user = await UserHelper.getByTelegramId(
this.context.user.getId()
);

const chat = await ChatHelper.getChatByTelegramId(
const chat = await ChatHelper.getByTelegramId(
this.context.chat.getId()
);

Expand Down
4 changes: 2 additions & 2 deletions src/command/AdaShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class AdaShield extends Command {
*/
public async index(): Promise<void> {

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class AdaShield extends Command {
*/
public async change(status: number) {

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/command/Ask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export default class Ask extends Command {

this.context.message.delete();

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
Lang.set(chat.language || "us");
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
Lang.set(chat?.language || "us");

const replyToMessage = this.context.message.getReplyToMessage();
if (replyToMessage) {
Expand Down
116 changes: 105 additions & 11 deletions src/command/Ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,27 @@

import Command from "./Command.js";
import Context from "../library/telegram/context/Context.js";
import Message from "src/library/telegram/context/Message.js";
import User from "src/library/telegram/context/User.js";
import Message from "../library/telegram/context/Message.js";
import User from "../library/telegram/context/User.js";
import CommandContext from "../library/telegram/context/Command.js";
import UserHelper from "../helper/User.js";
import ChatHelper from "../helper/Chat.js";
import UserContext from "../library/telegram/context/User.js";
import Bans from "../model/Bans.js";
import Lang from "../helper/Lang.js";
import Log from "../helper/Log.js";
import { User as UserType } from "../library/telegram/type/User.js";

export default class Ban extends Command {

/**
* Command context.
*
* @author Marcos Leandro
* @since 2023-07-04
*/
private command?: CommandContext;

/**
* The constructor.
*
Expand All @@ -37,30 +52,36 @@ export default class Ban extends Command {
* @since 2023-06-07
*
* @param command
*
* @returns
*/
public async run(command: CommandContext): Promise<void> {

if (!await this.context.user.isAdmin()) {
return;
}

this.command = command;
this.context.message.delete();

let params = command.getParams() || [];

const replyToMessage = this.context.message.getReplyToMessage();
if (replyToMessage) {
this.banByReply(replyToMessage);
this.banByReply(replyToMessage, params.join(" ").trim());
return;
}

const mentions = await this.context.message.getMentions();
if (!mentions.length) {
return;
if (mentions.length) {
params = params.filter((param) => param.indexOf("@") !== 0);
mentions.forEach((mention) => {
this.banByMention(mention, params.join(" ").trim());
});
}

for (const mention of mentions) {
this.banByMention(mention);
const userId = parseInt(params[0]);
if (userId === Number(params[0])) {
params.shift();
this.banByUserId(userId, params.join(" ").trim());
}
}

Expand All @@ -72,7 +93,8 @@ export default class Ban extends Command {
*
* @returns void
*/
private async banByReply(replyToMessage: Message): Promise<Record<string, any>> {
private async banByReply(replyToMessage: Message, reason: string): Promise<Record<string, any>> {
this.saveBan(replyToMessage.getUser(), reason);
return replyToMessage.getUser().ban();
}

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

/**
* Bans the user by Telegram ID.
*
* @author Marcos Leandro
* @since 2023-07-04
*
* @param userId
* @param reason
*/
private async banByUserId(userId: number, reason: string): Promise<Record<string, any>|undefined> {

const user = await UserHelper.getByTelegramId(userId);

const userType: UserType = {
id: userId,
isBot: user?.is_bot,
firstName: user?.first_name,
lastName: user?.last_name,
username: user?.username || userId
};

const contextUser = new UserContext(userType, this.context.chat);
this.saveBan(contextUser, reason);
return contextUser.ban();
}

/**
* Saves the ban.
*
* @author Marcos Leandro
* @since 2023-07-04
*
* @param {User} contextUser User object.
*/
private async saveBan(contextUser: User, reason: string): Promise<void> {

const user = await UserHelper.getByTelegramId(contextUser.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());

if (!user || !chat) {
return;
}

Lang.set(chat.language || "us");

const ban = new Bans();
const insert = ban.insert();
insert
.set("user_id", user.id)
.set("chat_id", chat.id)
.set("date", Math.floor(Date.now() / 1000));

if (reason.length) {
insert.set("reason", reason);
}

try {

await ban.execute();
const message = Lang.get("bannedMessage")
.replace("{userId}", contextUser.getId())
.replace("{username}", contextUser.getFirstName() || contextUser.getUsername())
.replace("{reason}", reason.length ? reason : "Unknown");

this.context.chat.sendMessage(message, { parseMode: "HTML" });

} catch (err: any) {
Log.error(err.toString());
}
}
}
8 changes: 4 additions & 4 deletions src/command/Greetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default class GreetingsCommand extends Command {
*/
private async index(): Promise<void> {

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ export default class GreetingsCommand extends Command {
*/
private async on(): Promise<void> {

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
Expand All @@ -135,7 +135,7 @@ export default class GreetingsCommand extends Command {
*/
private async off(): Promise<void> {

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ export default class GreetingsCommand extends Command {

params.shift();

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
Expand Down
Loading

0 comments on commit 413fba6

Please sign in to comment.