Browse Source

Restructure using structs

BotCommandStruct
Gregory Rudolph 3 years ago
parent
commit
3ab280a056
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 94
      config.go
  2. 39
      main.go
  3. 17
      types.go

94
config.go

@ -8,64 +8,12 @@ import ( @@ -8,64 +8,12 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
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(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(k)
status += fmt.Sprintf("\n%+v will be removed in %+v", uvUser.Username, time.Until(v.Add(1*time.Hour)))
}
status += "```"
} else {
status += "There are no unverified users.\n"
}
if len(config.Verifications) > 0 {
status += "Pending verifications:\n"
status += "```"
for _, v := range config.Verifications {
status += fmt.Sprintf("%+v has submitted a verification.", v.Username)
}
status += "```"
} else {
status += "There are no pending verifications."
}
if len(config.Probations) > 0 {
status += "\nThe following users are on probation: \n```"
for uid, join := range config.Probations {
probationUser := userFromID(uid)
status += fmt.Sprintf("%+v for another %+v\n", probationUser.Username, time.Until(join.Add(2*time.Hour)))
}
status += "```"
}
s.ChannelMessageSend(config.AdminChannel, status)
statistics := "```"
for k, v := range config.Stats {
adminUser, err := s.GuildMember(config.GuildID, k)
if err == nil {
statistics += fmt.Sprintf("\n%+v: %+v", adminUser.User.Username, v+1)
} else {
log.LogErrorType(err)
}
}
statistics += "\n```"
log.LogInfo("Private statistics: %+v", statistics)
go runPurge(s)
return
}
func rebootBump() {
time.Sleep(time.Until(config.BumpTime.Add(2 * time.Hour)))
dg.ChannelMessageSend(config.AdminChannel, "!d bump is ready")
@ -141,48 +89,6 @@ func saveConfig() { @@ -141,48 +89,6 @@ func saveConfig() {
}
}
func bumpSetTime(s *discordgo.Session, m *discordgo.MessageCreate) {
bump = false
parts := strings.Split(m.Content, " ")
timer, err := strconv.Atoi(parts[1])
if err != nil {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Unable to decode timer: %+v", parts[1]))
}
config.BumpTime = time.Now().Add(time.Duration(timer) * time.Minute).Add(-2 * time.Hour)
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("New bump time: %+v, expecting bump at %+v", config.BumpTime, config.BumpTime.Add(2*time.Hour)))
}
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

39
main.go

@ -32,6 +32,7 @@ var ( @@ -32,6 +32,7 @@ var (
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.7"
gitCommit string
commands []Command
)
func init() {
@ -328,6 +329,14 @@ func readReaction(s *discordgo.Session, m *discordgo.MessageReactionAdd) { @@ -328,6 +329,14 @@ func readReaction(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
defer log.PanicSafe()
var b BotCommand
if strings.HasPrefix(m.Content, s.State.User.Mention()) {
b = BotCommand{
Session: s,
Message: m,
Parts: strings.Split(m.Content, " ")[1:],
}
}
if m.Author.ID == s.State.User.ID || m.Author.Bot {
return
}
@ -363,25 +372,17 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { @@ -363,25 +372,17 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%+v please say \"!d bump\" without the quotes to bump our server :)", m.Author.Mention()))
}
if m.ChannelID == config.AdminChannel {
if strings.HasPrefix(m.Content, rebootToken) {
exit(s)
}
if strings.HasPrefix(m.Content, "!bumpset") {
bumpSetTime(s, m)
}
if strings.HasPrefix(m.Content, "!veri") {
findVerification(s, m)
}
if strings.HasPrefix(m.Content, "!quote") {
quotes = append(quotes, strings.ReplaceAll(m.Content, "!quote", ""))
}
if strings.HasPrefix(m.Content, "!snap") || strings.HasPrefix(m.Content, "!purge") {
go runPurge(s)
s.ChannelMessageSend(config.AdminChannel, quotes[rand.Intn(len(quotes))])
}
if strings.HasPrefix(m.Content, "!st") {
go status(s)
saveConfig()
if strings.HasPrefix(m.Content, s.State.User.Mention()) {
for _, cmd := range commands {
for _, keyword := range cmd.Keywords {
if strings.Contains(m.Content, keyword) {
b.Command = keyword
if !cmd.Exec(b) {
s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("There was an error running %+v\n%+v", cmd.Name, cmd.Help))
}
}
}
}
}
}
}

17
types.go

@ -3,9 +3,26 @@ package main @@ -3,9 +3,26 @@ package main
import (
"time"
"github.com/bwmarrin/discordgo"
"github.com/rudi9719/loggy"
)
// BotCommand struct used for modular commands
type BotCommand struct {
Message *discordgo.MessageCreate
Session *discordgo.Session
Parts []string
Command string
}
// Command is the type to store commands
type Command struct {
Name string
Help string
Keywords []string
Exec func(BotCommand) bool
}
// Config struct used for bot
type Config struct {
GuildID string

Loading…
Cancel
Save