Skip to content

Commit

Permalink
Merge pull request Desenvolvimento-de-Software#16 from mleandrojr/master
Browse files Browse the repository at this point in the history
Added the new warning system.
  • Loading branch information
mleandrojr authored Apr 22, 2024
2 parents cf81b47 + 1718d63 commit 09d99e7
Show file tree
Hide file tree
Showing 34 changed files with 1,028 additions and 282 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ package-lock.json
!log/.gitkeep
log/*
node_modules
bun.lockb
9 changes: 9 additions & 0 deletions sql/update_2023-11-18_01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS `macros` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`chat_id` int(10) unsigned NOT NULL,
`macro` varchar(50) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_macro_id_chat_id` (`chat_id`),
CONSTRAINT `fk_macro_id_chat_id` FOREIGN KEY (`chat_id`) REFERENCES `chats` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
2 changes: 2 additions & 0 deletions sql/update_2024-04-22_01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `warns` ADD `status` TINYINT(1) UNSIGNED NOT NULL DEFAULT 1 AFTER `reason`;
RENAME TABLE `warns` TO `warnings`;
2 changes: 1 addition & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class App {
*
* @var {number}
*/
private port: number;
private readonly port: number;

/**
* The constructor.
Expand Down
4 changes: 2 additions & 2 deletions src/action/Greetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ export default class Greetings extends Action {
const captchaButton: InlineKeyboardButton = {
text: Lang.get("captchaButton"),
callbackData: JSON.stringify({
callback: "captchaconfirmation",
data: {
c: "captchaconfirmation",
d: {
userId: this.context.newChatMember!.getId()
}
})
Expand Down
7 changes: 6 additions & 1 deletion src/action/SaveMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import UserHelper from "../helper/User.js";
import ChatHelper from "../helper/Chat.js";
import Messages from "../model/Messages.js";

export default class saveUserAndChat extends Action {
export default class SaveMessage extends Action {

/**
* The constructor.
Expand Down Expand Up @@ -45,6 +45,10 @@ export default class saveUserAndChat extends Action {
const user = await UserHelper.getByTelegramId(contextUser.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());

if (!user || !chat) {
return Promise.resolve();
}

switch (this.context.type) {

case "editedMessage":
Expand Down Expand Up @@ -73,6 +77,7 @@ export default class saveUserAndChat extends Action {

const message = new Messages();
const insert = message.insert();

insert
.set("type", this.context.type)
.set("user_id", user.id)
Expand Down
6 changes: 3 additions & 3 deletions src/action/SaveUserAndChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import UserHelper from "../helper/User.js";
import ChatHelper from "../helper/Chat.js";
import RelUsersChats from "../model/RelUsersChats.js";

export default class saveUserAndChat extends Action {
export default class SaveUserAndChat extends Action {

/**
* The constructor.
Expand All @@ -39,10 +39,10 @@ export default class saveUserAndChat extends Action {

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

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

UserHelper.updateUser(contextUser);
ChatHelper.updateChat(this.context.chat);
Expand Down
4 changes: 2 additions & 2 deletions src/callback/Callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export default abstract class Callback {
*/
public isCalled(): boolean {

if (!this.context.callbackQuery?.callbackData || !this.context.callbackQuery?.callbackData.callback) {
if (!this.context.callbackQuery?.callbackData || !this.context.callbackQuery?.callbackData.c) {
return false;
}

return this.callbacks.includes(this.context.callbackQuery?.callbackData.callback);
return this.callbacks.includes(this.context.callbackQuery?.callbackData.c);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/callback/CaptchaConfirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class CaptchaConfirmation extends Callback {
*/
public async run(): Promise<void> {

if (this.context.callbackQuery?.callbackData.data.userId !== this.context.user.getId()) {
if (this.context.callbackQuery?.callbackData.d.userId !== this.context.user.getId()) {
return;
}

Expand Down
103 changes: 103 additions & 0 deletions src/callback/Warning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Ada Lovelace Telegram Bot
*
* This file is part of Ada Lovelace Telegram Bot.
* You are free to modify and share this project or its files.
*
* @package mslovelace_bot
* @author Marcos Leandro <mleandrojr@yggdrasill.com.br>
* @license GPLv3 <http://www.gnu.org/licenses/gpl-3.0.en.html>
*/

import Callback from "./Callback.js";
import Warnings from "../model/Warnings.js";
import Context from "../library/telegram/context/Context.js";
import UserHelper from "../helper/User.js";
import ChatHelper from "../helper/Chat.js";
import Lang from "../helper/Lang.js";
import { parse } from "dotenv";

export default class CaptchaConfirmation extends Callback {

/**
* The constructor.
*
* @author Marcos Leandro
* @since 2024-04-22
*
* @param context
*/
public constructor(context: Context) {
super(context);
this.setCallbacks(["warning"]);
}

/**
* Command main route.
*
* @author Marcos Leandro
* @since 2024-04-22
*/
public async run(): Promise<void> {

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

if (!user || !chat) {
this.context.callbackQuery?.answer(Lang.get("adminOnlyAction"));
return;
}

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

if (!await this.context.user.isAdmin()) {
this.context.callbackQuery?.answer(Lang.get("adminOnlyAction"));
}

const [userId, chatId, warningId] = this.context.callbackQuery?.callbackData?.d?.split(",");
this.context.callbackQuery?.answer("OK");
this.context.message.delete();

const contextUser = await UserHelper.getByTelegramId(userId);
await this.remove(userId, chatId, warningId);

let message = Lang.get(typeof warningId === "undefined" ? "warningAdminRemovedAll" : "warningAdminRemovedLast")
.replace("{userid}", contextUser.user_id)
.replace("{username}", contextUser.first_name || user.username)
.replace("{adminId}", this.context.user.getId())
.replace("{adminUsername}", this.context.user.getFirstName() || this.context.user.getUsername());

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

/**
* Removes one or all the warnings.
*
* @author Marcos Leandro
* @since 2024-04-22
*
* @param userId
* @param chatId
* @param warningId
*/
private async remove(userId: number, chatId: number, warningId: number|undefined = undefined): Promise<void> {

const user = await UserHelper.getByTelegramId(userId);
const chat = await ChatHelper.getByTelegramId(chatId);

const warnings = new Warnings();
const update = warnings.update();

update
.set("status", 0)
.where("user_id").equal(user!.id)
.and("chat_id").equal(chat!.id);

if (typeof warningId !== "undefined") {
update.and("id").equal(warningId);
}

await warnings.execute();
}

}
6 changes: 3 additions & 3 deletions src/callback/Yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ export default class Yarn extends Callback {
*/
public async run(): Promise<void> {

if (!this.context.callbackQuery?.callbackData.data.package) {
if (!this.context.callbackQuery?.callbackData.d.package) {
return;
}

const yarnCommand = new YarnCommand(this.context);
await yarnCommand.getPackage(this.context.callbackQuery?.callbackData.data.package);
this.context.callbackQuery.answer(this.context.callbackQuery?.callbackData.data.package.toUpperCase());
await yarnCommand.getPackage(this.context.callbackQuery?.callbackData.d.package);
this.context.callbackQuery.answer(this.context.callbackQuery?.callbackData.d.package.toUpperCase());
}
}
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 (await contextUser.ban()) {
this.saveBan(contextUser, reason);
}

return Promise.resolve();
}

/**
Expand Down
Loading

0 comments on commit 09d99e7

Please sign in to comment.