Reorganizing a little

This commit is contained in:
Joseph Ferano 2022-01-11 13:35:35 +07:00
parent d4e7170be1
commit 248e8ed64d
4 changed files with 178 additions and 154 deletions

77
Joebot.fs Normal file
View File

@ -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

View File

@ -1,157 +1,17 @@
open System module Joebot.Program
open System
open System.Threading.Tasks open System.Threading.Tasks
open DSharpPlus open DSharpPlus
open DSharpPlus.Entities open DSharpPlus.Entities
open DSharpPlus.EventArgs open DSharpPlus.EventArgs
open DSharpPlus.SlashCommands open DSharpPlus.SlashCommands
open Emzi0767.Utilities open Emzi0767.Utilities
open Joebot.Types
type ActionClass = open Joebot.Commands
| 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 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 EmptyGlobalCommandToAvoidFamousDuplicateSlashCommandsBug() = inherit ApplicationCommandModule ()
type JoeBot() = type JoeBot() =
@ -286,12 +146,6 @@ type JoeBot() =
} |> Async.StartAsTask } |> Async.StartAsTask
:> Task :> 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 handleAttack (event : ComponentInteractionCreateEventArgs) =
let updatePlayer amount attack p = { let updatePlayer amount attack p = {
@ -415,7 +269,6 @@ let handleButtonEvent (client : DiscordClient) (event : ComponentInteractionCrea
} }
} |> Async.StartAsTask } |> Async.StartAsTask
:> Task :> Task
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

90
Types.fs Normal file
View File

@ -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
}

View File

@ -6,7 +6,6 @@
<RootNamespace>discord_bot</RootNamespace> <RootNamespace>discord_bot</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Program.fs" />
<Content Include="challenge.jpg"> <Content Include="challenge.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
@ -14,5 +13,10 @@
<ItemGroup> <ItemGroup>
<ReferencePathWithRefAssemblies Update="\home\joe\.nuget\packages\dsharpplus.slashcommands\4.2.0-nightly-01054\lib\netstandard2.0\DSharpPlus.SlashCommands.dll" /> <ReferencePathWithRefAssemblies Update="\home\joe\.nuget\packages\dsharpplus.slashcommands\4.2.0-nightly-01054\lib\netstandard2.0\DSharpPlus.SlashCommands.dll" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Include="Types.fs" />
<Compile Include="Joebot.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<Import Project=".paket\Paket.Restore.targets" /> <Import Project=".paket\Paket.Restore.targets" />
</Project> </Project>