From 6fa0b58349d36fb3286292587e13d7750a788a01 Mon Sep 17 00:00:00 2001 From: Basique Evangelist Date: Wed, 28 Apr 2021 00:06:09 +0300 Subject: [PATCH] Check for admin in commands --- JetHerald/Commands/CommandHelper.cs | 20 ++++++++++++++++++++ JetHerald/Commands/DiscordCommands.cs | 5 ++++- JetHerald/Commands/ListCommand.cs | 10 ++++++++-- JetHerald/Commands/SubscribeCommand.cs | 10 ++++++++-- JetHerald/Commands/UnsubscribeCommand.cs | 10 ++++++++-- JetHerald/JetHeraldBot.Discord.cs | 1 + JetHerald/JetHeraldBot.Telegram.cs | 6 +++--- 7 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 JetHerald/Commands/CommandHelper.cs diff --git a/JetHerald/Commands/CommandHelper.cs b/JetHerald/Commands/CommandHelper.cs new file mode 100644 index 0000000..df8445a --- /dev/null +++ b/JetHerald/Commands/CommandHelper.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; +using Telegram.Bot; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; + +namespace JetHerald.Commands +{ + public static class CommandHelper + { + public static async Task CheckAdministrator(TelegramBotClient bot, Message msg) + { + if (msg.Chat.Type != ChatType.Private) + { + var chatMember = await bot.GetChatMemberAsync(msg.Chat.Id, msg.From.Id); + return chatMember.Status is ChatMemberStatus.Administrator or ChatMemberStatus.Creator; + } + return true; + } + } +} \ No newline at end of file diff --git a/JetHerald/Commands/DiscordCommands.cs b/JetHerald/Commands/DiscordCommands.cs index 91f9840..42951f4 100644 --- a/JetHerald/Commands/DiscordCommands.cs +++ b/JetHerald/Commands/DiscordCommands.cs @@ -4,7 +4,7 @@ using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; using MySql.Data.MySqlClient; -namespace JetHerald +namespace JetHerald.Commands { [ModuleLifespan(ModuleLifespan.Transient)] public class DiscordCommands : BaseCommandModule @@ -60,6 +60,7 @@ namespace JetHerald [Command("list")] [Description("List all subscriptions in this channel.")] + [RequireUserPermissions(DSharpPlus.Permissions.ManageGuild)] public async Task ListSubscriptions(CommandContext ctx) { _ = ctx.TriggerTypingAsync(); @@ -73,6 +74,7 @@ namespace JetHerald [Command("subscribe")] [Description("Subscribes to a topic.")] + [RequireUserPermissions(DSharpPlus.Permissions.ManageGuild)] public async Task Subscribe( CommandContext ctx, [Description("The read token of the token to subscribe to.")] @@ -99,6 +101,7 @@ namespace JetHerald [Command("unsubscribe")] [Description("Unsubscribes from a topic.")] + [RequireUserPermissions(DSharpPlus.Permissions.ManageGuild)] public async Task Unsubscribe( CommandContext ctx, [Description("The name of the topic to unsubscribe from.")] diff --git a/JetHerald/Commands/ListCommand.cs b/JetHerald/Commands/ListCommand.cs index d8f9346..810abb6 100644 --- a/JetHerald/Commands/ListCommand.cs +++ b/JetHerald/Commands/ListCommand.cs @@ -1,20 +1,26 @@ using System.Linq; using System.Threading.Tasks; +using Telegram.Bot; using Telegram.Bot.Args; -namespace JetHerald +namespace JetHerald.Commands { public class ListCommand : IChatCommand { readonly Db db; + readonly TelegramBotClient bot; - public ListCommand(Db db) + public ListCommand(Db db, TelegramBotClient bot) { this.db = db; + this.bot = bot; } public async Task Execute(CommandString cmd, MessageEventArgs messageEventArgs) { + if (!await CommandHelper.CheckAdministrator(bot, messageEventArgs.Message)) + return null; + var msg = messageEventArgs.Message; var chatid = msg.Chat.Id; var topics = await db.GetTopicsForChat(NamespacedId.Telegram(chatid)); diff --git a/JetHerald/Commands/SubscribeCommand.cs b/JetHerald/Commands/SubscribeCommand.cs index 974b86c..09525ed 100644 --- a/JetHerald/Commands/SubscribeCommand.cs +++ b/JetHerald/Commands/SubscribeCommand.cs @@ -1,15 +1,18 @@ using System.Threading.Tasks; +using Telegram.Bot; using Telegram.Bot.Args; -namespace JetHerald +namespace JetHerald.Commands { public class SubscribeCommand : IChatCommand { readonly Db db; + readonly TelegramBotClient bot; - public SubscribeCommand(Db db) + public SubscribeCommand(Db db, TelegramBotClient bot) { this.db = db; + this.bot = bot; } public async Task Execute(CommandString cmd, MessageEventArgs args) @@ -17,6 +20,9 @@ namespace JetHerald if (cmd.Parameters.Length < 1) return null; + if (!await CommandHelper.CheckAdministrator(bot, args.Message)) + return null; + var chat = NamespacedId.Telegram(args.Message.Chat.Id); var token = cmd.Parameters[0]; diff --git a/JetHerald/Commands/UnsubscribeCommand.cs b/JetHerald/Commands/UnsubscribeCommand.cs index 86065d1..360d8df 100644 --- a/JetHerald/Commands/UnsubscribeCommand.cs +++ b/JetHerald/Commands/UnsubscribeCommand.cs @@ -1,15 +1,18 @@ using System.Threading.Tasks; +using Telegram.Bot; using Telegram.Bot.Args; -namespace JetHerald +namespace JetHerald.Commands { public class UnsubscribeCommand : IChatCommand { readonly Db db; + readonly TelegramBotClient bot; - public UnsubscribeCommand(Db db) + public UnsubscribeCommand(Db db, TelegramBotClient bot) { this.db = db; + this.bot = bot; } public async Task Execute(CommandString cmd, MessageEventArgs messageEventArgs) @@ -17,6 +20,9 @@ namespace JetHerald if (cmd.Parameters.Length < 1) return null; + if (!await CommandHelper.CheckAdministrator(bot, messageEventArgs.Message)) + return null; + var msg = messageEventArgs.Message; var chat = NamespacedId.Telegram(msg.Chat.Id); diff --git a/JetHerald/JetHeraldBot.Discord.cs b/JetHerald/JetHeraldBot.Discord.cs index 9687581..5feeb22 100644 --- a/JetHerald/JetHeraldBot.Discord.cs +++ b/JetHerald/JetHeraldBot.Discord.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using DSharpPlus; using DSharpPlus.CommandsNext; +using JetHerald.Commands; namespace JetHerald { diff --git a/JetHerald/JetHeraldBot.Telegram.cs b/JetHerald/JetHeraldBot.Telegram.cs index de095a9..6eeb7da 100644 --- a/JetHerald/JetHeraldBot.Telegram.cs +++ b/JetHerald/JetHeraldBot.Telegram.cs @@ -32,9 +32,9 @@ namespace JetHerald Commands = new ChatCommandRouter(Me.Username, Log); Commands.Add(new CreateTopicCommand(Db), "createtopic"); Commands.Add(new DeleteTopicCommand(Db), "deletetopic"); - Commands.Add(new SubscribeCommand(Db), "subscribe", "sub"); - Commands.Add(new UnsubscribeCommand(Db), "unsubscribe", "unsub"); - Commands.Add(new ListCommand(Db), "list"); + Commands.Add(new SubscribeCommand(Db, TelegramBot), "subscribe", "sub"); + Commands.Add(new UnsubscribeCommand(Db, TelegramBot), "unsubscribe", "unsub"); + Commands.Add(new ListCommand(Db, TelegramBot), "list"); HeartbeatCancellation = new(); HeartbeatTask = CheckHeartbeats(HeartbeatCancellation.Token);