CookieMod/bot.go

119 lines
3.0 KiB
Go
Raw Normal View History

2023-12-01 20:40:49 +01:00
package main
import (
"fmt"
"os"
2023-12-01 22:40:08 +01:00
"os/signal"
2023-12-02 04:26:02 +01:00
"strings"
2023-12-01 22:40:08 +01:00
"syscall"
2023-12-01 20:40:49 +01:00
"time"
2023-12-01 22:40:08 +01:00
"github.com/bwmarrin/discordgo"
2023-12-02 04:26:02 +01:00
badger "github.com/dgraph-io/badger/v4"
2023-12-01 20:40:49 +01:00
)
func CheckInstallationFolder(path string) {
if info, err := os.Stat(path); err != nil {
if info == nil {
start := time.Now()
log.Info(fmt.Sprintf("Creating folder \"%s\".", path))
err := os.Mkdir(path, 0777)
if err != nil {
log.Fatal(fmt.Sprintf("Couldn't create folder \"%s\".", path))
} else {
log.Info(fmt.Sprintf("Done! (%s)", time.Since(start).String()))
}
}
return
}
}
2023-12-02 04:26:02 +01:00
func Init() CookieConfig {
2023-12-01 20:40:49 +01:00
CheckInstallationFolder("db")
2023-12-02 12:13:27 +01:00
CheckInstallationFolder("backup")
2023-12-01 20:40:49 +01:00
CheckInstallationFolder("data")
2023-12-03 23:17:05 +01:00
CheckInstallationFolder("data/modules")
return ConfigCheck()
2023-12-02 04:26:02 +01:00
}
func main() {
ourConfig := Init()
2023-12-03 23:17:05 +01:00
twitchConfig = Twitch_ConfigCheck()
2023-12-02 04:26:02 +01:00
config = ourConfig
2023-12-03 23:17:05 +01:00
db, err := badger.Open(badger.DefaultOptions("db/").WithIndexCacheSize(100 << 20).WithBlockCacheSize(50 << 20))
time.Sleep(1 * time.Second)
2023-12-01 22:40:08 +01:00
if err != nil {
log.Fatal(err)
}
2023-12-02 04:26:02 +01:00
defer db.Close()
log.Info("Starting CookieMod 🍪")
session, err := discordgo.New("Bot " + config.Token)
if err != nil {
log.Fatal(err)
}
2023-12-02 12:13:27 +01:00
2023-12-03 23:17:05 +01:00
// session
2023-12-01 22:40:08 +01:00
session.AddHandler(messageCreate)
2023-12-02 04:26:02 +01:00
session.Identify.Intents = discordgo.IntentsAllWithoutPrivileged
2023-12-01 22:40:08 +01:00
err = session.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
2023-12-03 23:17:05 +01:00
go Twitch_Task(session)
2023-12-02 04:26:02 +01:00
log.Info(fmt.Sprintf("CookieMod 🍪 is running as user %s#%s. Press CTRL-C to exit.", session.State.User.Username, session.State.User.Discriminator))
2023-12-01 22:40:08 +01:00
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
log.Info("CookieMod 🍪 is stopping.")
session.Close()
}
2023-12-01 20:40:49 +01:00
2023-12-01 22:40:08 +01:00
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
2023-12-02 04:26:02 +01:00
// don't react to own messages or messages which are not from the selected guild
if m.Author.ID == s.State.User.ID || m.GuildID != config.GuildID {
2023-12-01 22:40:08 +01:00
return
}
2023-12-02 04:26:02 +01:00
2023-12-02 12:13:27 +01:00
if strings.HasPrefix(m.Content, botPrefix) {
cmd := m.Content
if strings.Contains(m.Content, " ") {
// could be a command
cmd = strings.Split(m.Content, " ")[0]
2023-12-01 22:40:08 +01:00
}
2023-12-02 12:13:27 +01:00
switch strings.TrimPrefix(cmd, botPrefix) {
2023-12-03 23:17:05 +01:00
case "uptime":
secs := int(time.Since(started).Seconds())
2023-12-04 00:16:28 +01:00
mins := secs / 60
hours := mins / 60
days := hours / 24
secs -= mins*60 + (hours * 60 * 60) + (days * 24 * 60 * 60)
2023-12-03 23:17:05 +01:00
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("I'm running for %d days, %d hours, %d minutes and %d seconds.", days, hours, mins, secs))
2023-12-02 12:13:27 +01:00
case "time":
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
hourStr := fmt.Sprint(int(hour))
minStr := fmt.Sprint(int(min))
secStr := fmt.Sprint(int(sec))
if len(hourStr) < 2 {
hourStr = "0" + hourStr
}
if len(minStr) < 2 {
minStr = "0" + minStr
}
if len(secStr) < 2 {
secStr = "0" + secStr
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Current time: %s:%s:%s.", hourStr, minStr, secStr))
case "weather":
fmt.Println(config.OpenWeatherMapAPIKey)
2023-12-01 22:40:08 +01:00
}
}
2023-12-01 20:40:49 +01:00
}