-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandHandling.fs
112 lines (95 loc) · 4.56 KB
/
CommandHandling.fs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
module CommandHandling
open Discord
open Discord.Commands
open Discord.WebSocket
open System
open System.Reflection
open System.Threading.Tasks
let log (msg: LogMessage) =
// Return an error message for async commands
match msg.Exception with
| :? CommandException as command ->
// Don't risk blocking the logging task by awaiting a message send rate limits!?
command.Context.Channel.SendMessageAsync($"Error: {command.Message}")
|> ignore
| _ -> ()
printfn "%s" <| msg.ToString()
Task.CompletedTask
let messageReceived (rawMessage: SocketMessage) : Task =
task {
match rawMessage with
| :? SocketUserMessage as message ->
if (message.Source = MessageSource.User) then
let mutable argPos = 0
let isCommand =
message.HasMentionPrefix(Services.discord.CurrentUser, &argPos)
let context =
SocketCommandContext(Services.discord, message)
Database.updateActiveDates context.Guild.Id message.Author.Id
if not isCommand then
match Database.getNewMilestoneNames context.Guild.Id message.Author.Id with
| [] -> ()
| milestoneNames ->
let milestoneChannel =
Database.getMilestoneChannel context.Guild.Id
|> Option.defaultValue 0UL
for sobrietyType, milestoneName in milestoneNames do
let sobrietyTypeMessage =
match sobrietyType with
| "" -> ""
| sobrietyType -> sprintf " for %s" sobrietyType
let milestoneMessage =
sprintf
"<@%d> has reached a new milestone%s: **%s**"
message.Author.Id
sobrietyTypeMessage
milestoneName
if (milestoneChannel > 0UL) then
do!
context
.Guild
.GetTextChannel(milestoneChannel)
.SendMessageAsync(milestoneMessage)
:> Task
else
do!
(context.Channel :?> SocketTextChannel)
.SendMessageAsync(milestoneMessage)
:> Task
else
match Database.getBanMessage context.Guild.Id message.Author.Id with
| Some banMessage ->
do!
(context.Channel :?> SocketTextChannel)
.SendMessageAsync($"<@{message.Author.Id}> {banMessage}")
:> Task
| None ->
Database.pruneInactiveUsers context.Guild.Id
|> ignore
while (message.Content.[argPos] = ' ') do
argPos <- argPos + 1
let! result = Services.commands.ExecuteAsync(context, argPos, Services.provider)
if result.Error = Nullable(CommandError.UnknownCommand) then
do!
(context.Channel :?> SocketTextChannel)
.SendMessageAsync(
text =
sprintf
"Unknown command: %s"
(message.Content.Substring(message.Content.IndexOf('>') + 2))
)
:> Task
elif result.Error.HasValue then
do!
(context.Channel :?> SocketTextChannel)
.SendMessageAsync(result.ToString())
:> Task
| _ -> ()
}
let initialize provider =
task {
do! Services.commands.AddModulesAsync(Assembly.GetEntryAssembly(), provider) :> Task
Func<_, _>(messageReceived)
|> Services.discord.add_MessageReceived
Func<_, _>(log) |> Services.commands.add_Log
}