module Degenz.Admin open System.Threading.Tasks open DSharpPlus open DSharpPlus.Entities open DSharpPlus.EventArgs open DSharpPlus.SlashCommands open DSharpPlus.SlashCommands.Attributes open Degenz.Messaging type InitEmbeds = | Dojo = 0 | Whitelist = 1 | Slots = 2 let handleGuildDownloadReady (_ : DiscordClient) (event : GuildDownloadCompletedEventArgs) = task { let ( _ , guild ) = event.Guilds.TryGetValue(GuildEnvironment.guildId) let! commands = guild.GetApplicationCommandsAsync() let ( _ , adminRole ) = guild.Roles.TryGetValue(GuildEnvironment.roleAdmin) let permission = DiscordApplicationCommandPermission(adminRole, true) let commands = commands |> Seq.map (fun com -> DiscordGuildApplicationCommandPermissions(com.Id, [ permission ])) do! guild.BatchEditApplicationCommandPermissionsAsync(commands) |> Async.AwaitTask |> Async.Ignore } :> Task let sendEmbed embed (ctx : IDiscordContext) = task { match embed with | InitEmbeds.Dojo -> Trainer.sendInitialEmbed ctx | InitEmbeds.Whitelist -> InviteTracker.sendInitialEmbed ctx | InitEmbeds.Slots -> SlotMachine.sendInitialEmbed ctx | _ -> () do! Messaging.sendSimpleResponse ctx "Sent!" } :> Task type AdminBot() = inherit ApplicationCommandModule () let enforceAdmin (ctx : IDiscordContext) (adminFn : IDiscordContext -> Task) = let isAdmin = Seq.exists (fun (role : DiscordRole) -> role.Id = GuildEnvironment.roleAdmin) (ctx.GetDiscordMember().Roles) if isAdmin then adminFn ctx else Messaging.sendSimpleResponse ctx $"You are not admin" |> Async.StartAsTask :> Task [] member this.GetAttributions (ctx : InteractionContext, [] user : DiscordUser) = enforceAdmin (DiscordInteractionContext ctx) (InviteTracker.getInvitedUsersForId) [] member this.SetStock (ctx : InteractionContext, [] amount : int64) = enforceAdmin (DiscordInteractionContext ctx) (InviteTracker.setWhitelistStock (int amount)) [] member this.SendEmbedToChannel (ctx : InteractionContext, [] embed : InitEmbeds) = enforceAdmin (DiscordInteractionContext ctx) (sendEmbed embed)