-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtestCommands.nim
More file actions
319 lines (249 loc) · 9.07 KB
/
Copy pathtestCommands.nim
File metadata and controls
319 lines (249 loc) · 9.07 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import unittest
import asyncdispatch
import dimscmd
import strutils
import dimscord
import dimscmd/[
scanner,
common
]
import os
include token
import options
import std/exitprocs
import std/tables
#
# Test commands
#
let discord = newDiscordClient(token)
var cmd = discord.newHandler()
var latestMessage = ""
type Colour = enum
Red
Green
Blue = "bloo"
cmd.addChat("ping") do ():
## Returns pong
latestMessage = "pong"
cmd.addChatAlias("ping", ["p", "pi"])
cmd.addChat("var") do (c: Message):
latestMessage = c.content
cmd.addChat("repeat") do (word: string, count: int):
latestMessage = word.repeat(count)
cmd.addChat("sum") do (nums: seq[int]):
var total = 0
for num in nums: total += num
latestMessage = $total
cmd.addChat("isPog") do (pog: bool): # I hate myself
if pog:
latestMessage = "poggers"
else:
latestMessage = "pogn't"
cmd.addChat("sumrepeat") do (nums: seq[int], word: string):
var total = 0
for num in nums: total += num
latestMessage = word.repeat(total)
cmd.addChat("twotypes") do (nums: seq[int], words: seq[string]):
latestMessage = ""
for i in 0..<len(nums):
latestMessage &= words[i].repeat(nums[i]) & " "
cmd.addChat("chan") do (channel: Channel):
latestMessage = channel.name
cmd.addChat("colour") do (colour: Colour):
case colour:
of Red, Green:
latestMessage = $colour
of Blue:
latestMessage = $colour & " passport"
type Email = object
user, domain: string
proc next(scanner: CommandScanner, kind: typedesc[Email]): Email =
## This implements the `next` proc for Email which allows using it as a type for a command
scanner.skipWhitespace()
result.user = scanner.parseUntil('@')
if result.user == "": raiseScannerError("Invalid email, must be in format user@domain")
scanner.skipPast("@")
result.domain = scanner.parseUntil(' ')
cmd.addChat("email") do (email: Email): # Email =
latestMessage = "Ok, I'll send an email to " & email.user & " at " & email.domain
cmd.addChat("chans") do (channels: seq[Channel]):
latestMessage = ""
for channel in channels:
latestMessage &= channel.name & " "
# cmd.addChat("dice") do (sides: range[2..high(int)]): # Two sided minimum
# latestMessage = $sides # sorta random
cmd.addChat("username") do (user: User):
latestMessage = user.username
cmd.addChat("usernames") do (users: seq[User]):
latestMessage = ""
for user in users:
latestMessage &= user.username & " "
cmd.addChat("role") do (role: Role):
latestMessage = role.name
cmd.addChat("dosay") do (word: Option[string]):
if word.isSome():
latestMessage = word.get()
else:
latestMessage = "*crickets*"
cmd.addChat("roles") do (roles {.help: "test".}: seq[Role]):
latestMessage = ""
for role in roles:
latestMessage &= role.name & " "
cmd.addChat("calc sum") do (a: int, b: int):
## Adds two numbers together
latestMessage = $(a + b)
cmd.addChatAlias("calc sum", ["ca add"])
cmd.addChat("calc times") do (a: int, b: int):
latestMessage = $(a * b)
cmd.addChat("say english greeting") do ():
latestMessage = "Hello world"
cmd.addChat("say english goodbye") do ():
latestMessage = "Goodbye friends"
cmd.addChat("say irish goodbye") do ():
latestMessage = "slan"
cmd.addChat("string") do (strings: seq[string]):
check strings.len == 4
latestMessage = strings.join(" ")
discard
using xUsing: string
cmd.addChat("using") do (xUsing):
latestMessage = xUsing
# cmd.addChat("variablearray") do (nums: array[1..4, int]):
# # latestMessage = sum(nums)
# discard
cmd.addChat("nimsyntax") do (a, b, c: int, s: string):
latestMessage = s & " " & $(a + b + c)
template sendMsg(msg: string, prefix: untyped = "!!", status = true) =
var message = Message(
content: prefix & msg,
guildID: some "479193574341214208",
channelID: "1156121173176885248"
)
check cmd.handleMessage(prefix, message).await() == status
template checkLatest(msg: string) =
## Checks if the latest message against `msg` and then clears it
check latestMessage == msg
latestMessage = ""
test "Documentation on command":
check cmd.chatCommands.get(["ping"]).description == "Returns pong"
proc onReady(s: Shard, r: Ready) {.event(discord).} =
test "Basic command":
sendMsg("ping")
checkLatest "pong"
test "Different command variable":
sendMsg("var")
checkLatest "!!var"
test "Space before command":
sendMsg(" ping")
check latestMessage == "pong"
test "Multiple prefixes":
var message = Message(content: "!!ping")
check await cmd.handleMessage(@["!!", "$"], message)
check latestMessage == "pong"
message = Message(content: "$ping")
check await cmd.handleMessage(@["!!", "$"], message)
check latestMessage == "pong"
suite "Parsing parameters":
test "Simple parameters":
sendMsg("repeat hello 4")
check latestMessage == "hellohellohellohello"
test "Boolean value":
sendMsg("isPog yes")
check latestMessage == "poggers"
test "Channel mention":
sendMsg("chan <#479193574341214210>")
check latestMessage == "general"
test "Channel mentions":
sendMsg("chans <#479193574341214210> <#479193924813062152>")
check latestMessage == "general bots-playground "
test "User mention":
sendMsg("username <@!742010764302221334>")
check latestMessage == "Kayne"
test "User Mentions":
sendMsg("usernames <@!742010764302221334> <@!259999449995018240>")
check latestMessage == "Kayne intellij_gamer "
test "Role mention":
sendMsg("role <@&483606693180342272>")
check latestMessage == "Supreme Ruler"
test "Role mention":
sendMsg("roles <@&483606693180342272> <@&483606693180342272>")
check latestMessage == "Supreme Ruler Supreme Ruler "
test "Sequence of one type":
sendMsg("sum 1 2 3")
check latestMessage == "6"
test "Sequence followed by another type":
sendMsg("sumrepeat 1 2 3 hello")
check latestMessage == "hellohellohellohellohellohello"
test "Sequence of two types":
sendMsg("twotypes 2 3 hello world")
check latestMessage == "hellohello worldworldworld "
suite "Optional types":
test "Passing nothing":
sendMsg("dosay")
check latestMessage == "*crickets*"
test "Passing something":
sendMsg("dosay hello")
check latestMessage == "hello"
test "ISSUE: Invalid channel response msg is greater than 2000 characters":
# Tests that the async traceback isn't included in the message
# which causes it to go over the word limit
sendMsg("chan <#1234>", status = false)
test "Custom type parsing":
sendMsg("email test@example.com")
check latestMessage == "Ok, I'll send an email to test at example.com"
suite "Command Groups":
test "Text sub commands":
sendMsg "calc sum 6 25"
checkLatest "31"
test "Space before command group":
sendMsg(" calc sum 6 4")
checkLatest "10"
test "Space between commands":
sendMsg("calc times 9 8")
checkLatest "72"
test "Calling command that doesn't exist":
var message = Message(content: "!!calc divide 12 4", guildID: some "479193574341214208")
check not await cmd.handleMessage("!!", message)
test "Higher depth than 1":
sendMsg("say english greeting")
check latestMessage == "Hello world"
sendMsg("say irish goodbye")
check latestMessage == "slan"
test "Enums":
sendMsg("colour red")
check latestMessage == "Red"
sendMsg("colour bloo")
check latestMessage == "bloo passport"
# test "Ranges":
# sendMsg("dice 6")
# check latestMessage == "6"
test "Simple nim syntax parameters":
sendMsg("nimsyntax 1 2 3 hello")
check latestMessage == "hello 6"
suite "Alias":
test "Single word command alias":
sendMsg "p"
checkLatest "pong"
sendMsg "pi"
checkLatest "pong"
test "Sub command aliasing":
sendMsg "calc sum 5 6"
check latestMessage == "11"
latestMessage = ""
sendMsg "ca add 5 6"
check latestMessage == "11"
test "Using":
sendMsg("using stuff")
check latestMessage == "stuff"
# suite "Arrays":
# test "Basic array":
# sendMsg("array i am bob hello world")
# check latestMessage == "i am boob hello"
#
# test "Variable array":
# sendMsg("variablearray 1 2")
# check latestMessage == "3"
# sendMsg("variablearray 1 2 3")
# check latestMessage == "6"
quit getProgramResult()
waitFor discord.startSession()