-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
316 lines (277 loc) · 8.95 KB
/
Copy pathmain.go
File metadata and controls
316 lines (277 loc) · 8.95 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"fmt"
"log"
"os"
"os/signal"
"slices"
"strconv"
"strings"
"syscall"
"time"
"github.com/SharkBot-Dev/ClockBot/lib"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var nowDataFormat = "2006年01月02日 15時04分"
var nowDayFormat = "2006年01月02日"
var nowTimeFormat = "15時04分"
var (
session *discordgo.Session
commands = []*discordgo.ApplicationCommand{
{
Name: "help",
Description: "Botの使い方を知ります",
},
{
Name: "about",
Description: "Botの情報を取得します",
},
}
startTime time.Time
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
token := os.Getenv("DISCORD_TOKEN")
if token == "" {
log.Fatal("環境変数 DISCORD_TOKEN が設定されていません")
}
sessionManager := &lib.DiscordSessionManager{}
session = sessionManager.InitializeSession(token)
session.Identify.Intents = discordgo.IntentsGuilds
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Print("起動しました。")
go func() {
s.ApplicationCommandBulkOverwrite(s.State.Application.ID, "", commands)
log.Print("スラッシュコマンドを同期しました。")
}()
go func() {
for {
nowtime := time.Now().Format(nowDataFormat)
s.UpdateCustomStatus(nowtime)
time.Sleep(10 * time.Second)
}
}()
go func() {
time.Sleep(5 * time.Second)
for {
nowtime := time.Now().Format(nowTimeFormat)
nowday := time.Now().Format(nowDayFormat)
nowclock := time.Now().Format(nowDataFormat)
guilds := s.State.Guilds
for _, guild := range guilds {
channels := guild.Channels
var usedTypes []string
for _, channel := range channels {
var newName string
if strings.Contains(channel.Topic, "time-ch") {
if slices.Contains(usedTypes, "time-ch") {
continue
}
newName = nowtime
usedTypes = append(usedTypes, "time-ch")
} else if strings.Contains(channel.Topic, "day-ch") {
if slices.Contains(usedTypes, "day-ch") {
continue
}
newName = nowday
usedTypes = append(usedTypes, "day-ch")
} else if strings.Contains(channel.Topic, "clock-ch") {
if slices.Contains(usedTypes, "clock-ch") {
continue
}
newName = nowclock
usedTypes = append(usedTypes, "clock-ch")
}
if strings.HasSuffix(channel.Name, "#time-ch") {
if slices.Contains(usedTypes, "time-ch") {
continue
}
newName = nowtime + "#time-ch"
usedTypes = append(usedTypes, "time-ch")
} else if strings.HasSuffix(channel.Name, "#day-ch") {
if slices.Contains(usedTypes, "day-ch") {
continue
}
newName = nowday + "#day-ch"
usedTypes = append(usedTypes, "day-ch")
} else if strings.HasSuffix(channel.Name, "#clock-ch") {
if slices.Contains(usedTypes, "clock-ch") {
continue
}
newName = nowclock + "#clock-ch"
usedTypes = append(usedTypes, "clock-ch")
}
if newName != "" && channel.Name != newName {
_, err := s.ChannelEdit(channel.ID, &discordgo.ChannelEdit{Name: newName})
if err != nil {
log.Printf("チャンネル %s (%s) の編集に失敗: %v", channel.Name, channel.ID, err)
}
time.Sleep(1 * time.Second)
}
}
}
time.Sleep(30 * time.Minute)
}
}()
})
session.AddHandler(func(s *discordgo.Session, channel *discordgo.ChannelUpdate) {
nowtime := time.Now().Format(nowTimeFormat)
nowday := time.Now().Format(nowDayFormat)
nowclock := time.Now().Format(nowDataFormat)
var newName string
if strings.Contains(channel.Topic, "time-ch") {
newName = nowtime
} else if strings.Contains(channel.Topic, "day-ch") {
newName = nowday
} else if strings.Contains(channel.Topic, "clock-ch") {
newName = nowclock
}
if strings.HasSuffix(channel.Name, "#time-ch") {
newName = nowtime + "#time-ch"
} else if strings.HasSuffix(channel.Name, "#day-ch") {
newName = nowday + "#day-ch"
} else if strings.HasSuffix(channel.Name, "#clock-ch") {
newName = nowclock + "#clock-ch"
}
if newName != "" && channel.Name != newName {
_, err := s.ChannelEdit(channel.ID, &discordgo.ChannelEdit{Name: newName})
if err != nil {
log.Printf("チャンネル %s (%s) の編集に失敗: %v", channel.Name, channel.ID, err)
}
time.Sleep(1 * time.Second)
}
})
session.AddHandler(func(s *discordgo.Session, channel *discordgo.ChannelCreate) {
nowtime := time.Now().Format(nowTimeFormat)
nowday := time.Now().Format(nowDayFormat)
nowclock := time.Now().Format(nowDataFormat)
var newName string
if strings.Contains(channel.Topic, "time-ch") {
newName = nowtime
} else if strings.Contains(channel.Topic, "day-ch") {
newName = nowday
} else if strings.Contains(channel.Topic, "clock-ch") {
newName = nowclock
}
if strings.HasSuffix(channel.Name, "#time-ch") {
newName = nowtime + "#time-ch"
} else if strings.HasSuffix(channel.Name, "#day-ch") {
newName = nowday + "#day-ch"
} else if strings.HasSuffix(channel.Name, "#clock-ch") {
newName = nowclock + "#clock-ch"
}
if newName != "" && channel.Name != newName {
_, err := s.ChannelEdit(channel.ID, &discordgo.ChannelEdit{Name: newName})
if err != nil {
log.Printf("チャンネル %s (%s) の編集に失敗: %v", channel.Name, channel.ID, err)
}
time.Sleep(1 * time.Second)
}
})
session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type != discordgo.InteractionApplicationCommand {
return
}
commandName := i.ApplicationCommandData().Name
switch commandName {
case "help":
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Title: "時計Botの使い方",
Color: 16769280,
Fields: []*discordgo.MessageEmbedField{
{
Name: "基本的な時計",
Value: "時計Botのステータスに現在時刻が表示されています",
Inline: false,
},
{
Name: "チャンネル表示時計 (1)",
Value: "特定のチャンネルのチャンネル名が現在時刻に編集されます。\n以下の文字列をチャンネルトピックに含ませると編集されるようになります。\n・時刻 (`time-ch`)\n・日付 (`day-ch`)\n・日付と時刻 (`clock-ch`)",
Inline: false,
},
{
Name: "チャンネル表示時計 (2)",
Value: "特定のチャンネルもチャンネル名が現在時刻に編集されます。\n以下の文字列をチャンネル名の最後に置くと編集されるようになります。\n・時刻 (`#time-ch`)\n・日付 (`#day-ch`)\n・日付と時刻 (`#clock-ch`)",
Inline: false,
},
{
Name: "編集の周期",
Value: "編集の周期は以下になっています。\n・ステータス (10秒に一回)\n・チャンネル (30分に一回)",
Inline: false,
},
},
},
},
Flags: discordgo.MessageFlagsEphemeral,
},
})
case "about":
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Title: "時計Botの情報",
Color: 16769280,
Fields: []*discordgo.MessageEmbedField{
{
Name: "サーバー数",
Value: strconv.Itoa(len(s.State.Guilds)) + "サーバー",
Inline: false,
},
{
Name: "起動時間",
Value: strconv.Itoa(len(s.State.Guilds)) + "サーバー",
Inline: false,
},
},
},
},
Flags: discordgo.MessageFlagsEphemeral,
},
})
uptime := time.Since(startTime)
uptimeStr := fmt.Sprintf("%d時間%d分", int(uptime.Hours()), int(uptime.Minutes())%60)
s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Embeds: &[]*discordgo.MessageEmbed{
{
Title: "時計Botの情報",
Color: 16769280,
Fields: []*discordgo.MessageEmbedField{
{
Name: "サーバー数",
Value: strconv.Itoa(len(s.State.Guilds)) + " サーバー",
Inline: false,
},
{
Name: "起動時間",
Value: uptimeStr,
Inline: false,
},
},
},
},
})
}
})
if err := session.Open(); err != nil {
log.Fatalf("Discordセッションのオープンに失敗: %v", err)
}
startTime = time.Now()
defer session.Close()
log.Println("ボットが起動しました。Ctrl+Cで終了します。")
waitForExitSignal()
}
func waitForExitSignal() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}