discord-bot-game/Program.fs

138 lines
4.0 KiB
Forth

open System.IO
open System.Threading.Tasks
open DSharpPlus
open DSharpPlus.Entities
open DSharpPlus.SlashCommands
open Emzi0767.Utilities
open LiteDB
open LiteDB.FSharp
type HackType =
| Virus = 0
| Ransom = 1
| DDos = 2
| Worm = 3
| Crack = 4
type DefenseType =
| Firewall
| PortScan
| Encryption
| Cypher
| Hardening
[<CLIMutable>]
type Weapon = {
Id : int
Name : string
Damage : single
}
type Player = {
Id : int
Name : string
Nickname : string
DiscordId : uint16
Weapons : int array
}
type Match =
{ scorePlayer1 : int
round : int
scorePlayer2 : int }
type JoeBot() =
inherit ApplicationCommandModule ()
static let mutable currentMatch : Match option = None
[<SlashCommand("challenge", "Challenge another user")>]
member _.StartMatch (ctx : InteractionContext, [<Option("player", "Player you want to challenge")>] player : DiscordUser) =
async {
// We won't be able to find the user if they are Away or Sleeping apparently
let ( result , discordMember ) = ctx.Guild.Members.TryGetValue(player.Id)
if result then
let yes = DiscordButtonComponent(
ButtonStyle.Primary,
"first_button",
"I do")
let no = DiscordButtonComponent(
ButtonStyle.Danger,
"second_button",
"No thank you")
let builder = DiscordMessageBuilder()
let builder = builder.AddComponents(yes, no)
use img = new FileStream("challenge.jpg", FileMode.Open)
builder.WithFile(img) |> ignore
builder.Content <- $"You have been challenged by {player.Username}, do you accept?"
for channel in ctx.Guild.Channels do
if channel.Value.Name = "battle-1" then
do! channel.Value.SendMessageAsync builder
|> Async.AwaitTask
|> Async.Ignore
let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true
builder.Content <- $"Sending challenge to {player.Username}"
do! ctx.CreateResponseAsync (builder)
|> Async.AwaitTask
else
let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true
builder.Content <- $"Unable to find user in this server"
do! ctx.CreateResponseAsync (builder)
|> Async.AwaitTask
} |> Async.StartAsTask
:> Task
[<SlashCommand("hack", "Send a hack attack to the player")>]
member _.Hack (ctx : InteractionContext, [<Option("hack", "weapon attack")>] hackType : HackType) =
async {
let builder = DiscordInteractionResponseBuilder()
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder.WithContent(sprintf "%A" hackType))
|> Async.AwaitTask
} |> Async.StartAsTask
:> Task
let mapper = FSharpBsonMapper()
let db = new LiteDatabase("hacker-game.db", mapper)
let config = DiscordConfiguration()
config.Token <- "OTIyNDIyMDIyMTI1MDEwOTU1.YcBOcw.JxfW1CSIwEO7j6RbRFCnPZ-HoTk"
config.TokenType <- TokenType.Bot
config.Intents <- DiscordIntents.All
// config.MinimumLogLevel <- LogLevel.Trace
let client = new DiscordClient(config)
client.add_ComponentInteractionCreated(AsyncEventHandler(
fun client event ->
async {
return ()
} |> Async.StartAsTask
:> Task))
let slash = client.UseSlashCommands()
slash.RegisterCommands<JoeBot>();
client.ConnectAsync ()
|> Async.AwaitTask
|> Async.RunSynchronously
Task.Delay(-1)
|> Async.AwaitTask
|> Async.RunSynchronously
client.DisconnectAsync ()
|> Async.AwaitTask
|> Async.RunSynchronously