This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
127 lines (105 loc) · 3.6 KB
/
command.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
package katia
import (
"errors"
"reflect"
"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
)
type Command struct {
Name string `json:"name"`
Description string `json:"-"`
Options interface{} `json:"-"`
Plugin *Plugin `json:"-"`
OnExecute func(ctx CommandContext) interface{} `json:"-"`
}
type CommandContext struct {
Bot *Bot `json:"-"`
Command *Command `json:"command"`
Interaction *discordgo.Interaction `json:"interaction"`
Data discordgo.ApplicationCommandInteractionData `json:"-"`
Logger *logrus.Entry `json:"-"`
Options interface{} `json:"options"`
}
var (
ErrCommandConflict = errors.New("명령어의 이름은 중복될 수 없습니다")
)
func (command *Command) parseOptions(fields interface{}) []*discordgo.ApplicationCommandOption {
options := []*discordgo.ApplicationCommandOption{}
values := reflect.ValueOf(fields)
types := reflect.TypeOf(fields)
for i := 0; i < values.NumField(); i++ {
field := values.Field(i)
fieldType := types.Field(i)
tag := fieldType.Tag
option := &discordgo.ApplicationCommandOption{
Name: tag.Get("name"),
Description: tag.Get("desc"),
}
if tag.Get("required") == "true" {
option.Required = true
}
switch field.Kind() {
// TODO: ApplicationCommandOptionSubCommandGroup
// TODO: ApplicationCommandOptionMentionable
case reflect.Struct:
option.Type = discordgo.ApplicationCommandOptionSubCommand
option.Options = command.parseOptions(field.Interface())
case reflect.String:
option.Type = discordgo.ApplicationCommandOptionString
case reflect.Int:
option.Type = discordgo.ApplicationCommandOptionInteger
case reflect.Bool:
option.Type = discordgo.ApplicationCommandOptionBoolean
case reflect.Ptr:
switch fieldType.Type {
case reflect.TypeOf(&discordgo.User{}):
option.Type = discordgo.ApplicationCommandOptionUser
case reflect.TypeOf(&discordgo.Channel{}):
option.Type = discordgo.ApplicationCommandOptionChannel
case reflect.TypeOf(&discordgo.Role{}):
option.Type = discordgo.ApplicationCommandOptionRole
default:
continue
}
default:
continue
}
options = append(options, option)
}
return options
}
func (ctx *CommandContext) formatOptions(fields interface{}, options []*discordgo.ApplicationCommandInteractionDataOption) interface{} {
types := reflect.TypeOf(fields)
if types.Kind() != reflect.Struct {
return nil
}
values := reflect.New(types)
for _, option := range options {
var field reflect.Value
for i := 0; i < types.NumField(); i++ {
if types.Field(i).Tag.Get("name") == option.Name {
field = values.Elem().Field(i)
break
}
}
var value interface{}
switch option.Type {
// TODO: ApplicationCommandOptionSubCommandGroup
// TODO: ApplicationCommandOptionMentionable
case discordgo.ApplicationCommandOptionSubCommand:
value = ctx.formatOptions(field.Interface(), option.Options)
case discordgo.ApplicationCommandOptionInteger,
discordgo.ApplicationCommandOptionBoolean,
discordgo.ApplicationCommandOptionString:
value = option.Value
case discordgo.ApplicationCommandOptionUser:
value = option.UserValue(ctx.Bot.Session)
case discordgo.ApplicationCommandOptionChannel:
value = option.ChannelValue(ctx.Bot.Session)
case discordgo.ApplicationCommandOptionRole:
value = option.RoleValue(ctx.Bot.Session, ctx.Interaction.GuildID)
}
field.Set(reflect.ValueOf(value))
}
return values.Elem().Interface()
}