-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
136 lines (118 loc) · 3.13 KB
/
batch.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
package batch
import (
"context"
"fmt"
"os"
"time"
"github.com/bwmarrin/discordgo"
)
type PubSubMessage struct {
Data []byte `json:"data"`
}
var dg *discordgo.Session
var token, guildID, announceChannelID, guestRoleID string
var err error
func init() {
token = os.Getenv("DISCORD_TOKEN")
guildID = os.Getenv("GUILD_ID")
announceChannelID = os.Getenv("ANNOUNCE_CHANNEL_ID")
guestRoleID = os.Getenv("GUEST_ROLE_ID")
dg, err = discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
}
func MemberBatch(ctx context.Context, m PubSubMessage) error {
err := dg.Open()
if err != nil {
return err
}
defer dg.Close()
guild, err := dg.Guild(guildID)
if err != nil {
return err
}
guildMembers, err := dg.GuildMembers(guildID, "", 1000)
if err != nil {
return err
}
layout := "2006/1/2"
jst := time.FixedZone("Asia/Tokyo", 9*60*60)
year, month, day := time.Now().Date()
today := time.Date(year, month, day, 0, 0, 0, 0, jst)
for _, guildRole := range guild.Roles {
trialLimit, err := time.ParseInLocation(layout, guildRole.Name, jst)
if err != nil {
// パース出来ないロールは関係ないのでスキップ
continue
}
if trialLimit.Equal(today.AddDate(0, 0, 7)) {
roleMembers := findRoleMembers(guildMembers, guild.ID, guildRole.ID)
if err != nil {
return err
}
err = notifyMembersKickDay(roleMembers, 7)
if err != nil {
return err
}
continue
}
// 体験入部期間を過ぎていた場合
if trialLimit.Before(today) {
roleMembers := findRoleMembers(guildMembers, guild.ID, guildRole.ID)
if err != nil {
return err
}
err = removeRolesFromMembers(roleMembers, guild.ID, guildRole.ID, guestRoleID)
if err != nil {
return err
}
err = dg.GuildRoleDelete(guildID, guildRole.ID)
if err != nil {
return err
}
}
}
return nil
}
func notifyMembersKickDay(members []*discordgo.Member, day int) error {
for _, mem := range members {
mention := mem.Mention()
content := fmt.Sprintf(mention+"さんの体験入部期間はあと %d 日で終了します。", day)
_, err := dg.ChannelMessageSend(announceChannelID, content)
if err != nil {
return err
}
}
return nil
}
func removeRolesFromMembers(members []*discordgo.Member, guildID, guildRoleID, guestRoleID string) error {
for _, mem := range members {
content := mem.Mention() + " さんの体験入部期間が終了しました。"
_, err = dg.ChannelMessageSend(announceChannelID, content)
if err != nil {
return err
}
err = dg.GuildMemberRoleRemove(guildID, mem.User.ID, guestRoleID)
if err != nil {
return err
}
err = dg.GuildMemberRoleRemove(guildID, mem.User.ID, guildRoleID)
if err != nil {
return err
}
}
return nil
}
func findRoleMembers(mems []*discordgo.Member, guildID, roleID string) (members []*discordgo.Member) {
// 体験入部期間のリミット権限は最大で1人1つのため、そのメンバーにリミット権限が既にあればそれ以上調べる必要はない
for _, mem := range mems {
for _, memRoleID := range mem.Roles {
if memRoleID == roleID {
members = append(members, mem)
break
}
}
}
return members
}