Browse Source

Cleanup utilities

BotCommandStruct
Gregory Rudolph 4 years ago
parent
commit
37fe9fcaba
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 28
      config.go
  2. 21
      main.go

28
config.go

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"time" "time"
"strings"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -14,12 +15,12 @@ func status(s *discordgo.Session) {
defer log.PanicSafe() defer log.PanicSafe()
status := fmt.Sprintf("Uptime: %+v\n", time.Since(startupTime)) status := fmt.Sprintf("Uptime: %+v\n", time.Since(startupTime))
status += fmt.Sprintf("Last bump: %+v\n", time.Since(config.BumpTime)) 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) status += fmt.Sprintf("Bump needed: %+v\n", bump)
if len(config.Unverified) > 0 { if len(config.Unverified) > 0 {
status += "Unverified users:\n```" status += "Unverified users:\n```"
for k, v := range config.Unverified { 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 += fmt.Sprintf("\n%+v will be removed in %+v", uvUser.Username, time.Until(v.Add(1*time.Hour)))
} }
status += "```" status += "```"
@ -39,7 +40,7 @@ func status(s *discordgo.Session) {
if len(config.Probations) > 0 { if len(config.Probations) > 0 {
status += "\nThe following users are on probation: \n```" status += "\nThe following users are on probation: \n```"
for uid, join := range config.Probations { 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 += fmt.Sprintf("%+v for another %+v\n", probationUser.Username, time.Until(join.Add(2*time.Hour)))
} }
status += "```" status += "```"
@ -139,8 +140,8 @@ func (v Verification) prettyPrint() string {
return ret return ret
} }
func userFromID(s *discordgo.Session, i string) discordgo.User { func userFromID(i string) discordgo.User {
u, err := s.GuildMember(config.GuildID, i) u, err := dg.GuildMember(config.GuildID, i)
if err != nil { if err != nil {
log.LogErrorType(err) log.LogErrorType(err)
return discordgo.User{} return discordgo.User{}
@ -148,6 +149,23 @@ func userFromID(s *discordgo.Session, i string) discordgo.User {
return *u.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]

21
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.") s.ChannelMessageSend(m.ChannelID, "I specifically said to say \"!rules\" without quotes in the unverified channel for the rules.")
} }
for _, uid := range config.Verifications { for _, uid := range config.Verifications {
user := userFromID(s, uid.UserID) user := userFromID(uid.UserID)
if m.Author.ID == user.ID { 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(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)) 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.Admin = admin.User.Username
verification.Closed = time.Now() verification.Closed = time.Now()
user := userFromID(s, verification.UserID) user := userFromID(verification.UserID)
if user.ID == "" { if user.ID == "" {
s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("%+v, that user was not found, they might have left.", admin.Mention())) s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("%+v, that user was not found, they might have left.", admin.Mention()))
delete(config.Verifications, m.MessageID) 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) { func findVerification(s *discordgo.Session, m *discordgo.MessageCreate) {
defer log.PanicSafe() defer log.PanicSafe()

Loading…
Cancel
Save