Skip to content

Commit

Permalink
add ownerOnly & ownerOnlyCallback decorators, fix reset_all_warns
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukaizen committed Mar 11, 2024
1 parent 77eac5e commit 150ddb0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
27 changes: 27 additions & 0 deletions src/database/warns_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,31 @@ export async function reset_warn_numbers(chatId: string, userId: bigint, reasons
console.error(e)
return false;
}
}

export async function reset_all_warns(chatId: string, userId: bigint, reasons: string[]) {
try {
let warn_numbers = await prisma.warns.upsert({
where: {
chat_id: chatId,
user_id: userId,
user_id_chat_id: {user_id: userId, chat_id: chatId}
},
update: {
num_warns: 0n,
reasons: reasons
},
create: {
chat_id: chatId,
user_id: userId,
num_warns: 1n,
reasons: reasons
}
})
return true;
}
catch (e) {
console.error(e)
return false;
}
}
25 changes: 25 additions & 0 deletions src/helpers/helper_func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ export function typingAction(handler: any) {
}

// ==================== USER STUFF ====================

export function ownerOnly(handler: any) {
return async (ctx: any) => {
let user = await ctx.getAuthor();
if (ctx.from.id == constants.OWNER_ID || user.status == "creator") {
await handler(ctx);
}
else {
await ctx.reply("Only owner of this chat can use this command.", {reply_parameters: {message_id: ctx.message.message_id}});
}
}
}

export function ownerOnlyCallback(handler: any) {
return async (ctx: any) => {
let user = await ctx.getAuthor();
if (ctx.from.id == constants.OWNER_ID || user.status == "creator") {
await handler(ctx);
}
else {
await ctx.answerCallbackQuery({ text: "Only owner of this chat can use this command!"});
}
};
}

export function elevatedUsersOnly(handler: any) {
return async (ctx: any) => {
let user = await ctx.getAuthor();
Expand Down
35 changes: 25 additions & 10 deletions src/modules/warns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import {
get_warn_settings,
set_warn_numbers,
set_warn_settings,
reset_warn_numbers
reset_warn_numbers,
reset_all_warns
} from "../database/warns_sql";
import {
canRestrictUsers,
canRestrictUsersCallback,
canDeleteMessages, // for dwarn
canDeleteMessages,
checkElevatedUser,
checkElevatedUserFrom,
elevatedUsersOnly,
elevatedUsersCallbackOnly,
isUserRestricted,
userIdExtractor,
ownerOnly,
ownerOnlyCallback,
userInfo
} from "../helpers/helper_func";

Expand Down Expand Up @@ -568,9 +569,8 @@ bot.chatType("supergroup" || "group").command("resetwarns", elevatedUsersOnly(ca
warnReasons = warnReasons ?? [];
warnReasons.pop()
warnReasons = []

await reset_warn_numbers(ctx.chat.id.toString(), ctx.message.reply_to_message.from.id, warnReasons);

await reset_all_warns(ctx.chat.id.toString(), ctx.message.reply_to_message.from.id, warnReasons)
}
await ctx.api.sendMessage(ctx.chat.id, warns_message, {parse_mode: "HTML"});
}
Expand Down Expand Up @@ -620,9 +620,8 @@ bot.chatType("supergroup" || "group").command("resetwarns", elevatedUsersOnly(ca
warnReasons = warnReasons ?? [];
warnReasons.pop()
warnReasons = []

await reset_warn_numbers(ctx.chat.id.toString(), ctx.message.reply_to_message.from.id, warnReasons);


await reset_all_warns(ctx.chat.id.toString(), user_info.user.id, warnReasons)
}
await ctx.api.sendMessage(ctx.chat.id, warns_message, {parse_mode: "HTML"});
}
Expand All @@ -637,4 +636,20 @@ bot.chatType("supergroup" || "group").command("resetwarns", elevatedUsersOnly(ca
}
}
}
})));
})));

bot.chatType("supergroup" || "group").command("resetallwarns", ownerOnly(canRestrictUsers(async (ctx: any) => {
let confirmReset = new InlineKeyboard()
.text("Yes", "yes-reset")
.text("No", "no-reset")

await ctx.api.sendMessage(ctx.chat.id, "Are you sure you want to reset <b>everyone's</b> warnings in this chat?\n\n<i>This action cannot be undone.</i>", {reply_markup: confirmReset, parse_mode: "HTML"});
})));

bot.callbackQuery("yes-reset", ownerOnlyCallback(async(ctx: any) => {
// await reset_all_chat_warns(ctx.chat.id) // will do this later
}));

bot.callbackQuery("no-reset", ownerOnlyCallback(async(ctx: any) => {
await ctx.editMessageText("Okay fine. Tell me when you change your mind!", { parse_mode: "HTML" });
}));

0 comments on commit 150ddb0

Please sign in to comment.