199 lines
8.6 KiB
Forth

open System
open System.Threading.Tasks
open DSharpPlus.Entities
open DSharpPlus
open DSharpPlus.EventArgs
open DSharpPlus.SlashCommands
open DegenzGame
open DegenzGame.Shared
open Emzi0767.Utilities
open Newtonsoft.Json
let store =
try
let file = System.IO.File.ReadAllText("Items.json")
JsonConvert.DeserializeObject<Item array>(file)
|> Array.groupBy (fun (i : Item) -> i.ItemType)
with _ -> [||]
let storeListing =
store
|> Array.map (fun ( itemType , items ) ->
let itemList = items |> Array.map (fun i -> $"{i.Name} - {i.Cost} GBT") |> String.concat "\n"
(constructEmbed $"{itemType}:\n{itemList}"))
module Commands =
let viewStore (ctx : InteractionContext) =
async {
let builder = DiscordInteractionResponseBuilder()
builder.AddEmbeds(storeListing)
.AsEphemeral(true)
|> ignore
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|> Async.AwaitTask
} |> Async.StartAsTask
:> Task
let getItems itemType = store |> Array.find (fun ( t , _ ) -> t = itemType) |> snd
let buyHack (ctx : InteractionContext) hackId =
async {
let! playerResult = DbService.tryFindPlayer ctx.Member.Id
let weapons = getItems ItemType.Weapon
let weaponResult = weapons |> Array.tryFind (fun w -> w.Name = string hackId)
return!
match playerResult , weaponResult with
| Some player , Some item ->
async {
let newBalance = player.Bank - item.Cost
if newBalance >= 0.0f then
let playerHasItem = player.Weapons |> Array.exists (fun w -> item.Name = string w)
if not playerHasItem then
let weapon = weaponInventory |> Array.find (fun w -> item.Name = string w)
let p = { player with Bank = newBalance ; Weapons = Array.append [| weapon |] player.Weapons }
do! DbService.updatePlayer p
do! createSimpleResponseAsync $"Successfully purchased {item.Name}! You now have {newBalance} remaining" ctx
else
do! createSimpleResponseAsync $"You already own this item!" ctx
else
do! createSimpleResponseAsync $"You do not have sufficient funds to buy this item! Current balance: {player.Bank} GBT" ctx
}
| None , _ -> notYetAHackerMsg ctx
| _ -> createSimpleResponseAsync "Something is wrong" ctx
} |> Async.StartAsTask
:> Task
let buyShield (ctx : InteractionContext) shieldId =
async {
let! playerResult = DbService.tryFindPlayer ctx.Member.Id
let shieldResult =
getItems ItemType.Shield
|> Array.tryFind (fun w -> w.Name = string shieldId)
return!
match playerResult , shieldResult with
| Some player , Some item ->
async {
let newBalance = player.Bank - item.Cost
if newBalance >= 0.0f then
let playerHasItem = player.Shields |> Array.exists (fun w -> item.Name = string w)
if not playerHasItem then
let shield = shieldInventory |> Array.find (fun w -> item.Name = string w)
let p = { player with Bank = newBalance ; Shields = Array.append [| shield |] player.Shields }
do! DbService.updatePlayer p
do! createSimpleResponseAsync $"Successfully purchased {item.Name}! You now have {newBalance} remaining" ctx
else
do! createSimpleResponseAsync $"You already own this item!" ctx
else
do! createSimpleResponseAsync $"You do not have sufficient funds to buy this item! Current balance: {player.Bank} GBT" ctx
}
| None , _ -> notYetAHackerMsg ctx
| _ -> createSimpleResponseAsync "Something is wrong" ctx
} |> Async.StartAsTask
:> Task
let constructItemButtons playerInfo itemType (items : 'a array) =
items
|> Seq.map (fun item -> DiscordButtonComponent(ButtonStyle.Primary, $"{playerInfo}-{itemType}-{item}", $"{item}"))
let sellItem (ctx : InteractionContext) =
async {
let! playerResult = DbService.tryFindPlayer ctx.Member.Id
match playerResult with
| Some player ->
let hasInventoryToSell = Array.length player.Weapons + Array.length player.Shields > 0
if hasInventoryToSell then
let builder = DiscordInteractionResponseBuilder()
builder.AddEmbed (constructEmbed "Pick the item you wish to sell.") |> ignore
Array.chunkBySize 3 player.Weapons
|> Array.iter
(fun wps ->
wps
|> Array.map (fun w ->
DiscordButtonComponent(ButtonStyle.Primary, $"{player.DiscordId}-Weapon-{w}", $"{w}"))
|> Seq.cast<DiscordComponent>
|> builder.AddComponents
|> ignore)
Array.chunkBySize 3 player.Shields
|> Array.iter
(fun shs ->
shs
|> Array.map (fun s ->
DiscordButtonComponent(ButtonStyle.Primary, $"{player.DiscordId}-Shield-{s}", $"{s}"))
|> Seq.cast<DiscordComponent>
|> builder.AddComponents
|> ignore)
builder.AsEphemeral true |> ignore
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|> Async.AwaitTask
else
do! createSimpleResponseAsync "You currently have no inventory to sell" ctx
| None -> do! notYetAHackerMsg ctx
return ()
} |> Async.StartAsTask
:> Task
let handleSellButtonEvents (_ : DiscordClient) (event : ComponentInteractionCreateEventArgs) =
async {
let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true
builder.Content <- $"{event.Id}"
do! event.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder)
|> Async.AwaitTask
} |> Async.StartAsTask
:> Task
type EmptyGlobalCommandToAvoidFamousDuplicateSlashCommandsBug() = inherit ApplicationCommandModule ()
type Store() =
inherit ApplicationCommandModule ()
[<SlashCommand("store", "View items available for purchase")>]
member _.ViewStore (ctx : InteractionContext) = Commands.viewStore ctx
[<SlashCommand("buy-hack", "Purchase a hack attack you can use to earn GoodBoyTokenz")>]
member _.BuyHack (ctx : InteractionContext, [<Option("hack-id", "The ID of the hack you wish to purchase")>] hackId : Weapon) =
Commands.buyHack ctx hackId
[<SlashCommand("buy-shield", "Purchase a hack shield so you can protect your GoodBoyTokenz")>]
member this.BuyShield (ctx : InteractionContext, [<Option("shield-id", "The ID of the shield you wish to purchase")>] shieldId : Shield) =
Commands.buyShield ctx shieldId
[<SlashCommand("sell", "Sell an item in your inventory for GoodBoyTokenz")>]
member this.SellItem (ctx : InteractionContext) =
Commands.sellItem ctx
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(handleSellButtonEvents))
let slash = client.UseSlashCommands()
// My server
slash.RegisterCommands<Store>(922419263275425832uL);
// Degenz
//slash.RegisterCommands<HackerGame>(922414052708327494uL);
client.ConnectAsync ()
|> Async.AwaitTask
|> Async.RunSynchronously
Task.Delay(-1)
|> Async.AwaitTask
|> Async.RunSynchronously
client.DisconnectAsync ()
|> Async.AwaitTask
|> Async.RunSynchronously