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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
package main
import (
_ "embed"
"errors"
"flag"
"github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/utils"
"github.com/anhgelus/les-copaings-bot/commands"
"github.com/anhgelus/les-copaings-bot/config"
"github.com/anhgelus/les-copaings-bot/user"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"os"
"time"
)
var (
token string
//go:embed updates.json
updatesData []byte
Version = gokord.Version{
Major: 3,
Minor: 1,
Patch: 3,
}
stopPeriodicReducer chan<- interface{}
)
func init() {
err := godotenv.Load()
if err != nil && !errors.Is(err, os.ErrNotExist) {
utils.SendWarn("Error while loading .env file", "error", err.Error())
}
flag.StringVar(&token, "token", os.Getenv("TOKEN"), "token of the bot")
}
func main() {
flag.Parse()
gokord.UseRedis = false
err := gokord.SetupConfigs(&Config{}, []*gokord.ConfigInfo{})
if err != nil {
panic(err)
}
err = gokord.DB.AutoMigrate(&user.Copaing{}, &config.GuildConfig{}, &config.XpRole{}, &user.CopaingXP{})
if err != nil {
panic(err)
}
adm := gokord.AdminPermission
rankCmd := gokord.NewCommand("rank", "Affiche le niveau d'un copaing").
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionUser,
"copaing",
"Le niveau du Copaing que vous souhaitez obtenir",
)).
SetHandler(commands.Rank)
configCmd := gokord.NewCommand("config", "Modifie la config").
ContainsSub().
AddSub(
gokord.NewCommand("show", "Affiche la config").SetHandler(commands.ConfigShow),
).
AddSub(
gokord.NewCommand("xp", "Modifie l'xp").
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionString,
"type",
"Type d'action à effectuer",
).
AddChoice(gokord.NewChoice("Ajouter", "add")).
AddChoice(gokord.NewChoice("Supprimer", "del")).
AddChoice(gokord.NewChoice("Modifier", "edit")).IsRequired(),
).
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionInteger,
"level",
"Niveau du rôle",
).IsRequired()).
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionRole,
"role",
"Rôle",
).IsRequired()).
SetHandler(commands.ConfigXP),
).
AddSub(
gokord.NewCommand("disabled-channels", "Modifie les salons désactivés").
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionString,
"type",
"Type d'action à effectuer",
).
AddChoice(gokord.NewChoice("Désactiver le salon", "add")).
AddChoice(gokord.NewChoice("Activer le salon", "del")).IsRequired(),
).
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionChannel,
"channel",
"Salon à modifier",
).IsRequired()).
SetHandler(commands.ConfigChannel),
).
AddSub(
gokord.NewCommand("period-before-reduce", "Temps avant la perte d'xp (affecte aussi le /top)").
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionInteger,
"days",
"Nombre de jours avant la perte d'xp (doit être égal ou plus grand que 30)",
).IsRequired()).
SetHandler(commands.ConfigPeriodBeforeReduce),
).
AddSub(
gokord.NewCommand("fallback-channel", "Modifie le salon textuel par défaut").
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionChannel,
"channel",
"Salon textuel par défaut",
).IsRequired()).
SetHandler(commands.ConfigFallbackChannel),
).SetPermission(&adm)
topCmd := gokord.NewCommand("top", "Copaings les plus actifs").
SetHandler(commands.Top)
resetCmd := gokord.NewCommand("reset", "Reset l'xp").
SetHandler(commands.Reset).
SetPermission(&adm)
resetUserCmd := gokord.NewCommand("reset-user", "Reset l'xp d'un utilisation").
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionUser,
"user",
"Copaing a reset",
).IsRequired()).
SetHandler(commands.ResetUser).
SetPermission(&adm)
creditsCmd := gokord.NewCommand("credits", "Crédits").
SetHandler(commands.Credits)
innovations, err := gokord.LoadInnovationFromJson(updatesData)
if err != nil {
panic(err)
}
bot := gokord.Bot{
Token: token,
Status: []*gokord.Status{
{
Type: gokord.WatchStatus,
Content: "Les Copaings",
},
{
Type: gokord.GameStatus,
Content: "être dev par @anhgelus",
},
{
Type: gokord.ListeningStatus,
Content: "http 418, I'm a tea pot",
},
{
Type: gokord.GameStatus,
Content: "Les Copaings Bot " + Version.String(),
},
},
Commands: []gokord.CommandBuilder{
rankCmd,
configCmd,
topCmd,
resetCmd,
resetUserCmd,
creditsCmd,
},
AfterInit: afterInit,
Innovations: innovations,
Version: &Version,
Intents: discordgo.IntentsAllWithoutPrivileged |
discordgo.IntentsMessageContent |
discordgo.IntentGuildMembers,
}
bot.Start()
if stopPeriodicReducer != nil {
stopPeriodicReducer <- true
}
}
func afterInit(dg *discordgo.Session) {
// handlers
dg.AddHandler(OnMessage)
dg.AddHandler(OnVoiceUpdate)
dg.AddHandler(OnLeave)
stopPeriodicReducer = utils.NewTimer(24*time.Hour, func(stop chan<- interface{}) {
user.PeriodicReducer(dg)
})
}
|