This repository has been archived by the owner on Apr 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathargument.go
144 lines (116 loc) · 3.66 KB
/
argument.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
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
package sapphire
import (
"fmt"
"github.com/bwmarrin/discordgo"
"regexp"
"strconv"
)
// ----- Argument casting -----
// Argument represents an argument, it has methods to grab the right type.
type Argument struct {
value interface{}
provided bool
}
// The methods do not check for errors and casts rightaway, because such validations are done at argument parsing time
// And the errors are reported and the command execution is aborted, so the type will always be the type the user asked for.
// But it can panic if the usage is specifying a different type than what the user used, in that case it's their fault.
// We need to cover as much as types as possible because it isn't easy for the user to extend these.
// Returns the argument as a string.
func (arg *Argument) AsString() string {
return arg.value.(string)
}
func (arg *Argument) AsInt() int {
return arg.value.(int)
}
func (arg *Argument) AsFloat() float64 {
return arg.value.(float64)
}
// IsProvided checks if this argument is provided, for optional arguments you must use this before casting.
func (arg *Argument) IsProvided() bool {
return arg.provided
}
func (arg *Argument) AsUser() *discordgo.User {
return arg.value.(*discordgo.User)
}
func (arg *Argument) AsMember() *discordgo.Member {
return arg.value.(*discordgo.Member)
}
func (arg *Argument) AsGuild() *discordgo.Guild {
return arg.value.(*discordgo.Guild)
}
func (arg *Argument) AsRole() *discordgo.Role {
return arg.value.(*discordgo.Role)
}
func (arg *Argument) AsBool() bool {
return arg.value.(bool)
}
func (arg *Argument) AsMessage() *discordgo.Message {
return arg.value.(*discordgo.Message)
}
// ----- Argument parsing -----
// quick helper so i don't repeat provided:true
func arg(val interface{}) *Argument {
return &Argument{provided: true, value: val}
}
// The Regexp used for matching user mentions.
var MentionRegex = regexp.MustCompile("^(?:<@!?)?(\\d{17,19})>?$")
// The Regexp used for matching channel mentions.
var ChannelMentionRegex = regexp.MustCompile("^(?:<#)?(\\d{17,19})>?$")
// Parses the raw argument as specified in tag in context of ctx
func ParseArgument(ctx *CommandContext, tag *UsageTag, raw string) (*Argument, error) {
if raw == "" {
return &Argument{provided: false}, nil
}
switch tag.Type {
case "str":
fallthrough
case "string":
return arg(raw), nil
case "num":
fallthrough
case "number":
fallthrough
case "int":
val, err := strconv.Atoi(raw)
return arg(val), err
case "member":
match := MentionRegex.FindStringSubmatch(raw)
if len(match) < 2 {
return nil, fmt.Errorf("**%s** must be a valid member mention or ID.", tag.Name)
}
member := ctx.Member(match[1])
if member == nil {
return nil, fmt.Errorf("That member cannot be found in this server.")
}
return arg(member), nil
case "user":
match := MentionRegex.FindStringSubmatch(raw)
if len(match) < 2 {
return nil, fmt.Errorf("**%s** must be a valid user mention or ID.", tag.Name)
}
user, _ := ctx.FetchUser(match[1])
if user == nil {
return nil, fmt.Errorf("That user cannot be found.")
}
return arg(user), nil
case "chan":
fallthrough // Alias
case "channel":
match := ChannelMentionRegex.FindStringSubmatch(raw)
if len(match) < 2 {
return nil, fmt.Errorf("**%s** must be a valid channel mention or ID.", tag.Name)
}
channel, _ := ctx.Session.State.Channel(match[1])
if channel == nil {
return nil, fmt.Errorf("That channel cannot be found.")
}
return arg(channel), nil
case "literal":
if raw != tag.Name {
return nil, fmt.Errorf("Literal argument must be **%s**", tag.Name)
}
return arg(raw), nil
default:
return nil, fmt.Errorf("The argument type '%s' is invalid.", tag.Type)
}
}