From db7126e642f2da2172d749d05c3ffd8efff95046 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 8 Jan 2022 23:18:06 +0700 Subject: [PATCH] Test out embeds with buttons. Add/Remove role --- Program.fs | 104 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 88 insertions(+), 16 deletions(-) diff --git a/Program.fs b/Program.fs index f049664..ff7158b 100644 --- a/Program.fs +++ b/Program.fs @@ -13,6 +13,7 @@ type HackType = | DDos = 2 | Worm = 3 | Crack = 4 + | SomeOtherThing = 4 type DefenseType = | Firewall @@ -35,18 +36,41 @@ type Player = { DiscordId : uint16 Weapons : int array } - type Match = { scorePlayer1 : int round : int scorePlayer2 : int } +type EmptyGlobalCommandToAvoidFamousDuplicateSlashCommandsBug() = inherit ApplicationCommandModule () + type JoeBot() = inherit ApplicationCommandModule () - static let mutable currentMatch : Match option = None - + [] + member _.AddHackerRole (ctx : InteractionContext) = + async { + for role in ctx.Guild.Roles do + if role.Value.Name = "Hacker" then + do! ctx.Member.GrantRoleAsync(role.Value) + |> Async.AwaitTask + do! ctx.CreateResponseAsync("You are now an elite haxxor", true) + |> Async.AwaitTask + } |> Async.StartAsTask + :> Task + + [] + member _.RemoveHackerRole (ctx : InteractionContext) = + async { + for role in ctx.Member.Roles do + if role.Name = "Hacker" then + do! ctx.Member.RevokeRoleAsync(role) + |> Async.AwaitTask + do! ctx.CreateResponseAsync("You are now lame", true) + |> Async.AwaitTask + } |> Async.StartAsTask + :> Task + [] member _.StartMatch (ctx : InteractionContext, [] player : DiscordUser) = async { @@ -73,13 +97,13 @@ type JoeBot() = do! channel.Value.SendMessageAsync builder |> Async.AwaitTask |> Async.Ignore - - - let builder = DiscordInteractionResponseBuilder() - builder.IsEphemeral <- true - builder.Content <- $"Sending challenge to {player.Username}" - do! ctx.CreateResponseAsync (builder) - |> Async.AwaitTask + + + let builder = DiscordInteractionResponseBuilder() + builder.IsEphemeral <- true + builder.Content <- $"Sending challenge to {player.Username}" + do! ctx.CreateResponseAsync (builder) + |> Async.AwaitTask else let builder = DiscordInteractionResponseBuilder() builder.IsEphemeral <- true @@ -90,26 +114,73 @@ type JoeBot() = } |> Async.StartAsTask :> Task - [] - member _.Hack (ctx : InteractionContext, [] hackType : HackType) = + member _.Embed () = + let builder = DiscordEmbedBuilder() + builder.Color <- Optional(DiscordColor.Blurple) + builder.Description <- "This is a test embed" + let author = DiscordEmbedBuilder.EmbedAuthor() + author.Name <- "Joebot Pro" + author.Url <- "https://ferano.io" + author.IconUrl <- "https://i.kym-cdn.com/entries/icons/original/000/028/861/cover3.jpg" + builder.Author <- author + let footer = DiscordEmbedBuilder.EmbedFooter() + footer.Text <- "This is a footer" + footer.IconUrl <- "https://dg8krxphbh767.cloudfront.net/exercises/bird-watcher.svg" + builder.Footer <- footer + builder.Title <- "THIS IS A TITLE" + builder.ImageUrl <- "https://avatars3.githubusercontent.com/u/2642263" + builder.Build() + + [] + member this.Hack (ctx : InteractionContext, [] player : DiscordUser) = + let constructButtons (weapons : HackType list) = + weapons + |> Seq.map (fun hack -> + // TODO:L Button ID should be a GUID and we should keep an in-memory store of the buttons we're waiting for + DiscordButtonComponent( + ButtonStyle.Primary, + $"{hack}{System.Guid.NewGuid()}", + $"{hack}")) async { let builder = DiscordInteractionResponseBuilder() - do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder.WithContent(sprintf "%A" hackType)) + builder.AddEmbed (this.Embed()) |> ignore + + constructButtons [ HackType.Virus ; HackType.Crack ; HackType.Ransom ; HackType.Worm ; HackType.DDos ] + |> Seq.cast + |> builder.AddComponents + |> ignore + + constructButtons [ HackType.Virus ; HackType.Crack ; HackType.Ransom ; HackType.Worm ; HackType.DDos ] + |> Seq.cast + |> builder.AddComponents + |> ignore + + builder.AsEphemeral true |> ignore + + do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder) |> Async.AwaitTask } |> Async.StartAsTask :> Task + [] + member this.TestEmbed (ctx : InteractionContext) = + async { + + do! ctx.CreateResponseAsync (this.Embed() , true) + |> Async.AwaitTask + } |> Async.StartAsTask + :> Task + let mapper = FSharpBsonMapper() let db = new LiteDatabase("hacker-game.db", mapper) - let config = DiscordConfiguration() config.Token <- "OTIyNDIyMDIyMTI1MDEwOTU1.YcBOcw.JxfW1CSIwEO7j6RbRFCnPZ-HoTk" config.TokenType <- TokenType.Bot config.Intents <- DiscordIntents.All -// config.MinimumLogLevel <- LogLevel.Trace +//config.MinimumLogLevel <- Microsoft.Extensions.Logging.LogLevel.Trace let client = new DiscordClient(config) @@ -121,7 +192,8 @@ client.add_ComponentInteractionCreated(AsyncEventHandler( :> Task)) let slash = client.UseSlashCommands() -slash.RegisterCommands(); + +slash.RegisterCommands(922419263275425832uL); client.ConnectAsync () |> Async.AwaitTask