diff --git a/README.md b/README.md index b3beac3..a884749 100644 --- a/README.md +++ b/README.md @@ -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 | ✅ | ❌ diff --git a/lib/create-db.ts b/lib/create-db.ts index f5dd6ee..a4bfa5e 100644 --- a/lib/create-db.ts +++ b/lib/create-db.ts @@ -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'); })(); \ No newline at end of file diff --git a/lib/db.ts b/lib/db.ts index 481767c..751bb27 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -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(); @@ -167,9 +131,6 @@ export { setGroupAccount, setRules, getRules, - addMod, - removeMod, - isMod, addBan, setBan, getDisputedBans diff --git a/lib/telegram/commands/addMod.ts b/lib/telegram/commands/addMod.ts deleted file mode 100644 index 7449a9f..0000000 --- a/lib/telegram/commands/addMod.ts +++ /dev/null @@ -1,43 +0,0 @@ -import * as TelegramBot from "node-telegram-bot-api"; -import {CommandCallback} from "../../../types"; -import {addMod} from "../../db"; - -const processCommand = async (bot: TelegramBot, chatId: number, userId: number, modUserId: number) => { - const user = await bot.getChatMember(chatId, String(userId)); - - if (user.status === 'creator' || user.status === 'administrator') { - await addMod(chatId, modUserId); - await bot.sendMessage(chatId, `[${modUserId}](tg://user?id=${modUserId}) is now a mod.`, {parse_mode: 'Markdown'}); - } else { - await bot.sendMessage(chatId, `Only admins can execute this command.`); - } -} - -/* - * /addmod - */ -const regexpReply = /^\/addmod$/ - -const callbackReply: CommandCallback = async (bot: TelegramBot, msg: TelegramBot.Message) => { - - if (!msg.reply_to_message) { - await bot.sendMessage(msg.chat.id, `/addmod must be used in a reply`); - return; - } - - await processCommand(bot, msg.chat.id, msg.from.id, msg.reply_to_message.from.id); -} - -/* - * /addmod [userId] - */ -const regexpUserId = /^\/addmod (\d+)$/ - -const callbackUserId: CommandCallback = async (bot: TelegramBot, msg: TelegramBot.Message, match: string[]) => { - await processCommand(bot, msg.chat.id, msg.from.id, Number(match[1])); -} - -export { - regexpReply, callbackReply, - regexpUserId, callbackUserId -}; \ No newline at end of file diff --git a/lib/telegram/commands/ban.ts b/lib/telegram/commands/ban.ts index f4511a1..9644d78 100644 --- a/lib/telegram/commands/ban.ts +++ b/lib/telegram/commands/ban.ts @@ -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"; @@ -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); diff --git a/lib/telegram/commands/removeMod.ts b/lib/telegram/commands/removeMod.ts deleted file mode 100644 index 0a259fa..0000000 --- a/lib/telegram/commands/removeMod.ts +++ /dev/null @@ -1,44 +0,0 @@ -import * as TelegramBot from "node-telegram-bot-api"; -import {CommandCallback} from "../../../types"; -import {removeMod} from "../../db"; - -const processCommand = async (bot: TelegramBot, chatId: number, userId: number, modUserId: number) => { - const user = await bot.getChatMember(chatId, String(userId)); - - if (user.status === 'creator' || user.status === 'administrator') { - await removeMod(chatId, modUserId); - await bot.sendMessage(chatId, `[${modUserId}](tg://user?id=${modUserId}) is no longer a mod.`, {parse_mode: 'Markdown'}); - } else { - await bot.sendMessage(chatId, `Only admins can execute this command.`); - } -} - - -/* - * /removemod - */ -const regexpReply = /^\/removemod$/ - -const callbackReply: CommandCallback = async (bot: TelegramBot, msg: TelegramBot.Message) => { - - if (!msg.reply_to_message) { - await bot.sendMessage(msg.chat.id, `/removemod must be used in a reply`); - return; - } - - await processCommand(bot, msg.chat.id, msg.from.id, msg.reply_to_message.from.id); -} - -/* - * /removemod [userId] - */ -const regexpUserId = /^\/removemod (\d+)$/ - -const callbackUserId: CommandCallback = async (bot: TelegramBot, msg: TelegramBot.Message, match: string[]) => { - await processCommand(bot, msg.chat.id, msg.from.id, Number(match[1])); -} - -export { - regexpReply, callbackReply, - regexpUserId, callbackUserId -}; \ No newline at end of file diff --git a/lib/telegram/index.ts b/lib/telegram/index.ts index 353f261..3077fa5 100644 --- a/lib/telegram/index.ts +++ b/lib/telegram/index.ts @@ -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"; @@ -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,