namespace Degenz open System open DSharpPlus open DSharpPlus.Entities open DSharpPlus.EventArgs open DSharpPlus.SlashCommands open Newtonsoft.Json [] module ResultHelpers = let (>>=) x f = Result.bind f x let () x f = Result.map f x [] module Types = [] type mins [] type GBT type BattleClass = | Network | Exploit | Penetration type HackId = | Virus = 0 | RemoteAccess = 1 | Worm = 2 type ShieldId = | Firewall = 6 | Encryption = 7 | Cypher = 8 type ItemType = | Hack | Shield type BattleItem = { Id : int Name : string Cost : int Type : ItemType Class : BattleClass Power : int Cooldown : int } type HackResult = | Strong | Weak [] type DiscordPlayer = { Id: uint64; Name: string } [] type AttackResult = { Result : bool Target : DiscordPlayer } type ActionType = | Attack of AttackResult | Defense [] type Action = { ActionId : int Type : ActionType Timestamp : System.DateTime } [] type PlayerData = { DiscordId : uint64 Name : string Arsenal : BattleItem array Actions : Action array Bank : int } module Armory = let battleItems = let file = System.IO.File.ReadAllText("Items.json") JsonConvert.DeserializeObject(file) let hacks = battleItems |> Array.filter (fun bi -> match bi.Type with Hack -> true | Shield -> false) let shields = battleItems |> Array.filter (fun bi -> match bi.Type with Shield -> true | Hack -> false) let getItem itemId = battleItems |> Array.find (fun w -> w.Id = itemId) module Messaging = type InteractiveMessage = { ButtonId : string ButtonText : string Message : string } let getTimeText isCooldown (timespan : TimeSpan) timestamp = let span = if isCooldown then timespan - (DateTime.UtcNow - timestamp) else (DateTime.UtcNow - timestamp) let plural amount = if amount = 1 then "" else "s" let ``and`` = if span.Hours > 0 then "and " else "" let hours = if span.Hours > 0 then $"{span.Hours} hour{plural span.Hours} {``and``}" else String.Empty let totalMins = span.Minutes let minutes = if totalMins > 0 then $"{totalMins} minute{plural totalMins}" else "1 minute" $"{hours}{minutes}" let getShortTimeText (timespan : TimeSpan) timestamp = let remaining = timespan - (DateTime.UtcNow - timestamp) let hours = if remaining.Hours > 0 then $"{remaining.Hours}h " else String.Empty let minutesRemaining = if remaining.Hours = 0 then remaining.Minutes + 1 else remaining.Minutes $"{hours}{minutesRemaining}min" let sendSimpleResponse (ctx: InteractionContext) msg = async { let builder = DiscordInteractionResponseBuilder() builder.Content <- msg builder.AsEphemeral true |> ignore do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder) |> Async.AwaitTask } let sendFollowUpMessage (event : ComponentInteractionCreateEventArgs) msg = async { let builder = DiscordFollowupMessageBuilder() builder.IsEphemeral <- true builder.Content <- msg do! event.Interaction.CreateFollowupMessageAsync(builder) |> Async.AwaitTask |> Async.Ignore } let sendFollowUpMessageFromCtx (ctx : InteractionContext) msg = async { let builder = DiscordFollowupMessageBuilder() builder.IsEphemeral <- true builder.Content <- msg do! ctx.FollowUpAsync(builder) |> Async.AwaitTask |> Async.Ignore } let sendFollowUpMessageWithEmbed (event : ComponentInteractionCreateEventArgs) (embed : DiscordEmbed) msg = async { let builder = DiscordFollowupMessageBuilder() .AsEphemeral(true) .WithContent(msg) .AddEmbed(embed) do! event.Interaction.CreateFollowupMessageAsync(builder) |> Async.AwaitTask |> Async.Ignore } let sendFollowUpMessageWithButton (event : ComponentInteractionCreateEventArgs) interactiveMessage = async { let builder = DiscordFollowupMessageBuilder() let button = DiscordButtonComponent(ButtonStyle.Success, interactiveMessage.ButtonId, interactiveMessage.ButtonText) :> DiscordComponent builder.AddComponents [| button |] |> ignore builder.IsEphemeral <- true builder.Content <- interactiveMessage.Message do! event.Interaction.CreateFollowupMessageAsync(builder) |> Async.AwaitTask |> Async.Ignore } let updateMessageWithGreyedOutButtons (event : ComponentInteractionCreateEventArgs) interactiveMessage = async { let builder = DiscordInteractionResponseBuilder() let button = DiscordButtonComponent(ButtonStyle.Success, interactiveMessage.ButtonId, interactiveMessage.ButtonText, true) :> DiscordComponent builder.AddComponents [| button |] |> ignore builder.IsEphemeral <- true builder.Content <- interactiveMessage.Message do! event.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, builder) |> Async.AwaitTask } let sendInteractionEvent (event : ComponentInteractionCreateEventArgs) msg = async { let builder = DiscordInteractionResponseBuilder() builder.IsEphemeral <- true builder.Content <- msg do! event.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder) |> Async.AwaitTask } let handleResultWithResponse ctx fn (player : Result) = match player with | Ok p -> fn p | Error e -> async { do! sendFollowUpMessageFromCtx ctx e } let handleResultWithResponseFromEvent event fn (player : Result) = match player with | Ok p -> fn p | Error e -> async { do! sendFollowUpMessage event e }