Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/dimscmd.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,14 @@ proc addInteractionParameterParseCode(prc: NimNode, name: string, parameters: se

for parameter in parameters:
let ident = parameter.name.ident()
var kind = parameter.kind.ident()
if parameter.sequence:
kind = nnkBracketExpr.newTree("seq".ident(), kind)

var procCall = nnkCall.newTree(
"get".bindSym(brOpen),
scannerIdent,
parameter.kind.ident(),
kind,
parameter.name.newStrLitNode()
)
if parameter.future:
Expand Down
9 changes: 3 additions & 6 deletions src/dimscmd/scanner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ type
template raiseScannerError*(msg: string) =
## Raises a scanner error with the msg parameter being the `message` attribute attached to the exception.
## This is so that the async stack trace is not added to the exception message
var err: ref ScannerError
new err
err.message = msg
raise err
raise (ref ScannerError)(message: msg)

macro scanProc*(prc: untyped): untyped =
## Adds in a type parameter to a proc to get a basic return type overloading
Expand Down Expand Up @@ -162,15 +159,15 @@ proc next*[T: enum](scanner: CommandScanner, kind: typedesc[T]): T =

proc next*[T: range](scanner: CommandScanner, kind: typedesc[T]): T =
## Gets the next int value in a range
const
const
max = high T
min = low T
let value = scanner.next(int)
if value in min..max:
result = value
else:
raiseScannerError(fmt"value is not in range {min}..{max}")

proc next*(scanner: CommandScanner): string {.scanProc.}=
## Returns the next word that appears in the command scanner
scanner.skipWhitespace()
Expand Down
18 changes: 10 additions & 8 deletions tests/testCommands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ cmd.addChat("nimsyntax") do (a, b, c: int, s: string):
latestMessage = s & " " & $(a + b + c)


template sendMsg(msg: string, prefix: untyped = "!!") =
var message = Message(content: prefix & msg, guildID: some "479193574341214208")
check await cmd.handleMessage(prefix, message)
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
Expand Down Expand Up @@ -237,11 +241,9 @@ proc onReady(s: Shard, r: Ready) {.event(discord).} =
check latestMessage == "hello"

test "ISSUE: Invalid channel response msg is greater than 2000 characters":
# Somehow the error message for this is 2257 characters long
# It shouldn't be anywhere near that
# Resolved, turns out async adds the stacktrace to the msg
expect RestError, DiscordHttpError: # Don't expect an Assert error
sendMsg("chan <#1234>")
# 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")
Expand Down
10 changes: 10 additions & 0 deletions tests/testSlash.nim
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ cmd.addSlash("cases") do (camelCase: int, snakeCase: int, PascalCase: int):

cmd.addSlashAlias("calc add", ["calc plus", "calc addition"])

# Stub so the `someSeq` test compiles
proc get(scanner: InteractionScanner, kind: typedesc[seq[int]], key: string): Option[seq[int]] =
discard

cmd.addSlash("someSeq") do (test: seq[int]):
## Checks that `seq[T]` is correctly a `seq`.
## This check happens at compile time
static:
assert typeof(test) is seq[int], $typeof(test)

proc newParam[T](name: string, val: T, kind: ApplicationCommandOptionType): JsonNode =
result = %* {"name": name, "value": %val, "kind": kind.ord}

Expand Down