From 37fe9fcaba5b8bcaa05067da9525c8044a2723fa Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Mon, 3 May 2021 09:09:17 -0400 Subject: [PATCH] Cleanup utilities --- config.go | 28 +++++++++++++++++++++++----- main.go | 21 +++------------------ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/config.go b/config.go index 6775cb1..8a7151c 100644 --- a/config.go +++ b/config.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "time" + "strings" "github.com/bwmarrin/discordgo" ) @@ -14,12 +15,12 @@ func status(s *discordgo.Session) { defer log.PanicSafe() status := fmt.Sprintf("Uptime: %+v\n", time.Since(startupTime)) status += fmt.Sprintf("Last bump: %+v\n", time.Since(config.BumpTime)) - status += fmt.Sprintf("Last bumper: <@%+v>\n", userFromID(s, config.LastBumper).Username) + status += fmt.Sprintf("Last bumper: <@%+v>\n", userFromID(config.LastBumper).Username) status += fmt.Sprintf("Bump needed: %+v\n", bump) if len(config.Unverified) > 0 { status += "Unverified users:\n```" for k, v := range config.Unverified { - uvUser := userFromID(s, k) + uvUser := userFromID(k) status += fmt.Sprintf("\n%+v will be removed in %+v", uvUser.Username, time.Until(v.Add(1*time.Hour))) } status += "```" @@ -39,7 +40,7 @@ func status(s *discordgo.Session) { if len(config.Probations) > 0 { status += "\nThe following users are on probation: \n```" for uid, join := range config.Probations { - probationUser := userFromID(s, uid) + probationUser := userFromID(uid) status += fmt.Sprintf("%+v for another %+v\n", probationUser.Username, time.Until(join.Add(2*time.Hour))) } status += "```" @@ -139,8 +140,8 @@ func (v Verification) prettyPrint() string { return ret } -func userFromID(s *discordgo.Session, i string) discordgo.User { - u, err := s.GuildMember(config.GuildID, i) +func userFromID(i string) discordgo.User { + u, err := dg.GuildMember(config.GuildID, i) if err != nil { log.LogErrorType(err) return discordgo.User{} @@ -148,6 +149,23 @@ func userFromID(s *discordgo.Session, i string) discordgo.User { return *u.User } +func idFromUsername(username string) string { + userID := "" + g, err := dg.GuildMembers(config.GuildID, "", 1000) + log.LogInfo("reqPass guild is %+v.", config.GuildID) + if err == nil { + for _, m := range g { + if strings.ToUpper(m.Nick) == strings.ToUpper(username) { + userID = m.User.ID + log.LogInfo("User ID found for %+v as %+v", username, userID) + } + } + } else { + log.LogError("Unable to find user ID for %+v", username) + } + return userID +} + func adminInteraction(s *discordgo.Session, m string) { admin, _ := s.GuildMember(config.GuildID, m) counter, ok := config.Stats[admin.User.ID] diff --git a/main.go b/main.go index b4d2841..4c4e273 100644 --- a/main.go +++ b/main.go @@ -262,7 +262,7 @@ func handlePM(s *discordgo.Session, m *discordgo.MessageCreate) { s.ChannelMessageSend(m.ChannelID, "I specifically said to say \"!rules\" without quotes in the unverified channel for the rules.") } for _, uid := range config.Verifications { - user := userFromID(s, uid.UserID) + user := userFromID(uid.UserID) if m.Author.ID == user.ID { s.ChannelMessageSend(m.ChannelID, "Your verification is pending. An admin will respond to it when they are available.") s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("%+v said: %+v", m.Author.Mention(), m.Content)) @@ -306,7 +306,7 @@ func readReaction(s *discordgo.Session, m *discordgo.MessageReactionAdd) { } verification.Admin = admin.User.Username verification.Closed = time.Now() - user := userFromID(s, verification.UserID) + user := userFromID(verification.UserID) if user.ID == "" { s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("%+v, that user was not found, they might have left.", admin.Mention())) delete(config.Verifications, m.MessageID) @@ -415,22 +415,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { } } -func idFromUsername(username string) string { - userID := "" - g, err := dg.GuildMembers(config.GuildID, "", 1000) - log.LogInfo("reqPass guild is %+v.", config.GuildID) - if err == nil { - for _, m := range g { - if strings.ToUpper(m.Nick) == strings.ToUpper(username) { - userID = m.User.ID - log.LogInfo("User ID found for %+v as %+v", username, userID) - } - } - } else { - log.LogError("Unable to find user ID for %+v", username) - } - return userID -} + func findVerification(s *discordgo.Session, m *discordgo.MessageCreate) { defer log.PanicSafe()