Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use native telegram permissions for moderators #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ Command | Description | Group Admin | Normal user
/setrules | `[by reply/username/id]` Set chat rules from the quoted message | ✅ | ❌
/setrules [url] | Set chat rules from the specified url | ✅ | ❌
/getrules | Get chat rules | ✅ | ✅
/addmod [username] | Promotes a user to moderator | ✅ | ❌
/removemod [username] | Demotes a user from moderator | ✅ | ❌
/ban | `[by reply/username/id]` Bans a user | ✅ | ❌
/addevidence [questionId] | `[by reply/username/id]` Adds the quoted message as evidence to the arbitrator of `questionId` | ✅ | ✅
/setlanguage | Sets the current chat language | ✅ | ❌
2 changes: 0 additions & 2 deletions lib/create-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@ import {openDb} from "./db";
*/
await db.exec('CREATE TABLE rules (chat_id INTEGER PRIMARY KEY, rules TEXT)');

await db.exec('CREATE TABLE mods (chat_id INTEGER, user_id INTEGER, PRIMARY KEY (chat_id, user_id))');

console.log('database created');
})();
39 changes: 0 additions & 39 deletions lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,42 +90,6 @@ const getRules = async (chatId: number) => {
return result?.rules || '';
}

const addMod = async (chatId: number, userId: number) => {
const db = await openDb();
await db.run(
'INSERT OR REPLACE INTO mods (chat_id, user_id) VALUES ($chatId, $userId);',
{
$chatId: chatId,
$userId: userId
}
);
}

const removeMod = async (chatId: number, userId: number) => {
const db = await openDb();
await db.run(
'DELETE FROM mods WHERE chat_id = $chatId AND user_id = $userId',
{
$chatId: chatId,
$userId: userId
}
);
}

const isMod = async (chatId: number, userId: number) => {
const db = await openDb();

const result = await db.get(
'SELECT COUNT(*) as total FROM mods WHERE chat_id = $chatId AND user_id = $userId',
{
$chatId: chatId,
$userId: userId
}
);

return result.total > 0;
}

const addBan = async (questionId: string, appType: string, appGroupId: string, appUserId: string, active: boolean) => {
const db = await openDb();

Expand Down Expand Up @@ -167,9 +131,6 @@ export {
setGroupAccount,
setRules,
getRules,
addMod,
removeMod,
isMod,
addBan,
setBan,
getDisputedBans
Expand Down
43 changes: 0 additions & 43 deletions lib/telegram/commands/addMod.ts

This file was deleted.

7 changes: 2 additions & 5 deletions lib/telegram/commands/ban.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as TelegramBot from "node-telegram-bot-api";
import {CommandCallback} from "../../../types";
import {addBan, getBot, getRules, isMod} from "../../db";
import {addBan, getBot, getRules} from "../../db";
import {processCommand as addEvidenceCommand} from "./addEvidence"
import {banUser} from "../../bot-core";

Expand Down Expand Up @@ -49,10 +49,7 @@ const callback: CommandCallback = async (bot: TelegramBot, msg: TelegramBot.Mess

const user = await bot.getChatMember(msg.chat.id, String(msg.from.id));

const isAdmin = user.status === 'creator' || user.status === 'administrator';
const isModerator = await isMod(msg.chat.id, msg.from.id);

const hasBanningPermission = isAdmin || isModerator;
const hasBanningPermission = user.status === 'creator' || user.status === 'administrator';

const {questionId, questionUrl: appealUrl} = await banUser(hasBanningPermission, fromUsername, rules, privateKey);

Expand Down
44 changes: 0 additions & 44 deletions lib/telegram/commands/removeMod.ts

This file was deleted.

6 changes: 0 additions & 6 deletions lib/telegram/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import * as TelegramBot from "node-telegram-bot-api";
import * as newAccount from "../../lib/telegram/commands/newAccount";
import * as setAccount from "../../lib/telegram/commands/setAccount";
import * as getAccount from "../../lib/telegram/commands/getAccount";
import * as addMod from "../../lib/telegram/commands/addMod";
import * as removeMod from "../../lib/telegram/commands/removeMod";
import * as setRules from "../../lib/telegram/commands/setRules";
import * as getRules from "../../lib/telegram/commands/getRules";
import * as ban from "../../lib/telegram/commands/ban";
Expand All @@ -19,10 +17,6 @@ const commands: {regexp: RegExp, callback: CommandCallback}[] = [
newAccount,
setAccount,
getAccount,
{regexp: addMod.regexpReply, callback: addMod.callbackReply},
{regexp: addMod.regexpUserId, callback: addMod.callbackUserId},
{regexp: removeMod.regexpReply, callback: removeMod.callbackReply},
{regexp: removeMod.regexpUserId, callback: removeMod.callbackUserId},
setRules,
getRules,
ban,
Expand Down