More cleaning and restructuring
This commit is contained in:
27
auth.go
27
auth.go
@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -159,3 +160,29 @@ func detectUser(r *http.Request, callFunc string) (bool, string) {
|
|||||||
}
|
}
|
||||||
return false, ""
|
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
|
||||||
|
}
|
||||||
|
|||||||
89
config.go
89
config.go
@ -3,10 +3,15 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
@ -60,7 +65,31 @@ func status(s *discordgo.Session) {
|
|||||||
go runPurge(s)
|
go runPurge(s)
|
||||||
return
|
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() {
|
func loadConfig() {
|
||||||
var c Config
|
var c Config
|
||||||
confFile, _ := ioutil.ReadFile(configFile)
|
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) {
|
func bumpTimer(s *discordgo.Session) {
|
||||||
if !bump {
|
if !bump {
|
||||||
return
|
return
|
||||||
@ -140,32 +201,6 @@ func (v Verification) prettyPrint() string {
|
|||||||
return ret
|
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) {
|
func adminInteraction(s *discordgo.Session, m string) {
|
||||||
admin, _ := s.GuildMember(config.GuildID, m)
|
admin, _ := s.GuildMember(config.GuildID, m)
|
||||||
counter, ok := config.Stats[admin.User.ID]
|
counter, ok := config.Stats[admin.User.ID]
|
||||||
|
|||||||
68
main.go
68
main.go
@ -3,14 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@ -35,7 +30,7 @@ var (
|
|||||||
lastPM = make(map[string]time.Time)
|
lastPM = make(map[string]time.Time)
|
||||||
introMsg = make(map[string]string)
|
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."}
|
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
|
gitCommit string
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -86,7 +81,7 @@ func main() {
|
|||||||
|
|
||||||
log.LogInfo("Thanos is now running. Press CTRL-C to exit.")
|
log.LogInfo("Thanos is now running. Press CTRL-C to exit.")
|
||||||
go purgeTimer(dg)
|
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")
|
dg.ChannelMessageSend(config.AdminChannel, "!d bump is ready")
|
||||||
}
|
}
|
||||||
sc := make(chan os.Signal, 1)
|
sc := make(chan os.Signal, 1)
|
||||||
@ -332,31 +327,6 @@ func readReaction(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
|
|||||||
log.LogInfo("%+v", verification.prettyPrint())
|
log.LogInfo("%+v", verification.prettyPrint())
|
||||||
delete(config.Verifications, m.MessageID)
|
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) {
|
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
defer log.PanicSafe()
|
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)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user