Test out embeds with buttons. Add/Remove role

This commit is contained in:
Joseph Ferano 2022-01-08 23:18:06 +07:00
parent a770eaf82b
commit db7126e642

View File

@ -13,6 +13,7 @@ type HackType =
| DDos = 2 | DDos = 2
| Worm = 3 | Worm = 3
| Crack = 4 | Crack = 4
| SomeOtherThing = 4
type DefenseType = type DefenseType =
| Firewall | Firewall
@ -35,18 +36,41 @@ type Player = {
DiscordId : uint16 DiscordId : uint16
Weapons : int array Weapons : int array
} }
type Match = type Match =
{ scorePlayer1 : int { scorePlayer1 : int
round : int round : int
scorePlayer2 : int } scorePlayer2 : int }
type EmptyGlobalCommandToAvoidFamousDuplicateSlashCommandsBug() = inherit ApplicationCommandModule ()
type JoeBot() = type JoeBot() =
inherit ApplicationCommandModule () inherit ApplicationCommandModule ()
static let mutable currentMatch : Match option = None [<SlashCommand("redpill", "Take the redpill and become a hacker")>]
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
[<SlashCommand("bluepill", "Take the bluepill and become lame")>]
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
[<SlashCommand("challenge", "Challenge another user")>] [<SlashCommand("challenge", "Challenge another user")>]
member _.StartMatch (ctx : InteractionContext, [<Option("player", "Player you want to challenge")>] player : DiscordUser) = member _.StartMatch (ctx : InteractionContext, [<Option("player", "Player you want to challenge")>] player : DiscordUser) =
async { async {
@ -73,13 +97,13 @@ type JoeBot() =
do! channel.Value.SendMessageAsync builder do! channel.Value.SendMessageAsync builder
|> Async.AwaitTask |> Async.AwaitTask
|> Async.Ignore |> Async.Ignore
let builder = DiscordInteractionResponseBuilder() let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true builder.IsEphemeral <- true
builder.Content <- $"Sending challenge to {player.Username}" builder.Content <- $"Sending challenge to {player.Username}"
do! ctx.CreateResponseAsync (builder) do! ctx.CreateResponseAsync (builder)
|> Async.AwaitTask |> Async.AwaitTask
else else
let builder = DiscordInteractionResponseBuilder() let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true builder.IsEphemeral <- true
@ -90,26 +114,73 @@ type JoeBot() =
} |> Async.StartAsTask } |> Async.StartAsTask
:> Task :> Task
[<SlashCommand("hack", "Send a hack attack to the player")>] member _.Embed () =
member _.Hack (ctx : InteractionContext, [<Option("hack", "weapon attack")>] hackType : HackType) = 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()
[<SlashCommand("hack", "Send a hack attack to another player")>]
member this.Hack (ctx : InteractionContext, [<Option("player", "The player you want to hack")>] 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 { async {
let builder = DiscordInteractionResponseBuilder() 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<DiscordComponent>
|> builder.AddComponents
|> ignore
constructButtons [ HackType.Virus ; HackType.Crack ; HackType.Ransom ; HackType.Worm ; HackType.DDos ]
|> Seq.cast<DiscordComponent>
|> builder.AddComponents
|> ignore
builder.AsEphemeral true |> ignore
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|> Async.AwaitTask |> Async.AwaitTask
} |> Async.StartAsTask } |> Async.StartAsTask
:> Task :> Task
[<SlashCommand("test", "testing the embeds")>]
member this.TestEmbed (ctx : InteractionContext) =
async {
do! ctx.CreateResponseAsync (this.Embed() , true)
|> Async.AwaitTask
} |> Async.StartAsTask
:> Task
let mapper = FSharpBsonMapper() let mapper = FSharpBsonMapper()
let db = new LiteDatabase("hacker-game.db", mapper) let db = new LiteDatabase("hacker-game.db", mapper)
let config = DiscordConfiguration() let config = DiscordConfiguration()
config.Token <- "OTIyNDIyMDIyMTI1MDEwOTU1.YcBOcw.JxfW1CSIwEO7j6RbRFCnPZ-HoTk" config.Token <- "OTIyNDIyMDIyMTI1MDEwOTU1.YcBOcw.JxfW1CSIwEO7j6RbRFCnPZ-HoTk"
config.TokenType <- TokenType.Bot config.TokenType <- TokenType.Bot
config.Intents <- DiscordIntents.All config.Intents <- DiscordIntents.All
// config.MinimumLogLevel <- LogLevel.Trace //config.MinimumLogLevel <- Microsoft.Extensions.Logging.LogLevel.Trace
let client = new DiscordClient(config) let client = new DiscordClient(config)
@ -121,7 +192,8 @@ client.add_ComponentInteractionCreated(AsyncEventHandler(
:> Task)) :> Task))
let slash = client.UseSlashCommands() let slash = client.UseSlashCommands()
slash.RegisterCommands<JoeBot>();
slash.RegisterCommands<JoeBot>(922419263275425832uL);
client.ConnectAsync () client.ConnectAsync ()
|> Async.AwaitTask |> Async.AwaitTask