Skip to content

Commit

Permalink
Added the new warning system.
Browse files Browse the repository at this point in the history
  • Loading branch information
mleandrojr committed Apr 22, 2024
1 parent 8672425 commit 1718d63
Show file tree
Hide file tree
Showing 21 changed files with 631 additions and 238 deletions.
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`;
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
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());
}
}
224 changes: 0 additions & 224 deletions src/command/Warn.ts

This file was deleted.

Loading

0 comments on commit 1718d63

Please sign in to comment.