This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
92 lines (86 loc) · 2.43 KB
/
core.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
package main
import (
"database/sql"
"github.com/bwmarrin/discordgo"
)
type CoreUser struct {
ID string `gorm:"primaryKey"`
Locale sql.NullString
}
func (cu *CoreUser) GetLocaleOrDefault() string {
if cu.Locale.Valid {
return cu.Locale.String
}
user, err := s.User(cu.ID)
if err != nil {
return "en-US"
}
return user.Locale
}
func (cu *CoreUser) Save() {
database.Save(cu)
}
// GetUser returns the user from the database
func GetUser(userID string) *CoreUser {
cu := CoreUser{ID: userID}
database.FirstOrCreate(&cu, userID)
return &cu
}
func SetupCore() {
database.AutoMigrate(&CoreUser{})
}
var (
coreCommands = []*discordgo.ApplicationCommand{
{
Name: "locale",
Description: "Get your current locale",
},
{
Name: "setlocale",
Description: "Set your locale",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionUser,
Name: "locale",
Description: "Your preferred locale. Set nothing to reset it",
Required: false,
},
},
},
}
coreCommandHandler = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"locale": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
cu := GetUser(i.User.ID)
if cu.Locale.Valid {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Your locale is `" + cu.Locale.String + "`",
},
})
} else {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You don't have a custom locale set. Using the default `" + i.Member.User.Locale + "`",
},
})
}
},
"setlocale": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var locale string
if len(i.ApplicationCommandData().Options) == 1 || i.ApplicationCommandData().Options[0].Value != "" {
locale = i.ApplicationCommandData().Options[0].Value.(string)
}
cu := GetUser(i.User.ID)
cu.Locale.String = locale
cu.Save()
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Your locale has been set to " + locale,
},
})
},
}
)