This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
/
arguments.go
63 lines (51 loc) · 1.49 KB
/
arguments.go
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
package main
import (
"fmt"
"github.com/lus/dgc"
)
// This example shows how to use the integrated argument parser
func someCommandHandler(ctx *dgc.Ctx) {
// First of all, get the arguments object
arguments := ctx.Arguments
// Print the amount of arguments into the console
amount := arguments.Amount()
fmt.Println("Amount:", amount)
// Print the raw argument string into the console
raw := arguments.Raw()
fmt.Println("Raw:", raw)
// Parse it into a codeblock struct
codeblock := arguments.AsCodeblock()
if codeblock == nil {
// Arguments aren't a codeblock
}
fmt.Println("Codeblock Language:", codeblock.Language)
fmt.Println("Codeblock Content:", codeblock.Content)
// Get the first argument
argument := arguments.Get(0)
// Parse it into an integer
// HINT: You can also use the argument.AsInt64 method
integer, err := argument.AsInt()
if err != nil {
// Argument is no integer
}
fmt.Println("Int:", integer)
// Parse it into a boolean
boolean, err := argument.AsBool()
if err != nil {
// Argument is no bool
}
fmt.Println("Bool:", boolean)
// Parse it into a user ID if it is an user mention
// HINT: You can also use the argument.AsRoleMentionID and argument.AsChannelMentionID methods
userID := argument.AsUserMentionID()
if userID == "" {
// Argument is no user mention
}
fmt.Println("User ID:", userID)
// Parse it into a duration
dur, err := argument.AsDuration()
if err != nil {
// Argument is no duration
}
fmt.Println("Duration:", dur.String())
}