227 lines
11 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, $"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, $"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! playerResult = DbService.tryFindPlayer event.User.Id
match playerResult with
| Some player ->
let split = event.Id.Split("-")
let itemType = match split.[0] with "Weapon" -> ItemType.Weapon | _ -> ItemType.Shield
let itemName = split.[1]
match itemType with
| ItemType.Weapon ->
let item = getItems ItemType.Weapon |> Array.find (fun w -> w.Name = itemName)
let salePrice = item.Cost / 2.f
let updatedWeapons = player.Weapons |> Array.filter (fun (w : Weapon) -> string w <> itemName)
let updatedPlayer = { player with Bank = player.Bank + salePrice ; Weapons = updatedWeapons }
do! DbService.updatePlayer updatedPlayer
let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true
builder.Content <- $"Sold weapon {itemName} for {salePrice}! Current Balance: {updatedPlayer.Bank}"
do! event.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, builder)
|> Async.AwaitTask
| ItemType.Shield ->
let item = getItems ItemType.Shield |> Array.find (fun s -> s.Name = itemName)
let salePrice = item.Cost / 2.f
let updatedShields = player.Shields |> Array.filter (fun (s : Shield) -> string s <> itemName)
let updatedPlayer = { player with Bank = player.Bank + salePrice ; Shields = updatedShields }
do! DbService.updatePlayer updatedPlayer
let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true
builder.Content <- $"Sold shield {itemName} for {salePrice}! Current Balance: {updatedPlayer.Bank}"
do! event.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, builder)
|> Async.AwaitTask
| None ->
let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true
builder.Content <- "An error occurred and the user doesn't not exist"
do! event.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, 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 <- "OTMyMzA3NTE0ODc4NDg4NjY2.YeRFCA.3NXX4tLObXU3rVgPt8D1fE58FnY"
config.TokenType <- TokenType.Bot
config.Intents <- DiscordIntents.All
//config.MinimumLogLevel <- Microsoft.Extensions.Logging.LogLevel.Trace
let client = new DiscordClient(config)
client.add_ComponentInteractionCreated(AsyncEventHandler(Commands.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