pepebot/discord/main.go

129 lines
3.1 KiB
Go
Raw Normal View History

2021-05-18 12:11:30 +02:00
package main
import (
"fmt"
"log"
"os"
"os/signal"
"strconv"
2021-05-18 12:11:30 +02:00
"syscall"
2021-05-31 10:58:16 +02:00
"time"
2021-05-18 12:11:30 +02:00
"github.com/bwmarrin/discordgo"
2021-06-04 09:54:28 +02:00
"quenten.nl/pepebot/discord/mux"
2021-05-31 10:58:16 +02:00
"quenten.nl/pepebot/limiter"
2021-05-18 12:11:30 +02:00
"quenten.nl/pepebot/models/mysql"
)
/* Application struct contains the logging objects.
It also has many methods for the different functions of the bot.
These methods are mostly located in discord.go */
type application struct {
2021-05-31 10:58:16 +02:00
errorLog *log.Logger
infoLog *log.Logger
badwords *mysql.BadwordModel
adminroles *mysql.AdminRolesModel
trigger string
2021-05-18 12:11:30 +02:00
allBadWords map[string][]string
2021-05-31 10:58:16 +02:00
limiter *limiter.Limiter
2021-06-04 09:54:28 +02:00
commandMux *mux.CommandMux
2021-06-04 16:21:36 +02:00
active bool
stop bool
2021-05-18 12:11:30 +02:00
}
func main() {
dbUser := os.Getenv("DB_USER")
dbPass := os.Getenv("DB_PASS")
discordToken := os.Getenv("DISCORD_TOKEN")
rateLimit := os.Getenv("RATE_LIMIT")
timeLimit := os.Getenv("TIME_LIMIT")
2021-05-18 12:11:30 +02:00
dsn := fmt.Sprintf("%s:%s@tcp(db:3306)/badwords?parseTime=true", dbUser, dbPass)
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
db, err := openDB(dsn)
if err != nil {
errorLog.Fatal(err)
}
rateLim, err := strconv.Atoi(rateLimit)
if err != nil {
errorLog.Fatal(err)
}
timeLim, err := strconv.Atoi(timeLimit)
if err != nil {
errorLog.Fatal(err)
}
2021-05-31 10:58:16 +02:00
limiter := &limiter.Limiter{
RateLimit: rateLim,
TimeLimit: time.Duration(timeLim * int(time.Second)),
2021-05-31 11:21:30 +02:00
Logs: make(map[string][]*limiter.Action),
2021-05-31 10:58:16 +02:00
}
2021-06-04 09:54:28 +02:00
server := mux.NewCommandMux()
server.Prefix = "!pepe"
2021-06-03 12:50:42 +02:00
2021-05-18 12:11:30 +02:00
app := &application{
2021-05-31 10:58:16 +02:00
infoLog: infoLog,
errorLog: errorLog,
badwords: &mysql.BadwordModel{DB: db},
2021-05-18 12:11:30 +02:00
adminroles: &mysql.AdminRolesModel{DB: db},
2021-05-31 10:58:16 +02:00
trigger: "!pepe",
limiter: limiter,
2021-06-04 09:54:28 +02:00
commandMux: server,
2021-05-18 12:11:30 +02:00
}
app.allBadWords, err = app.badwords.AllWords()
if err != nil {
app.errorLog.Fatal(err)
}
2021-06-04 09:54:28 +02:00
server.HandleFunc("cringe", app.sendCringe)
server.HandleFunc("gif", app.sendNigelGif)
server.HandleFunc("tuesday", app.sendTuesday)
server.HandleFunc("wednesday", app.sendWednesday)
server.HandleFunc("github", app.sendGithub)
server.HandleFunc("source", app.sendGithub)
2021-06-03 17:07:33 +02:00
/* The admin commands are left out for now.
They have specialised functions and don't work yet.
Their code is left unworking and nonfunctional to be fixed
sometime in the future... sometime
Another thing left out is the bad words feature.
It goes underused and has had it's joke.
Oh and no one must be sad to see the death of the spam command...*/
2021-06-04 09:54:28 +02:00
server.HandleFunc(server.Prefix, app.sendPepe)
2021-06-03 13:45:07 +02:00
2021-05-18 12:11:30 +02:00
/* token, err := app.readAuthToken()
if err != nil {
app.errorLog.Fatal(err)
} */
discord, err := discordgo.New("Bot " + discordToken)
if err != nil {
app.errorLog.Fatal(err)
}
discord.AddHandler(app.messageCreate)
discord.Identify.Intents = discordgo.IntentsGuildMessages
err = discord.Open()
if err != nil {
app.errorLog.Fatal(err)
}
defer discord.Close()
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
discord.Close()
}