forked from Desenvolvimento-de-Software/mslovelace_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Desenvolvimento-de-Software#16 from mleandrojr/master
Added the new warning system.
- Loading branch information
Showing
34 changed files
with
1,028 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ package-lock.json | |
!log/.gitkeep | ||
log/* | ||
node_modules | ||
bun.lockb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.