322 lines
12 KiB
Forth
322 lines
12 KiB
Forth
open System
|
|
open System.Threading.Tasks
|
|
open DSharpPlus
|
|
open DSharpPlus.Entities
|
|
open DSharpPlus.EventArgs
|
|
open DSharpPlus.SlashCommands
|
|
open Emzi0767.Utilities
|
|
|
|
type Hack =
|
|
| Virus = 0
|
|
| Ransom = 1
|
|
| DDos = 2
|
|
| Worm = 3
|
|
| Crack = 4
|
|
| Injection = 5
|
|
|
|
type Protection =
|
|
| Firewall = 0
|
|
| PortScan = 1
|
|
| Encryption = 2
|
|
| Cypher = 3
|
|
| Hardening = 4
|
|
| Sanitation = 5
|
|
|
|
type DiscordPlayer = {
|
|
Id : uint64
|
|
Name : string
|
|
}
|
|
|
|
type Attack = {
|
|
HackType : Hack
|
|
Target : DiscordPlayer
|
|
Timestamp : DateTime
|
|
}
|
|
|
|
type Defense = {
|
|
DefenseType : Protection
|
|
Timestamp : DateTime
|
|
}
|
|
|
|
type Player = {
|
|
DiscordId : uint64
|
|
Hacks : Hack list
|
|
Protections : Protection list
|
|
Attacks : Attack list
|
|
Defenses : Defense list
|
|
Bank : int64
|
|
}
|
|
|
|
let mutable players : Player list = []
|
|
|
|
type EmptyGlobalCommandToAvoidFamousDuplicateSlashCommandsBug() = inherit ApplicationCommandModule ()
|
|
|
|
let newPlayer (membr : uint64) =
|
|
// let rand = System.Random(System.Guid.NewGuid().GetHashCode())
|
|
// let hacks =
|
|
// [0..2]
|
|
// |> Set.map (fun _ -> enum<HackType>(rand.Next(0, 6)))
|
|
// let defns =
|
|
// [0..2]
|
|
// |> Set.map (fun _ -> enum<DefenseType>(rand.Next(0, 6)))
|
|
|
|
{ DiscordId = membr
|
|
Hacks = [ Hack.Virus ; Hack.Worm ; Hack.Injection ]
|
|
Protections = [ Protection.Cypher ; Protection.Sanitation ; Protection.Firewall ]
|
|
Attacks = []
|
|
Bank = 0L
|
|
Defenses = [] }
|
|
|
|
let constructButtons (actionType : string) (playerInfo : string) (weapons : 'a list) =
|
|
weapons
|
|
|> Seq.map (fun hack ->
|
|
DiscordButtonComponent(
|
|
ButtonStyle.Primary,
|
|
$"{actionType}-{hack}-{playerInfo}",
|
|
$"{hack}"))
|
|
|
|
let notRegisteredYetMessage (ctx : InteractionContext) =
|
|
async {
|
|
let builder = DiscordInteractionResponseBuilder()
|
|
builder.Content <- $"You are not currently a hacker, first use the /redpill command to become one"
|
|
|
|
builder.AsEphemeral true |> ignore
|
|
|
|
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|
|
|> Async.AwaitTask
|
|
|
|
} |> Async.StartAsTask
|
|
:> Task
|
|
|
|
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://ferano.io"
|
|
author.IconUrl <- "https://i.kym-cdn.com/entries/icons/original/000/028/861/cover3.jpg"
|
|
builder.Author <- author
|
|
builder.Build()
|
|
|
|
type JoeBot() =
|
|
inherit ApplicationCommandModule ()
|
|
|
|
[<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
|
|
|
|
let player = players |> List.tryFind (fun p -> int64 p.DiscordId = int64 ctx.Member.Id)
|
|
players <-
|
|
match player with
|
|
| Some _ -> players
|
|
| None -> (newPlayer ctx.Member.Id)::players
|
|
|
|
if Option.isSome player then
|
|
do! ctx.CreateResponseAsync("Already registered as an elite haxxor", true)
|
|
|> Async.AwaitTask
|
|
else
|
|
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("hack", "Send a hack attack to another player")>]
|
|
member this.AttackCommand (ctx : InteractionContext, [<Option("target", "The player you want to hack")>] target : DiscordUser) =
|
|
// TODO: We need to check if the player has any active hacks going, if not they can cheat
|
|
players
|
|
|> List.tryFind (fun p -> p.DiscordId = ctx.Member.Id)
|
|
|> function
|
|
| Some player ->
|
|
let updatedAttacks = removeExpiredActions (TimeSpan.FromMinutes(15)) (fun (atk : Attack) -> atk.Timestamp) player.Attacks
|
|
players <-
|
|
players
|
|
|> List.map (fun p -> if p.DiscordId = player.DiscordId then { p with Attacks = updatedAttacks } else p)
|
|
if updatedAttacks.Length < 1 then
|
|
async {
|
|
let builder = DiscordInteractionResponseBuilder()
|
|
builder.AddEmbed (constructEmbed "Pick the hack you wish to use.") |> ignore
|
|
|
|
let targetInfo = $"{target.Id}-{target.Username}"
|
|
constructButtons "Attack" targetInfo player.Hacks
|
|
|> Seq.cast<DiscordComponent>
|
|
|> builder.AddComponents
|
|
|> ignore
|
|
|
|
builder.AsEphemeral true |> ignore
|
|
|
|
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|
|
|> Async.AwaitTask
|
|
|
|
} |> Async.StartAsTask
|
|
:> Task
|
|
else
|
|
async {
|
|
let builder = DiscordInteractionResponseBuilder()
|
|
let timeRemaining = TimeSpan.FromMinutes(15) - (DateTime.UtcNow - updatedAttacks.Head.Timestamp)
|
|
builder.Content <- $"You already hacked, please wait {timeRemaining.Minutes} minutes and {timeRemaining.Seconds} seconds to attempt another hack"
|
|
|
|
builder.AsEphemeral true |> ignore
|
|
|
|
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|
|
|> Async.AwaitTask
|
|
|
|
} |> Async.StartAsTask
|
|
:> Task
|
|
| None -> notRegisteredYetMessage ctx
|
|
|
|
|
|
[<SlashCommand("defend", "Create a passive defense that will last a certain amount of time")>]
|
|
member this.DefendCommand (ctx : InteractionContext) =
|
|
players
|
|
|> List.tryFind (fun p -> p.DiscordId = ctx.Member.Id)
|
|
|> function
|
|
| Some player ->
|
|
async {
|
|
let updatedDefenses = removeExpiredActions (TimeSpan.FromMinutes(60)) (fun (pro : Defense) -> pro.Timestamp) player.Defenses
|
|
players <-
|
|
players
|
|
|> List.map (fun p -> if p.DiscordId = player.DiscordId then { p with Defenses = updatedDefenses } else p)
|
|
let builder = DiscordInteractionResponseBuilder()
|
|
builder.AddEmbed (constructEmbed "Pick a defense to mount for a duration of time") |> ignore
|
|
|
|
constructButtons "Defense" (string player.DiscordId) player.Protections
|
|
|> Seq.cast<DiscordComponent>
|
|
|> builder.AddComponents
|
|
|> ignore
|
|
|
|
builder.AsEphemeral true |> ignore
|
|
|
|
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|
|
|> Async.AwaitTask
|
|
|
|
} |> Async.StartAsTask
|
|
:> Task
|
|
| None -> notRegisteredYetMessage ctx
|
|
|
|
let handleAttack (event : ComponentInteractionCreateEventArgs) =
|
|
async {
|
|
let split = event.Id.Split("-")
|
|
let ( resultHack , hackType ) = Enum.TryParse(typedefof<Hack>, split.[1])
|
|
let ( resultId , targetId ) = UInt64.TryParse split.[2]
|
|
match resultHack , resultId with
|
|
| true , true ->
|
|
let hackType = hackType :?> Hack
|
|
let builder = DiscordInteractionResponseBuilder()
|
|
builder.IsEphemeral <- true
|
|
builder.Content <- $"Sent {hackType} to {split.[3]}!"
|
|
do! event.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, builder)
|
|
|> Async.AwaitTask
|
|
|
|
let attack = { HackType = hackType ; Timestamp = DateTime.UtcNow ; Target = { Id = targetId ; Name = split.[3] } }
|
|
players <-
|
|
players
|
|
|> List.map (fun p -> if p.DiscordId = event.User.Id then { p with Attacks = attack::p.Attacks } else p)
|
|
|
|
let builder = DiscordMessageBuilder()
|
|
builder.WithContent($"{event.User.Username} has sent a hack to <@{targetId}>") |> ignore
|
|
let battleChannel = (event.Guild.GetChannel(927449884204867664uL))
|
|
do! battleChannel.SendMessageAsync(builder)
|
|
|> Async.AwaitTask
|
|
|> Async.Ignore
|
|
| _ ->
|
|
let builder = DiscordInteractionResponseBuilder()
|
|
builder.IsEphemeral <- true
|
|
builder.Content <- "Error parsing Button Id"
|
|
do! event.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|
|
|> Async.AwaitTask
|
|
}
|
|
|
|
let handleDefense (event : ComponentInteractionCreateEventArgs) =
|
|
async {
|
|
let split = event.Id.Split("-")
|
|
let ( resultHack , protectionType ) = Enum.TryParse(typedefof<Protection>, split.[1])
|
|
match resultHack with
|
|
| true ->
|
|
let protectionType = protectionType :?> Protection
|
|
let builder = DiscordInteractionResponseBuilder()
|
|
builder.IsEphemeral <- true
|
|
builder.Content <- $"Mounted a {protectionType} defense for 1 hour"
|
|
do! event.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, builder)
|
|
|> Async.AwaitTask
|
|
|
|
let defense = { DefenseType = protectionType ; Timestamp = DateTime.UtcNow }
|
|
players <-
|
|
players
|
|
|> List.map (fun p -> { p with Defenses = defense::p.Defenses })
|
|
|
|
let builder = DiscordMessageBuilder()
|
|
builder.WithContent($"{event.User.Username} has protected their system!") |> ignore
|
|
let battleChannel = (event.Guild.GetChannel(927449884204867664uL))
|
|
do! battleChannel.SendMessageAsync(builder)
|
|
|> Async.AwaitTask
|
|
|> Async.Ignore
|
|
| _ ->
|
|
let builder = DiscordInteractionResponseBuilder()
|
|
builder.IsEphemeral <- true
|
|
builder.Content <- "Error parsing Button Id"
|
|
do! event.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|
|
|> Async.AwaitTask
|
|
}
|
|
|
|
let handleButtonEvent (client : DiscordClient) (event : ComponentInteractionCreateEventArgs) =
|
|
async {
|
|
return! match event.Id with
|
|
| id when id.StartsWith("Attack") -> handleAttack event
|
|
| id when id.StartsWith("Defend") -> handleDefense event
|
|
| _ -> async { return () }
|
|
} |> Async.StartAsTask
|
|
:> Task
|
|
|
|
let config = DiscordConfiguration()
|
|
config.Token <- "OTIyNDIyMDIyMTI1MDEwOTU1.YcBOcw.JxfW1CSIwEO7j6RbRFCnPZ-HoTk"
|
|
config.TokenType <- TokenType.Bot
|
|
config.Intents <- DiscordIntents.All
|
|
//config.MinimumLogLevel <- Microsoft.Extensions.Logging.LogLevel.Trace
|
|
|
|
let client = new DiscordClient(config)
|
|
|
|
client.add_ComponentInteractionCreated(AsyncEventHandler(handleButtonEvent))
|
|
|
|
let slash = client.UseSlashCommands()
|
|
|
|
slash.RegisterCommands<JoeBot>(922419263275425832uL);
|
|
|
|
client.ConnectAsync ()
|
|
|> Async.AwaitTask
|
|
|> Async.RunSynchronously
|
|
|
|
Task.Delay(-1)
|
|
|> Async.AwaitTask
|
|
|> Async.RunSynchronously
|
|
|
|
client.DisconnectAsync ()
|
|
|> Async.AwaitTask
|
|
|> Async.RunSynchronously
|
|
|