Skip to content

Commit

Permalink
Merge pull request #44 from ueckoken/disable-role-delete
Browse files Browse the repository at this point in the history
新しいusernameフォーマットに対応
  • Loading branch information
csenet authored Apr 12, 2024
2 parents 18ae02c + c7b7b3a commit d310353
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
34 changes: 22 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,26 @@ func main() {
// 無視するべきIDを除外する
func(user *discordgo.User, index int) bool { return !lo.Contains(Config.Discord.IgnoreUserIDs, user.ID) },
)
removeRoleTargets := lo.Filter(
goset.Difference(buinUsers, usersInKeycloak, func(key *discordgo.User) string { return key.ID }),
func(user *discordgo.User, index int) bool { return !lo.Contains(Config.Discord.IgnoreUserIDs, user.ID) },
)
// removeRoleTargets := lo.Filter(
// goset.Difference(buinUsers, usersInKeycloak, func(key *discordgo.User) string { return key.ID }),
// func(user *discordgo.User, index int) bool { return !lo.Contains(Config.Discord.IgnoreUserIDs, user.ID) },
// )
lo.ForEach(
addRoleTargets,
func(item *discordgo.User, _ int) {
if err := sess.GuildMemberRoleAdd(Config.Discord.GuildID, item.ID, Config.Discord.RoleID); err != nil {
logger.Error("role add failed", zap.Error(err), zap.String("username", item.Username))
}
})
lo.ForEach(
removeRoleTargets,
func(item *discordgo.User, _ int) {
if err := sess.GuildMemberRoleRemove(Config.Discord.GuildID, item.ID, Config.Discord.RoleID); err != nil {
logger.Error("role delete failed", zap.Error(err), zap.String("username", item.Username))
}
})
// lo.ForEach(
// removeRoleTargets,
// func(item *discordgo.User, _ int) {
// if err := sess.GuildMemberRoleRemove(Config.Discord.GuildID, item.ID, Config.Discord.RoleID); err != nil {
// logger.Error("role delete failed", zap.Error(err), zap.String("username", item.Username))
// }
// })
// 上手く動かないので一時的にロール削除を無効化する
removeRoleTargets := []*discordgo.User{}

logger.Info("task is over!",
zap.Stringers("role add users", addRoleTargets),
Expand Down Expand Up @@ -280,7 +282,7 @@ func ScreenName2user(logger *zap.Logger, sess *discordgo.Session, guildID string
return nil, fmt.Errorf("parse failed,err=`%w`", err)
}
users := lo.FilterMap(members, func(item *discordgo.Member, _ int) (*discordgo.User, bool) {
if item.User.Username == name && item.User.Discriminator == discriminator {
if (item.User.Username == name && item.User.Discriminator == discriminator) || (item.User.ID == name && item.User.Discriminator == "") {
return item.User, true
}
return nil, false
Expand All @@ -299,6 +301,14 @@ func ScreenName2user(logger *zap.Logger, sess *discordgo.Session, guildID string
var usernameRe = regexp.MustCompile(`(^.{2,32})#(\d{4}$)`)

func DiscordUserParse(usernameRaw string) (username, discriminator string, err error) {
// name shoud be 2-32 characters
if utf8string.NewString(usernameRaw).RuneCount() < 2 || utf8string.NewString(usernameRaw).RuneCount() > 32 {
return "", "", fmt.Errorf("username length invalid")
}
if !usernameRe.MatchString(usernameRaw) {
// for new type username
return usernameRaw, "", nil
}
parsed := usernameRe.FindStringSubmatch(usernameRaw)
switch len(parsed) {
case 0, 1, 2:
Expand Down
10 changes: 6 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ func TestDiscordUserParse(t *testing.T) {
descriminator: "1234",
wantErr: false,
},
"missmatch pattern": {
input: "onlyname",
wantErr: true,
},
"misspattern": {
input: `a`,
wantErr: true,
Expand All @@ -143,6 +139,12 @@ func TestDiscordUserParse(t *testing.T) {
assert.Equal(t, v.descriminator, d)
})
}
t.Run("new type username pattern", func(t *testing.T) {
u, d, err := DiscordUserParse("username")
assert.Empty(t, err)
assert.Equal(t, "username", u)
assert.Empty(t, d)
})
}

func TestCreateMsg(t *testing.T) {
Expand Down

0 comments on commit d310353

Please sign in to comment.