-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
231 lines (198 loc) · 5.29 KB
/
main.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
_ "embed"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"os"
"os/signal"
"strconv"
"strings"
"time"
)
// Bot parameters
var (
GuildID string
BotToken string
AppID string
ModeratorID string
BotID string
ModerationChannelName string
misskeyToken string
misskeyHost string
isDebug bool
Session *discordgo.Session
logger *logrus.Logger
)
var moderationChannel *discordgo.Channel
//go:embed message/ja-jp.yaml
var messageJp string
func init() {
logger = logrus.New()
// Log as JSON instead of the default ASCII formatter.
//debug.SetFormatter(&debug.TextFormatter{})
logger.SetOutput(os.Stdout)
logger.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
})
}
func init() {
loadEnvironments()
if isDebug {
logger.SetLevel(logrus.DebugLevel)
} else {
logger.SetLevel(logrus.InfoLevel)
}
var err error
Session, err = discordgo.New("Bot " + BotToken)
if err != nil {
logger.WithFields(logrus.Fields{
"event": "init",
}).Error(err)
return
}
command()
moderationChannel, err = findChannelByName(Session, GuildID, ModerationChannelName)
registeredCommands := make([]*discordgo.ApplicationCommand, len(Commands))
for i, v := range Commands {
cmd, err := Session.ApplicationCommandCreate(AppID, GuildID, v)
if err != nil {
Session.Close()
logger.WithFields(logrus.Fields{
"event": "commad",
"name": v.Name,
"guild id": GuildID,
"bot token": BotToken,
}).Panic(err)
}
registeredCommands[i] = cmd
}
}
func main() {
logger.Info(":::::::::::::::::::::::")
logger.Info(":: Misskey Emoji Bot ")
logger.Info(":::::::::::::::::::::::")
logger.Info(":: initializing")
// start
Session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
logger.Info(":: Bot starting")
logger.Info(":::::::::::::::::::::::")
})
// コンポーネントはインタラクションの一部なので、InteractionCreateHandlerを登録します。
Session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if h, ok := CommandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
case discordgo.InteractionMessageComponent:
if h, ok := ComponentsHandlers[i.MessageComponentData().CustomID]; ok {
h(s, i)
}
}
})
Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
channel, _ := s.Channel(m.ChannelID)
if !strings.Contains(channel.Name, "emoji-") {
return
}
emoji, err := GetEmoji(channel.Name[6:])
if err != nil {
logger.WithFields(logrus.Fields{
"user": m.Author.Username,
"channel": channel.Name,
"message": m.Content,
}).Debug(err)
return
}
logger.WithFields(logrus.Fields{
"user": m.Author.Username,
"channel": channel.Name,
"message": m.Content,
}).Trace("Send emoji channel.")
Process(emoji, s, m)
})
Session.AddHandler(emojiModerationReaction)
_, err := Session.ApplicationCommandCreate(AppID, GuildID, &discordgo.ApplicationCommand{
Name: "buttons",
Description: "Test the buttons if you got courage",
})
if err != nil {
logger.WithFields(logrus.Fields{
"event": "Session",
}).Fatal(err)
}
err = Session.Open()
if err != nil {
logger.WithFields(logrus.Fields{
"event": "Session",
}).Fatal(err)
}
defer Session.Close()
ticker := time.NewTicker(24 * time.Hour)
go func() {
for {
select {
case <-ticker.C:
emoji := emojiReconstruction()
if len(emoji) != 0 {
noteEmojiAdded(emoji)
}
}
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
logger.Info(":: Graceful shutdown")
}
func findChannelByName(s *discordgo.Session, guildID string, name string) (*discordgo.Channel, error) {
channels, err := s.GuildChannels(guildID)
if err != nil {
return nil, err
}
for _, ch := range channels {
if ch.Name == name {
return ch, nil
}
}
return nil, fmt.Errorf("channel not found")
}
func returnFailedMessage(s *discordgo.Session, i *discordgo.InteractionCreate, reason string) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "新たな申請のRequestに失敗しました。管理者に問い合わせを行ってください。",
},
})
logger.Error(reason)
return
}
func loadEnvironments() {
err := godotenv.Load("settings.env")
if err != nil {
panic(err)
}
GuildID = os.Getenv("guild_id")
BotToken = os.Getenv("bot_token")
AppID = os.Getenv("application_id")
ModeratorID = os.Getenv("moderator_role_id")
BotID = os.Getenv("bot_role_id")
ModerationChannelName = os.Getenv("moderation_channel_name")
misskeyToken = os.Getenv("misskey_token")
misskeyHost = os.Getenv("misskey_host")
isDebug, _ = strconv.ParseBool(os.Getenv("debug"))
logger.Debug(GuildID)
logger.Debug(BotToken)
logger.Debug(AppID)
logger.Debug(BotID)
logger.Debug(ModerationChannelName)
logger.Debug(misskeyToken)
logger.Debug(misskeyHost)
logger.Debug(isDebug)
}