Check for admin in commands

This commit is contained in:
Basique Evangelist 2021-04-28 00:06:09 +03:00
parent 89d5d2b9b5
commit 6fa0b58349
7 changed files with 52 additions and 10 deletions

View File

@ -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<bool> 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;
}
}
}

View File

@ -4,7 +4,7 @@ using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes; using DSharpPlus.CommandsNext.Attributes;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
namespace JetHerald namespace JetHerald.Commands
{ {
[ModuleLifespan(ModuleLifespan.Transient)] [ModuleLifespan(ModuleLifespan.Transient)]
public class DiscordCommands : BaseCommandModule public class DiscordCommands : BaseCommandModule
@ -60,6 +60,7 @@ namespace JetHerald
[Command("list")] [Command("list")]
[Description("List all subscriptions in this channel.")] [Description("List all subscriptions in this channel.")]
[RequireUserPermissions(DSharpPlus.Permissions.ManageGuild)]
public async Task ListSubscriptions(CommandContext ctx) public async Task ListSubscriptions(CommandContext ctx)
{ {
_ = ctx.TriggerTypingAsync(); _ = ctx.TriggerTypingAsync();
@ -73,6 +74,7 @@ namespace JetHerald
[Command("subscribe")] [Command("subscribe")]
[Description("Subscribes to a topic.")] [Description("Subscribes to a topic.")]
[RequireUserPermissions(DSharpPlus.Permissions.ManageGuild)]
public async Task Subscribe( public async Task Subscribe(
CommandContext ctx, CommandContext ctx,
[Description("The read token of the token to subscribe to.")] [Description("The read token of the token to subscribe to.")]
@ -99,6 +101,7 @@ namespace JetHerald
[Command("unsubscribe")] [Command("unsubscribe")]
[Description("Unsubscribes from a topic.")] [Description("Unsubscribes from a topic.")]
[RequireUserPermissions(DSharpPlus.Permissions.ManageGuild)]
public async Task Unsubscribe( public async Task Unsubscribe(
CommandContext ctx, CommandContext ctx,
[Description("The name of the topic to unsubscribe from.")] [Description("The name of the topic to unsubscribe from.")]

View File

@ -1,20 +1,26 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Args; using Telegram.Bot.Args;
namespace JetHerald namespace JetHerald.Commands
{ {
public class ListCommand : IChatCommand public class ListCommand : IChatCommand
{ {
readonly Db db; readonly Db db;
readonly TelegramBotClient bot;
public ListCommand(Db db) public ListCommand(Db db, TelegramBotClient bot)
{ {
this.db = db; this.db = db;
this.bot = bot;
} }
public async Task<string> Execute(CommandString cmd, MessageEventArgs messageEventArgs) public async Task<string> Execute(CommandString cmd, MessageEventArgs messageEventArgs)
{ {
if (!await CommandHelper.CheckAdministrator(bot, messageEventArgs.Message))
return null;
var msg = messageEventArgs.Message; var msg = messageEventArgs.Message;
var chatid = msg.Chat.Id; var chatid = msg.Chat.Id;
var topics = await db.GetTopicsForChat(NamespacedId.Telegram(chatid)); var topics = await db.GetTopicsForChat(NamespacedId.Telegram(chatid));

View File

@ -1,15 +1,18 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Args; using Telegram.Bot.Args;
namespace JetHerald namespace JetHerald.Commands
{ {
public class SubscribeCommand : IChatCommand public class SubscribeCommand : IChatCommand
{ {
readonly Db db; readonly Db db;
readonly TelegramBotClient bot;
public SubscribeCommand(Db db) public SubscribeCommand(Db db, TelegramBotClient bot)
{ {
this.db = db; this.db = db;
this.bot = bot;
} }
public async Task<string> Execute(CommandString cmd, MessageEventArgs args) public async Task<string> Execute(CommandString cmd, MessageEventArgs args)
@ -17,6 +20,9 @@ namespace JetHerald
if (cmd.Parameters.Length < 1) if (cmd.Parameters.Length < 1)
return null; return null;
if (!await CommandHelper.CheckAdministrator(bot, args.Message))
return null;
var chat = NamespacedId.Telegram(args.Message.Chat.Id); var chat = NamespacedId.Telegram(args.Message.Chat.Id);
var token = cmd.Parameters[0]; var token = cmd.Parameters[0];

View File

@ -1,15 +1,18 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Args; using Telegram.Bot.Args;
namespace JetHerald namespace JetHerald.Commands
{ {
public class UnsubscribeCommand : IChatCommand public class UnsubscribeCommand : IChatCommand
{ {
readonly Db db; readonly Db db;
readonly TelegramBotClient bot;
public UnsubscribeCommand(Db db) public UnsubscribeCommand(Db db, TelegramBotClient bot)
{ {
this.db = db; this.db = db;
this.bot = bot;
} }
public async Task<string> Execute(CommandString cmd, MessageEventArgs messageEventArgs) public async Task<string> Execute(CommandString cmd, MessageEventArgs messageEventArgs)
@ -17,6 +20,9 @@ namespace JetHerald
if (cmd.Parameters.Length < 1) if (cmd.Parameters.Length < 1)
return null; return null;
if (!await CommandHelper.CheckAdministrator(bot, messageEventArgs.Message))
return null;
var msg = messageEventArgs.Message; var msg = messageEventArgs.Message;
var chat = NamespacedId.Telegram(msg.Chat.Id); var chat = NamespacedId.Telegram(msg.Chat.Id);

View File

@ -1,6 +1,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using DSharpPlus; using DSharpPlus;
using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext;
using JetHerald.Commands;
namespace JetHerald namespace JetHerald
{ {

View File

@ -32,9 +32,9 @@ namespace JetHerald
Commands = new ChatCommandRouter(Me.Username, Log); Commands = new ChatCommandRouter(Me.Username, Log);
Commands.Add(new CreateTopicCommand(Db), "createtopic"); Commands.Add(new CreateTopicCommand(Db), "createtopic");
Commands.Add(new DeleteTopicCommand(Db), "deletetopic"); Commands.Add(new DeleteTopicCommand(Db), "deletetopic");
Commands.Add(new SubscribeCommand(Db), "subscribe", "sub"); Commands.Add(new SubscribeCommand(Db, TelegramBot), "subscribe", "sub");
Commands.Add(new UnsubscribeCommand(Db), "unsubscribe", "unsub"); Commands.Add(new UnsubscribeCommand(Db, TelegramBot), "unsubscribe", "unsub");
Commands.Add(new ListCommand(Db), "list"); Commands.Add(new ListCommand(Db, TelegramBot), "list");
HeartbeatCancellation = new(); HeartbeatCancellation = new();
HeartbeatTask = CheckHeartbeats(HeartbeatCancellation.Token); HeartbeatTask = CheckHeartbeats(HeartbeatCancellation.Token);