Skip to content

Commit

Permalink
Merge pull request Desenvolvimento-de-Software#24 from mleandrojr/master
Browse files Browse the repository at this point in the history
Fixed the federation commands and bumped MySQL libraries.
  • Loading branch information
mleandrojr authored Aug 9, 2024
2 parents 4824231 + dffd8cd commit 2a2e062
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 101 deletions.
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
"main": "src/index.ts",
"type": "module",
"scripts": {
"start": "node -r dotenv/config ./dist/index.js",
"start": "bun -r dotenv/config ./dist/index.js",
"build": "tsc --project ./",
"watch": "nodemon --watch './**/*.{ts,graphql}' --exec 'npm run watch-arg'",
"watch-arg": "node --loader ts-node/esm -r dotenv/config -r tsconfig-paths/register src/index.ts",
"dev": "tsc-watch --onSuccess \"npm run watch\""
"watch": "bun --watch ./src/index.ts"
},
"repository": {
"type": "git",
Expand All @@ -25,13 +23,11 @@
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mysql": "^2.18.1",
"mysql2": "^2.3.3",
"node-fetch": "^3.1.0"
"mysql2": "^3.11.0"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/mysql": "^2.15.19",
"nodemon": "^2.0.15",
"ts-node": "^10.9.1",
"tsc-watch": "^4.5.0",
"tsconfig-paths": "^3.12.0",
Expand Down
10 changes: 9 additions & 1 deletion src/callback/CaptchaConfirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Context from "../library/telegram/context/Context.js";
import UserHelper from "../helper/User.js";
import ChatHelper from "../helper/Chat.js";
import RelUsersChats from "../model/RelUsersChats.js";
import Lang from "../helper/Lang.js";
import { ChatPermissions } from "../library/telegram/type/ChatPermissions.js";

export default class CaptchaConfirmation extends Callback {
Expand All @@ -40,7 +41,7 @@ export default class CaptchaConfirmation extends Callback {
*/
public async run(): Promise<void> {

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

Expand All @@ -56,6 +57,12 @@ export default class CaptchaConfirmation extends Callback {
return;
}

Lang.set(chat.language || "us");
if (this.context.callbackQuery.callbackData.d.userId !== this.context.user.getId()) {
this.context.callbackQuery.answer(Lang.get("captchaNotSameUser"));
return;
}

const relUsersChats = new RelUsersChats();
relUsersChats
.update()
Expand Down Expand Up @@ -83,6 +90,7 @@ export default class CaptchaConfirmation extends Callback {
canManageTopics: false
};

this.context.callbackQuery.answer(Lang.get("captchaConfirmed"));
await this.context.user.setPermissions(permissions);

if (chat.restrict_new_users) {
Expand Down
1 change: 0 additions & 1 deletion src/callback/Warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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 Warning extends Callback {

Expand Down
14 changes: 9 additions & 5 deletions src/command/Ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class Ban extends Command {
let params = command.getParams() || [];

const replyToMessage = this.context.message.getReplyToMessage();
if (replyToMessage && command.command === "delban") {
if (replyToMessage && command.getCommand() === "delban") {
replyToMessage.delete();
}

Expand Down Expand Up @@ -112,10 +112,12 @@ export default class Ban extends Command {
*/
private async banByReply(replyToMessage: Message, reason: string): Promise<void> {

if (await replyToMessage.getUser().ban()) {
this.saveBan(replyToMessage.getUser(), reason);
if (!await replyToMessage.getUser().ban()) {
const message = Lang.get("banErrorMessage");
return this.context.chat.sendMessage(message, { parseMode: "HTML" });
}

this.saveBan(replyToMessage.getUser(), reason);
return Promise.resolve();
}

Expand All @@ -129,10 +131,12 @@ export default class Ban extends Command {
*/
private async banByMention(mention: User, reason: string): Promise<void> {

if (await mention.ban()) {
this.saveBan(mention, reason);
if (!await mention.ban()) {
const message = Lang.get("banErrorMessage");
return this.context.chat.sendMessage(message, { parseMode: "HTML" });
}

this.saveBan(mention, reason);
return Promise.resolve();
}

Expand Down
23 changes: 9 additions & 14 deletions src/command/Rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Rules extends Command {
* @author Marcos Leandro
* @since 2024-07-30
*/
private chat: Record<string, any>;
private chat: Record<string, any> | undefined;

/**
* Command context.
Expand All @@ -50,7 +50,7 @@ export default class Rules extends Command {
*
* @var {CommandContext}
*/
private command: CommandContext;
private command: CommandContext | undefined;

/**
* The constructor.
Expand Down Expand Up @@ -84,7 +84,7 @@ export default class Rules extends Command {
Lang.set(chat?.language || "us");

this.command = command;
this.chat = chat;
this.chat = chat || { id: null };

this.context.message.delete();
switch (this.command.getCommand()) {
Expand Down Expand Up @@ -136,17 +136,12 @@ export default class Rules extends Command {
*/
private async addrules(): Promise<void> {

const text = this.context.message.getText().split(/\s+/);
const text = this.context.message.getText().replace(`/${this.command!.getCommand()}`, "").trim();
if (!text.length || text.length < 2) {
return;
}

const rules = text
.slice(1)
.join(" ")
.replace(/^\\n/, "")
.replace(/\\n$/, "")
.trim();
const rules = text.replace(/^\\n/, "").replace(/\\n$/, "").trim();

try {

Expand All @@ -173,7 +168,7 @@ export default class Rules extends Command {
const chatRules = new ChatRules();
chatRules
.delete()
.where("chat_id").equal(this.chat.id);
.where("chat_id").equal(this.chat!.id);

await chatRules.execute();
}
Expand All @@ -192,22 +187,22 @@ export default class Rules extends Command {

chatRules
.select()
.where("chat_id").equal(this.chat.id);
.where("chat_id").equal(this.chat!.id);

const result = await chatRules.execute();
if (result.length) {

chatRules
.update()
.set("rules", rules)
.where("chat_id").equal(this.chat.id);
.where("chat_id").equal(this.chat!.id);

return await chatRules.execute();
}

chatRules
.insert()
.set("chat_id", this.chat.id)
.set("chat_id", this.chat!.id)
.set("rules", rules);

return await chatRules.execute();
Expand Down
11 changes: 7 additions & 4 deletions src/command/Send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ export default class setCommands extends Command {
return;
}

const currentCommand = command.getCommand();
let text = this.context.message.getText();
text = text.replace(`/${currentCommand}`, "").trim();

this.context.message.delete();

const params = command.getParams();
if (!params || !params.length) {
return;
if (!text.length) {
return Promise.resolve();
}

this.context.chat.sendMessage(params.join(" "));
this.context.chat.sendMessage(text, { parseMode: "HTML" });
}
}
39 changes: 27 additions & 12 deletions src/command/federation/Ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Federation from "./Federation.js";
import Context from "../../library/telegram/context/Context.js";
import Message from "../../library/telegram/context/Message.js";
import User from "../../library/telegram/context/User.js";
import { BotCommand } from "../library/telegram/type/BotCommand.js";
import { BotCommand } from "../../library/telegram/type/BotCommand.js";
import UserHelper from "../../helper/User.js";
import ChatHelper from "../../helper/Chat.js";
import FederationHelper from "../../helper/Federation.js";
Expand Down Expand Up @@ -46,16 +46,16 @@ export default class Ban extends Federation {
}

/**
* Bans an user in the federation.
* Bans an user from the federation.
*
* @author Marcos Leandro
* @since 2023-07-04
* @since 2023-08-09
*
* @return
* @return Promise<void>
*/
private async ban(): Promise<void> {

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

Expand Down Expand Up @@ -99,14 +99,21 @@ export default class Ban extends Federation {
*/
private async banByReply(replyToMessage: Message, reason: string): Promise<void> {

const user = UserHelper.getByTelegramId(replyToMessage.getUser().getId());
const user = await UserHelper.getByTelegramId(replyToMessage.getUser().getId());
const federationChats = await FederationHelper.getChats(this.federation!);

for (const chat of federationChats) {
const context = this.getContext(user, chat);
this.saveBan(context, reason);
context.user.ban();
}

const message = Lang.get("fedBannedMessage")
.replace("{userId}", user.user_id)
.replace("{username}", user.first_name || user.username || user.user_id)
.replace("{reason}", reason.length ? reason : "Unknown");

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

/**
Expand All @@ -127,6 +134,13 @@ export default class Ban extends Federation {
this.saveBan(context, reason);
context.user.ban();
}

const message = Lang.get("fedBannedMessage")
.replace("{userId}", user.user_id)
.replace("{username}", user.first_name || user.username || user.user_id)
.replace("{reason}", reason.length ? reason : "Unknown");

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

/**
Expand All @@ -150,6 +164,13 @@ export default class Ban extends Federation {
this.saveBan(context, reason);
context.user.ban();
}

const message = Lang.get("fedBannedMessage")
.replace("{userId}", user.user_id)
.replace("{username}", user.first_name || user.username || user.user_id)
.replace("{reason}", reason.length ? reason : "Unknown");

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

/**
Expand Down Expand Up @@ -189,12 +210,6 @@ export default class Ban extends Federation {
try {

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

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

} catch (err: any) {
Log.error(err.toString());
Expand Down
11 changes: 3 additions & 8 deletions src/command/federation/Federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,12 @@ export default class Federation extends Command {
return;
}

if (!this.chat?.federation_id) {
return;
}
Lang.set(this.chat!.language || "us");

this.federation = await FederationHelper.getById(Number(this.chat?.federation_id));
if (!this.federation) {
return;
if (this.chat.federation_id) {
this.federation = await FederationHelper.getById(Number(this.chat?.federation_id));
}

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

this.command = command;
const action = this.command!.getCommand().substring(1);
this[action as keyof typeof Federation.prototype](true as never);
Expand Down
39 changes: 38 additions & 1 deletion src/command/federation/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
*/

import Federation from "./Federation.js";
import ChatHelper from "../../helper/Chat.js";
import Chats from "../../model/Chats.js";
import Context from "../../library/telegram/context/Context.js";
import { BotCommand } from "../library/telegram/type/BotCommand.js";
import CommandContext from "../../library/telegram/context/Command.js";
import { BotCommand } from "../../library/telegram/type/BotCommand.js";
import FederationsHelper from "../../helper/Federation.js";
import Lang from "../../helper/Lang.js";
import Log from "../../helper/Log.js";
import UserHelper from "../../helper/User.js";

export default class Group extends Federation {

Expand All @@ -28,6 +31,7 @@ export default class Group extends Federation {
* @var {BotCommand[]}
*/
public static readonly commands: BotCommand[] = [
{ command: "fshow", description: "Shows the federation information." },
{ command: "fjoin", description: "Joins a federation." },
{ command: "fleave", description: "Leaves a federation." }
];
Expand All @@ -44,6 +48,39 @@ export default class Group extends Federation {
super(context);
}

/**
* Shows the group federation.
*
* @author Marcos Leandro
* @since 2024-09-09
*
* @return {Promise<void>}
*/
private async show(): Promise<void> {

if (this.context.chat.getType() === "private") {
this.context.message.reply(Lang.get("federationCommandOnlyGroupError"));
return;
}

if (!this.chat?.federation_id) {
this.context.message.reply(Lang.get("federationLeaveNoFederationError"));
return;
}

const federation = await FederationsHelper.getById(this.chat!.federation_id);
if (!federation) {
this.context.message.reply(Lang.get("federationLeaveNoFederationError"));
return;
}

const message = Lang.get("federationDetails")
.replace("{federation}", federation.description)
.replace("{hash}", federation.hash);

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

/**
* Joins a federation.
*
Expand Down
Loading

0 comments on commit 2a2e062

Please sign in to comment.