From 248e8ed64da2d7ffaa2684cf4efa07544bb717fa Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Tue, 11 Jan 2022 13:35:35 +0700 Subject: [PATCH] Reorganizing a little --- Joebot.fs | 77 ++++++++++++++++++++++ Program.fs | 159 ++------------------------------------------- Types.fs | 90 +++++++++++++++++++++++++ discord-bot.fsproj | 6 +- 4 files changed, 178 insertions(+), 154 deletions(-) create mode 100644 Joebot.fs create mode 100644 Types.fs diff --git a/Joebot.fs b/Joebot.fs new file mode 100644 index 0000000..12d55bc --- /dev/null +++ b/Joebot.fs @@ -0,0 +1,77 @@ +module Joebot.Commands + +open System +open System.Threading.Tasks +open DSharpPlus +open DSharpPlus.Entities +open DSharpPlus.EventArgs +open DSharpPlus.SlashCommands +open Emzi0767.Utilities +open Joebot.Types + +let newPlayer (membr : uint64) = + let h1 = [| Virus ; Ransom |] + let h2 = [| DDos ; Worm |] + let h3 = [| Crack ; Injection |] + let d1 = [| Firewall ; PortScan |] + let d2 = [| Encryption ; Cypher |] + let d3 = [| Hardening ; Sanitation |] + + let rand = System.Random(System.Guid.NewGuid().GetHashCode()) + let getRandom (actions : 'a array) = actions.[rand.Next(0,2)] + + let weapons = [ getRandom h1 ; getRandom h2 ; getRandom h3 ] + let shields = [ getRandom d1 ; getRandom d2 ; getRandom d3 ] + + { DiscordId = membr + Weapons = weapons + Shields = shields + Attacks = [] + Defenses = [] + Bank = 0f } + +let constructButtons (actionType : string) (playerInfo : string) (weapons : 'a list) = + weapons + |> Seq.map (fun hack -> + DiscordButtonComponent( + ButtonStyle.Primary, + $"{actionType}-{hack}-{playerInfo}", + $"{hack}")) + +let createSimpleResponseAsync msg (ctx : InteractionContext) = + async { + let builder = DiscordInteractionResponseBuilder() + builder.Content <- msg + builder.AsEphemeral true |> ignore + do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder) + |> Async.AwaitTask + } |> Async.StartAsTask + :> Task + +let notYetAHackerMsg = createSimpleResponseAsync "You are not currently a hacker, first use the /redpill command to become one" + +let removeExpiredActions timespan (timestamp : 'a -> DateTime) actions = + actions + |> List.filter (fun act -> + if DateTime.UtcNow - (timestamp act) < timespan + then true + else false) + +let constructEmbed message = + let builder = DiscordEmbedBuilder() + builder.Color <- Optional(DiscordColor.PhthaloGreen) + builder.Description <- message + let author = DiscordEmbedBuilder.EmbedAuthor() + author.Name <- "Joebot Pro" + author.Url <- "https://twitter.com/degenzgame" + author.IconUrl <- "https://pbs.twimg.com/profile_images/1473192843359309825/cqjm0VQ4_400x400.jpg" + builder.Author <- author + builder.Build() + +let calculateDamage (hack : IClass) (protection : IClass) = + let hackClass = hack.GetClass() + let protectionClass = protection.GetClass() + match hackClass , protectionClass with + | h , p when h = p -> Weak + | _ -> Strong + diff --git a/Program.fs b/Program.fs index 8d22152..2b66956 100644 --- a/Program.fs +++ b/Program.fs @@ -1,157 +1,17 @@ -open System +module Joebot.Program + +open System open System.Threading.Tasks open DSharpPlus open DSharpPlus.Entities open DSharpPlus.EventArgs open DSharpPlus.SlashCommands open Emzi0767.Utilities +open Joebot.Types +open Joebot.Commands -type ActionClass = - | Network - | Exploit - | Penetration - -type IClass = abstract GetClass : unit -> ActionClass - -type Weapon = - | Virus - | Ransom - | Worm - | DDos - | Crack - | Injection - interface IClass with - member this.GetClass () = - match this with - | Virus | Ransom -> Exploit - | DDos | Worm -> Network - | Crack | Injection -> Penetration - static member TryParse weapon = - match weapon with - | "Virus" -> Some Virus - | "Ransom" -> Some Ransom - | "Worm" -> Some Worm - | "DDos" -> Some DDos - | "Crack" -> Some Crack - | "Injection" -> Some Injection - | _ -> None - - - -type Shield = - | Firewall - | PortScan - | Encryption - | Cypher - | Hardening - | Sanitation - interface IClass with - member this.GetClass () = - match this with - | Firewall | PortScan -> Exploit - | Encryption | Cypher -> Network - | Hardening | Sanitation -> Penetration - static member TryParse shield = - match shield with - | "Firewall" -> Some Firewall - | "PortScan" -> Some PortScan - | "Encryption" -> Some Encryption - | "Cypher" -> Some Cypher - | "Hardening" -> Some Hardening - | "Sanitation" -> Some Sanitation - | _ -> None - -type HackResult = - | Strong - | Weak - - -type DiscordPlayer = { - Id : uint64 - Name : string -} - -type Attack = { - HackType : Weapon - Target : DiscordPlayer - Timestamp : DateTime -} - -type Defense = { - DefenseType : Shield - Timestamp : DateTime -} - -type Player = { - DiscordId : uint64 - Weapons : Weapon list - Shields : Shield list - Attacks : Attack list - Defenses : Defense list - Bank : single -} - let mutable players : Player list = [] -let newPlayer (membr : uint64) = - let h1 = [| Virus ; Ransom |] - let h2 = [| DDos ; Worm |] - let h3 = [| Crack ; Injection |] - let d1 = [| Firewall ; PortScan |] - let d2 = [| Encryption ; Cypher |] - let d3 = [| Hardening ; Sanitation |] - - let rand = System.Random(System.Guid.NewGuid().GetHashCode()) - let getRandom (actions : 'a array) = actions.[rand.Next(0,2)] - - let weapons = [ getRandom h1 ; getRandom h2 ; getRandom h3 ] - let shields = [ getRandom d1 ; getRandom d2 ; getRandom d3 ] - - { DiscordId = membr - Weapons = weapons - Shields = shields - Attacks = [] - Defenses = [] - Bank = 0f } - -let constructButtons (actionType : string) (playerInfo : string) (weapons : 'a list) = - weapons - |> Seq.map (fun hack -> - DiscordButtonComponent( - ButtonStyle.Primary, - $"{actionType}-{hack}-{playerInfo}", - $"{hack}")) - -let createSimpleResponseAsync msg (ctx : InteractionContext) = - async { - let builder = DiscordInteractionResponseBuilder() - builder.Content <- msg - builder.AsEphemeral true |> ignore - do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder) - |> Async.AwaitTask - } |> Async.StartAsTask - :> Task - -let notYetAHackerMsg = createSimpleResponseAsync "You are not currently a hacker, first use the /redpill command to become one" - -let removeExpiredActions timespan (timestamp : 'a -> DateTime) actions = - actions - |> List.filter (fun act -> - if DateTime.UtcNow - (timestamp act) < timespan - then true - else false) - -let constructEmbed message = - let builder = DiscordEmbedBuilder() - builder.Color <- Optional(DiscordColor.PhthaloGreen) - builder.Description <- message - let author = DiscordEmbedBuilder.EmbedAuthor() - author.Name <- "Joebot Pro" - author.Url <- "https://twitter.com/degenzgame" - author.IconUrl <- "https://pbs.twimg.com/profile_images/1473192843359309825/cqjm0VQ4_400x400.jpg" - builder.Author <- author - builder.Build() - type EmptyGlobalCommandToAvoidFamousDuplicateSlashCommandsBug() = inherit ApplicationCommandModule () type JoeBot() = @@ -286,13 +146,7 @@ type JoeBot() = } |> Async.StartAsTask :> Task -let calculateDamage (hack : IClass) (protection : IClass) = - let hackClass = hack.GetClass() - let protectionClass = protection.GetClass() - match hackClass , protectionClass with - | h , p when h = p -> Weak - | _ -> Strong - + let handleAttack (event : ComponentInteractionCreateEventArgs) = let updatePlayer amount attack p = { p with Attacks = attack::p.Attacks @@ -415,7 +269,6 @@ let handleButtonEvent (client : DiscordClient) (event : ComponentInteractionCrea } } |> Async.StartAsTask :> Task - let config = DiscordConfiguration() config.Token <- "OTIyNDIyMDIyMTI1MDEwOTU1.YcBOcw.JxfW1CSIwEO7j6RbRFCnPZ-HoTk" config.TokenType <- TokenType.Bot diff --git a/Types.fs b/Types.fs new file mode 100644 index 0000000..95e0488 --- /dev/null +++ b/Types.fs @@ -0,0 +1,90 @@ +module Joebot.Types + +open System + +type ActionClass = + | Network + | Exploit + | Penetration + +type IClass = abstract GetClass : unit -> ActionClass + +type Weapon = + | Virus + | Ransom + | Worm + | DDos + | Crack + | Injection + interface IClass with + member this.GetClass () = + match this with + | Virus | Ransom -> Exploit + | DDos | Worm -> Network + | Crack | Injection -> Penetration + static member TryParse weapon = + match weapon with + | "Virus" -> Some Virus + | "Ransom" -> Some Ransom + | "Worm" -> Some Worm + | "DDos" -> Some DDos + | "Crack" -> Some Crack + | "Injection" -> Some Injection + | _ -> None + + + +type Shield = + | Firewall + | PortScan + | Encryption + | Cypher + | Hardening + | Sanitation + interface IClass with + member this.GetClass () = + match this with + | Firewall | PortScan -> Exploit + | Encryption | Cypher -> Network + | Hardening | Sanitation -> Penetration + static member TryParse shield = + match shield with + | "Firewall" -> Some Firewall + | "PortScan" -> Some PortScan + | "Encryption" -> Some Encryption + | "Cypher" -> Some Cypher + | "Hardening" -> Some Hardening + | "Sanitation" -> Some Sanitation + | _ -> None + +type HackResult = + | Strong + | Weak + + +type DiscordPlayer = { + Id : uint64 + Name : string +} + +type Attack = { + HackType : Weapon + Target : DiscordPlayer + Timestamp : DateTime +} + +type Defense = { + DefenseType : Shield + Timestamp : DateTime +} + +type Player = { + DiscordId : uint64 + Weapons : Weapon list + Shields : Shield list + Attacks : Attack list + Defenses : Defense list + Bank : single +} + + diff --git a/discord-bot.fsproj b/discord-bot.fsproj index 76763d2..07e263b 100644 --- a/discord-bot.fsproj +++ b/discord-bot.fsproj @@ -6,7 +6,6 @@ discord_bot - PreserveNewest @@ -14,5 +13,10 @@ + + + + + \ No newline at end of file