diff --git a/auth.go b/auth.go index ca8e137..6f80899 100644 --- a/auth.go +++ b/auth.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/bwmarrin/discordgo" "github.com/gorilla/mux" ) @@ -159,3 +160,29 @@ func detectUser(r *http.Request, callFunc string) (bool, string) { } return false, "" } + +func userFromID(i string) discordgo.User { + u, err := dg.GuildMember(config.GuildID, i) + if err != nil { + log.LogErrorType(err) + return 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 +} diff --git a/config.go b/config.go index 8a7151c..8ca52dd 100644 --- a/config.go +++ b/config.go @@ -3,10 +3,15 @@ package main import ( "encoding/json" "fmt" + "io" "io/ioutil" + "net/http" + "net/url" "os" - "time" + "path/filepath" + "strconv" "strings" + "time" "github.com/bwmarrin/discordgo" ) @@ -60,7 +65,31 @@ func status(s *discordgo.Session) { go runPurge(s) return } +func storeVerification(v Verification) { + defer log.PanicSafe() + fileURL, _ := url.Parse(v.Photo) + path := fileURL.Path + segments := strings.Split(path, "/") + fileName := segments[len(segments)-1] + file, _ := os.Create(fmt.Sprintf("./verifications/%s-%s-%s", v.UserID, v.Username, fileName)) + client := http.Client{ + CheckRedirect: func(r *http.Request, via []*http.Request) error { + r.URL.Opaque = r.URL.Path + return nil + }, + } + resp, err := client.Get(v.Photo) + if err != nil { + log.LogError("Unable to download verification %s-%s-%s", v.UserID, v.Username, fileName) + } + defer resp.Body.Close() + defer file.Close() + _, err = io.Copy(file, resp.Body) + if err != nil { + log.LogError("Unable to store verification %s-%s-%s", v.UserID, v.Username, fileName) + } +} func loadConfig() { var c Config confFile, _ := ioutil.ReadFile(configFile) @@ -105,6 +134,38 @@ func saveConfig() { } } +func findVerification(s *discordgo.Session, m *discordgo.MessageCreate) { + defer log.PanicSafe() + parts := strings.Split(m.Content, " ") + discordId := parts[1] + _, err := strconv.Atoi(discordId) + if err != nil { + discordId = idFromUsername(discordId) + } + user, err := s.GuildMember(config.GuildID, discordId) + if err != nil { + log.LogErrorType(err) + } + + matches, err := filepath.Glob(fmt.Sprintf("./verifications/*%+v*", discordId)) + if err != nil { + log.LogErrorType(err) + return + } + if len(matches) != 1 { + s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Error finding verification for ID %+v", discordId)) + return + } + + verificationImage, err := os.Open(matches[0]) + if err != nil { + log.LogErrorType(err) + return + } + msg := fmt.Sprintf("```%+v\nJoined: %+v\n```", user.User.Username, user.JoinedAt) + s.ChannelFileSendWithMessage(m.ChannelID, msg, fmt.Sprintf("%+v Verification", discordId), verificationImage) +} + func bumpTimer(s *discordgo.Session) { if !bump { return @@ -140,32 +201,6 @@ func (v Verification) prettyPrint() string { return ret } -func userFromID(i string) discordgo.User { - u, err := dg.GuildMember(config.GuildID, i) - if err != nil { - log.LogErrorType(err) - return 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 4c4e273..51695cd 100644 --- a/main.go +++ b/main.go @@ -3,14 +3,9 @@ package main import ( "flag" "fmt" - "io" "math/rand" - "net/http" - "net/url" "os" "os/signal" - "path/filepath" - "strconv" "strings" "syscall" "time" @@ -35,7 +30,7 @@ var ( lastPM = make(map[string]time.Time) introMsg = make(map[string]string) quotes = []string{"The hardest choices require the strongest wills.", "You're strong, but I could snap my fingers and you'd all cease to exist.", "Fun isn't something one considers when balancing the universe. But this... does put a smile on my face.", "Perfectly balanced, as all things should be.", "I am inevitable."} - version = "2.6" + version = "2.7" gitCommit string ) @@ -86,7 +81,7 @@ func main() { log.LogInfo("Thanos is now running. Press CTRL-C to exit.") go purgeTimer(dg) - if time.Since(config.BumpTime) > 2 * time.Hour { + if time.Since(config.BumpTime) > 2*time.Hour { dg.ChannelMessageSend(config.AdminChannel, "!d bump is ready") } sc := make(chan os.Signal, 1) @@ -332,31 +327,6 @@ func readReaction(s *discordgo.Session, m *discordgo.MessageReactionAdd) { log.LogInfo("%+v", verification.prettyPrint()) delete(config.Verifications, m.MessageID) } -func storeVerification(v Verification) { - defer log.PanicSafe() - fileURL, _ := url.Parse(v.Photo) - path := fileURL.Path - segments := strings.Split(path, "/") - - fileName := segments[len(segments)-1] - file, _ := os.Create(fmt.Sprintf("./verifications/%s-%s-%s", v.UserID, v.Username, fileName)) - client := http.Client{ - CheckRedirect: func(r *http.Request, via []*http.Request) error { - r.URL.Opaque = r.URL.Path - return nil - }, - } - resp, err := client.Get(v.Photo) - if err != nil { - log.LogError("Unable to download verification %s-%s-%s", v.UserID, v.Username, fileName) - } - defer resp.Body.Close() - defer file.Close() - _, err = io.Copy(file, resp.Body) - if err != nil { - log.LogError("Unable to store verification %s-%s-%s", v.UserID, v.Username, fileName) - } -} func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { defer log.PanicSafe() @@ -414,37 +384,3 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { } } } - - - -func findVerification(s *discordgo.Session, m *discordgo.MessageCreate) { - defer log.PanicSafe() - parts := strings.Split(m.Content, " ") - discordId := parts[1] - _, err := strconv.Atoi(discordId) - if err != nil { - discordId = idFromUsername(discordId) - } - user, err := s.GuildMember(config.GuildID, discordId) - if err != nil { - log.LogErrorType(err) - } - - matches, err := filepath.Glob(fmt.Sprintf("./verifications/*%+v*", discordId)) - if err != nil { - log.LogErrorType(err) - return - } - if len(matches) != 1 { - s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Error finding verification for ID %+v", discordId)) - return - } - - verificationImage, err := os.Open(matches[0]) - if err != nil { - log.LogErrorType(err) - return - } - msg := fmt.Sprintf("```%+v\nJoined: %+v\n```", user.User.Username, user.JoinedAt) - s.ChannelFileSendWithMessage(m.ChannelID, msg, fmt.Sprintf("%+v Verification", discordId), verificationImage) -}