Browse Source

More cleaning and restructuring

BotCommandStruct
Gregory Rudolph 4 years ago
parent
commit
f82db4166a
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 27
      auth.go
  2. 89
      config.go
  3. 66
      main.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

@ -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]

66
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
) )
@ -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)
}

Loading…
Cancel
Save