-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
67 lines (58 loc) · 2.39 KB
/
Program.fs
File metadata and controls
67 lines (58 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
open NetCord
open NetCord.Gateway
open NetCord.Logging
open NetCord.Rest
open NetCord.Services
open NetCord.Services.ApplicationCommands
open System
open System.Threading.Tasks
[<EntryPoint>]
let main args =
task {
use client =
new GatewayClient(
BotToken(Environment.GetEnvironmentVariable "TOKEN"),
GatewayClientConfiguration(Logger = ConsoleLogger())
)
// Create the application command service
let applicationCommandService =
new ApplicationCommandService<ApplicationCommandContext>()
// Add commands from modules
applicationCommandService.AddModule typeof<OttoBot.Module>
let handler (interaction: Interaction) =
task {
// Check if the interaction is an application command interaction
match interaction with
| :? ApplicationCommandInteraction as applicationCommandInteraction ->
// Execute the command
let! result =
applicationCommandService.ExecuteAsync(
ApplicationCommandContext(applicationCommandInteraction, client)
)
// Check if the execution failed
match result with
| :? IFailResult as failResult ->
// Return the error message to the user if the execution failed
let message = $"**Error:** {failResult.Message}"
try
let! _ = interaction.SendResponseAsync(InteractionCallback.Message message)
()
with _ ->
()
| _ -> ()
| _ -> ()
}
// Add the handler to handle interactions
client.add_InteractionCreate (handler >> ValueTask)
let guildId =
match Environment.GetEnvironmentVariable "GUILD_ID" with
| null -> Nullable()
| s -> s |> Int128.Parse |> uint64 |> Nullable
// Register the commands so that you can use them in the Discord client
let! _ = applicationCommandService.RegisterCommandsAsync(client.Rest, client.Id, guildId = guildId)
do! client.StartAsync()
do! Task.Delay -1
}
|> Async.AwaitTask
|> Async.RunSynchronously
0