Changing to new slash commands

This commit is contained in:
Joseph Ferano 2022-01-02 02:09:31 +07:00
parent e681242e6e
commit 73b9aeaec4
5 changed files with 69 additions and 93 deletions

View File

@ -2,9 +2,6 @@
open DSharpPlus open DSharpPlus
open DSharpPlus.Entities open DSharpPlus.Entities
open DSharpPlus.SlashCommands open DSharpPlus.SlashCommands
open DSharpPlus.CommandsNext
open DSharpPlus.CommandsNext.Attributes
open Emzi0767.Utilities
type Move = type Move =
| Rock | Rock
@ -27,24 +24,21 @@ type Turn =
| BothCompleted of Move * Move | BothCompleted of Move * Move
type Match = type Match =
{ player1 : DiscordMember { player1 : DiscordUser
player2 : DiscordMember player2 : DiscordUser
round : int round : int
scorePlayer1 : int scorePlayer1 : int
scorePlayer2 : int scorePlayer2 : int
turn : Turn } turn : Turn }
type JoeBot() = type JoeBot() =
inherit BaseCommandModule () inherit ApplicationCommandModule ()
let mutable currentMatch : Match option = None static let mutable currentMatch : Match option = None
[<Command "start-match">] [<SlashCommand("match", "Start the match")>]
[<Description "Start a match between two players">] member _.StartMatch (ctx : InteractionContext, [<Option("player1", "first player")>] player1 : DiscordUser, [<Option("player2", "seceond player")>] player2 : DiscordUser) =
member this.StartMatch (ctx : CommandContext, [<Description "Player 1">] player1 : DiscordMember, [<Description "Player 2">] player2 : DiscordMember) =
async { async {
do! ctx.TriggerTypingAsync() |> Async.AwaitTask
currentMatch <- Some { currentMatch <- Some {
player1 = player1 player1 = player1
player2 = player2 player2 = player2
@ -54,24 +48,21 @@ type JoeBot() =
turn = WaitingForBoth turn = WaitingForBoth
} }
do! ctx.RespondAsync (sprintf "Match started between %s and %s. May the best hacker win!" player1.DisplayName player2.DisplayName) do! ctx.CreateResponseAsync (sprintf "Match started between %s and %s. May the best hacker win!" player1.Username player2.Username)
|> Async.AwaitTask |> Async.AwaitTask
|> Async.Ignore |> Async.Ignore
} |> Async.StartAsTask } |> Async.StartAsTask
:> Task :> Task
[<Command "send-move">] [<SlashCommand("move", "Challenge another member to combat")>]
[<Description "Challenge another member to combat">] member _.SendMove (ctx : InteractionContext, [<Option("move", "choose between rock, paper, scissors")>] moveString : string) =
member this.SendMove (ctx : CommandContext, [<Description "The name of the move: rock, paper, or scissor">] moveString : string) =
async { async {
do! ctx.TriggerTypingAsync() |> Async.AwaitTask
let move = let move =
match moveString.ToLower() with match moveString.ToLower() with
| "rock" -> Some Rock | "rock" -> Some Rock
| "paper" -> Some Paper | "paper" -> Some Paper
| "scissor" -> Some Scissor | "scissors" -> Some Scissor
| _ -> None | _ -> None
match currentMatch , move with match currentMatch , move with
@ -79,17 +70,17 @@ type JoeBot() =
let updatedTurn = let updatedTurn =
match mtc.turn with match mtc.turn with
| WaitingForBoth -> | WaitingForBoth ->
match ctx.Member with match ctx.User with
| mem when mem = mtc.player1 -> WaitingForTwo move | mem when mem = mtc.player1 -> WaitingForTwo move
| mem when mem = mtc.player2 -> WaitingForOne move | mem when mem = mtc.player2 -> WaitingForOne move
| _ -> mtc.turn | _ -> mtc.turn
| WaitingForOne p1m -> | WaitingForOne p1m ->
match ctx.Member with match ctx.User with
| mem when mem = mtc.player1 -> BothCompleted ( p1m , move ) | mem when mem = mtc.player1 -> BothCompleted ( p1m , move )
| mem when mem = mtc.player2 -> mtc.turn | mem when mem = mtc.player2 -> mtc.turn
| _ -> mtc.turn | _ -> mtc.turn
| WaitingForTwo p2m -> | WaitingForTwo p2m ->
match ctx.Member with match ctx.User with
| mem when mem = mtc.player1 -> mtc.turn | mem when mem = mtc.player1 -> mtc.turn
| mem when mem = mtc.player2 -> BothCompleted ( p2m , move ) | mem when mem = mtc.player2 -> BothCompleted ( p2m , move )
| _ -> mtc.turn | _ -> mtc.turn
@ -110,99 +101,71 @@ type JoeBot() =
match result with match result with
| P1Win | P2Win -> | P1Win | P2Win ->
let winner = match result with P1Win -> mtc.player1 | P2Win -> mtc.player2 | Draw -> mtc.player1 let winner = match result with P1Win -> mtc.player1 | P2Win -> mtc.player2 | Draw -> mtc.player1
let message = $"{p1m} beats {p2m}! {winner} takes the round!" let winningMove = match result with P1Win -> p1m | P2Win -> p2m | Draw -> p1m
do! ctx.RespondAsync message let losingMove = match result with P1Win -> p2m | P2Win -> p1m | Draw -> p1m
|> Async.AwaitTask let message = $"{winningMove} beats {losingMove}! {winner.Username} takes the round!"
|> Async.Ignore
if mtc.round >= 5 then
let winnerScore = match result with P1Win -> mtc.scorePlayer1 | P2Win -> mtc.scorePlayer2 | Draw -> 0
let loserScore = match result with P1Win -> mtc.scorePlayer2 | P2Win -> mtc.scorePlayer1 | Draw -> 0
currentMatch <- None
do! ctx.RespondAsync $"{winner} wins the match {winnerScore} to {loserScore}! Awarding a 100 genz!"
|> Async.AwaitTask
|> Async.Ignore
else
currentMatch <- Some currentMatch <- Some
{ mtc with { mtc with
round = mtc.round + 1 round = mtc.round + 1
scorePlayer1 = mtc.scorePlayer1 + (match result with P1Win -> 1 | _ -> 0) scorePlayer1 = mtc.scorePlayer1 + (match result with P1Win -> 1 | _ -> 0)
scorePlayer2 = mtc.scorePlayer2 + (match result with P2Win -> 1 | _ -> 0) scorePlayer2 = mtc.scorePlayer2 + (match result with P2Win -> 1 | _ -> 0)
turn = updatedTurn turn = WaitingForBoth
} }
| Draw -> if mtc.round >= 3 then
do! ctx.RespondAsync $"{mtc.player1.DisplayName} and {mtc.player2.DisplayName} both did {p1m}. Round was a draw! Go again!" let winnerScore = match result with P1Win -> mtc.scorePlayer1 | P2Win -> mtc.scorePlayer2 | Draw -> 0
let loserScore = match result with P1Win -> mtc.scorePlayer2 | P2Win -> mtc.scorePlayer1 | Draw -> 0
currentMatch <- None
do! ctx.CreateResponseAsync $"{winner} wins the match {winnerScore} to {loserScore}! Awarding a 100 genz!"
|> Async.AwaitTask
else
do! ctx.CreateResponseAsync message |> Async.AwaitTask
| Draw ->
currentMatch <- Some { mtc with turn = WaitingForBoth }
do! ctx.CreateResponseAsync $"{mtc.player1.Username} and {mtc.player2.Username} both did {p1m}. Round was a draw! Go again!"
|> Async.AwaitTask |> Async.AwaitTask
|> Async.Ignore
| _ -> | _ ->
if updatedTurn <> mtc.turn then if updatedTurn <> mtc.turn then
do! ctx.RespondAsync ($"Move received by {ctx.Member.DisplayName}!") currentMatch <- Some { mtc with turn = updatedTurn }
let builder = DiscordInteractionResponseBuilder()
do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder.WithContent(($"Move received by {ctx.Member.DisplayName}!")))
|> Async.AwaitTask |> Async.AwaitTask
|> Async.Ignore
else else
do! async{ return () } do! async{ return () }
| None , _ -> | None , _ ->
do! ctx.RespondAsync ("No match has been found, please use the '/start-match' command and mention the two players") do! ctx.CreateResponseAsync ("No match has been found, please use the '/start-match' command and mention the two players")
|> Async.AwaitTask |> Async.AwaitTask
|> Async.Ignore |> Async.Ignore
| _ , None -> | _ , None ->
do! ctx.RespondAsync $"Could not recognize move '{moveString}', please try again. Valid moves are 'rock', 'paper', or 'scissor'" do! ctx.CreateResponseAsync $"Could not recognize move '{moveString}', please try again. Valid moves are 'rock', 'paper', or 'scissor'"
|> Async.AwaitTask |> Async.AwaitTask
|> Async.Ignore |> Async.Ignore
} |> Async.StartAsTask } |> Async.StartAsTask
:> Task :> Task
[<Command "get-status">] [<SlashCommand("status", "Status of the match")>]
[<Description "Challenge another member to combat">] member _.Status (ctx : InteractionContext) =
member this.Status (ctx : CommandContext) =
async { async {
do! ctx.TriggerTypingAsync() |> Async.AwaitTask let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true
do! ctx.RespondAsync ("Status") do! ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder.WithContent(sprintf "%A" currentMatch))
|> Async.AwaitTask |> Async.AwaitTask
|> Async.Ignore
} |> Async.StartAsTask } |> Async.StartAsTask
:> Task :> Task
[<Command "challenge">]
[<Description "Challenge another member to combat">]
member this.Challenge (ctx : CommandContext, [<Description "Who do you want to challenge?">] user : DiscordMember) =
async {
do! ctx.TriggerTypingAsync() |> Async.AwaitTask
do! ctx.RespondAsync (sprintf "Targeting %u" user.Id)
|> Async.AwaitTask
|> Async.Ignore
} |> Async.StartAsTask
:> 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
let commandsConfig = CommandsNextConfiguration()
commandsConfig.StringPrefixes <- [ "/" ]
let client = new DiscordClient(config) let client = new DiscordClient(config)
let slash = client.UseSlashCommands() let slash = client.UseSlashCommands();
slash.RegisterCommands<JoeBot>();
slash.add_SlashCommandExecuted(AsyncEventHandler<_,_>(
fun client e ->
async {
let builder = DiscordInteractionResponseBuilder()
builder.IsEphemeral <- true
builder.Content <- "Does this need to be filled out?"
return e.Context.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, builder)
} |> Async.StartAsTask :> Task))
//let commands = client.UseCommandsNext(commandsConfig)
//commands.RegisterCommands<JoeBot>()
client.ConnectAsync () client.ConnectAsync ()
|> Async.AwaitTask |> Async.AwaitTask
@ -211,3 +174,8 @@ client.ConnectAsync ()
Task.Delay(-1) Task.Delay(-1)
|> Async.AwaitTask |> Async.AwaitTask
|> Async.RunSynchronously |> Async.RunSynchronously
client.DisconnectAsync ()
|> Async.AwaitTask
|> Async.RunSynchronously

View File

@ -8,5 +8,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="Program.fs" /> <Compile Include="Program.fs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ReferencePathWithRefAssemblies Update="\home\joe\.nuget\packages\dsharpplus.slashcommands\4.2.0-nightly-01054\lib\netstandard2.0\DSharpPlus.SlashCommands.dll" />
</ItemGroup>
<Import Project=".paket\Paket.Restore.targets" /> <Import Project=".paket\Paket.Restore.targets" />
</Project> </Project>

View File

@ -3,7 +3,9 @@ source https://api.nuget.org/v3/index.json
storage: none storage: none
framework: net6.0, netstandard2.0, netstandard2.1 framework: net6.0, netstandard2.0, netstandard2.1
nuget DSharpPlus >= 4.1.0
nuget DSharpPlus.CommandsNext >= 4.1.0
nuget DSharpPlus.SlashCommands >= 1.0.0
nuget FSharp.Core >= 6.0.0 nuget FSharp.Core >= 6.0.0
source https://nuget.emzi0767.com/api/v3/index.json
nuget DSharpPlus >= 4.2.0-nightly-01054
nuget DSharpPlus.SlashCommands >= 4.2.0-nightly-01054

View File

@ -1,8 +1,8 @@
STORAGE: NONE STORAGE: NONE
RESTRICTION: || (== net6.0) (== netstandard2.0) (== netstandard2.1) RESTRICTION: || (== net6.0) (== netstandard2.0) (== netstandard2.1)
NUGET NUGET
remote: https://api.nuget.org/v3/index.json remote: https://nuget.emzi0767.com/api/v3/index.json
DSharpPlus (4.1) DSharpPlus (4.2.0-nightly-01054)
Emzi0767.Common (>= 2.6.2) Emzi0767.Common (>= 2.6.2)
Microsoft.Extensions.Logging.Abstractions (>= 5.0) Microsoft.Extensions.Logging.Abstractions (>= 5.0)
Newtonsoft.Json (>= 13.0.1) Newtonsoft.Json (>= 13.0.1)
@ -11,11 +11,11 @@ NUGET
System.Net.WebSockets (>= 4.3) System.Net.WebSockets (>= 4.3)
System.Net.WebSockets.Client (>= 4.3.2) System.Net.WebSockets.Client (>= 4.3.2)
System.Runtime.InteropServices.RuntimeInformation (>= 4.3) System.Runtime.InteropServices.RuntimeInformation (>= 4.3)
DSharpPlus.CommandsNext (4.1) System.Threading.Channels (>= 5.0)
DSharpPlus (>= 4.1) DSharpPlus.SlashCommands (4.2.0-nightly-01054)
DSharpPlus (>= 4.2.0-nightly-01054)
Microsoft.Extensions.DependencyInjection (>= 5.0.1) Microsoft.Extensions.DependencyInjection (>= 5.0.1)
DSharpPlus.SlashCommands (1.0) remote: https://api.nuget.org/v3/index.json
DSharpPlus (>= 4.0.0-rc3)
Emzi0767.Common (2.6.2) Emzi0767.Common (2.6.2)
System.Collections.Immutable (>= 5.0) System.Collections.Immutable (>= 5.0)
System.Memory (>= 4.5.4) System.Memory (>= 4.5.4)
@ -436,6 +436,8 @@ NUGET
System.Threading (4.3) System.Threading (4.3)
System.Runtime (>= 4.3) System.Runtime (>= 4.3)
System.Threading.Tasks (>= 4.3) System.Threading.Tasks (>= 4.3)
System.Threading.Channels (6.0)
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net6.0) (>= net461)) (&& (== net6.0) (< netstandard2.1)) (== netstandard2.0) (&& (== netstandard2.1) (>= net461))
System.Threading.Tasks (4.3) System.Threading.Tasks (4.3)
Microsoft.NETCore.Platforms (>= 1.1) Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1) Microsoft.NETCore.Targets (>= 1.1)

View File

@ -1,4 +1,5 @@
FSharp.Core FSharp.Core
DSharpPlus DSharpPlus
DSharpPlus.CommandsNext // DSharpPlus.CommandsNext
// DSharpPlus.Interactivity
DSharpPlus.SlashCommands DSharpPlus.SlashCommands