2020-04-11 14:16:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"github.com/foxtrot/scuzzy/features"
|
|
|
|
"github.com/foxtrot/scuzzy/models"
|
2020-05-25 22:46:15 +00:00
|
|
|
"github.com/foxtrot/scuzzy/permissions"
|
2020-04-11 14:16:09 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discord.go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Core Bot Properties
|
|
|
|
var (
|
|
|
|
Token string
|
|
|
|
ConfigPath string
|
|
|
|
Config models.Configuration
|
|
|
|
)
|
|
|
|
|
|
|
|
func getConfig() error {
|
|
|
|
cf, err := ioutil.ReadFile(ConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(cf, &Config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Parse and Check Flags
|
|
|
|
flag.StringVar(&Token, "t", "", "Bot Token")
|
|
|
|
flag.StringVar(&ConfigPath, "c", "", "Config Path")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if len(Token) == 0 {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal("[!] Error: No Auth Token supplied.")
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
|
|
|
if len(ConfigPath) == 0 {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal("[!] Error: No Config Path supplied.")
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get Config
|
|
|
|
err := getConfig()
|
|
|
|
if err != nil {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal("[!] Error: " + err.Error())
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
2020-05-25 21:58:38 +00:00
|
|
|
Config.ConfigPath = ConfigPath
|
2020-04-11 14:16:09 +00:00
|
|
|
|
|
|
|
// Instantiate Bot
|
|
|
|
bot, err := discordgo.New("Bot " + Token)
|
|
|
|
if err != nil {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal("[!] Error: " + err.Error())
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 09:12:24 +00:00
|
|
|
// Enable Message Caching (Last 1024 Events)
|
|
|
|
bot.State.MaxMessageCount = 1024
|
|
|
|
|
2020-04-11 14:16:09 +00:00
|
|
|
// Open Connection
|
|
|
|
err = bot.Open()
|
|
|
|
if err != nil {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal("[!] Error: " + err.Error())
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Auth
|
|
|
|
Config.Guild, err = bot.Guild(Config.GuildID)
|
|
|
|
if err != nil {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal("[!] Error: " + err.Error())
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
2020-05-25 22:46:15 +00:00
|
|
|
var p *permissions.Permissions
|
|
|
|
p = permissions.New(&Config, Config.Guild)
|
2020-04-11 14:16:09 +00:00
|
|
|
|
|
|
|
// Setup Handlers
|
|
|
|
f := features.Features{
|
2020-05-25 22:46:15 +00:00
|
|
|
Token: Token,
|
|
|
|
Permissions: p,
|
|
|
|
Config: Config,
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register Handlers
|
2020-05-04 22:03:11 +00:00
|
|
|
f.RegisterHandlers()
|
2020-05-25 09:12:24 +00:00
|
|
|
bot.AddHandler(f.ProcessMessage)
|
2020-04-11 14:16:09 +00:00
|
|
|
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Printf("[*] Bot Running.\n")
|
2020-04-11 14:16:09 +00:00
|
|
|
|
|
|
|
// Set Bot Status
|
|
|
|
go func() {
|
|
|
|
usd := discordgo.UpdateStatusData{
|
|
|
|
IdleSince: nil,
|
|
|
|
Game: &discordgo.Game{
|
|
|
|
Name: Config.StatusText,
|
|
|
|
Type: 0,
|
|
|
|
URL: "",
|
|
|
|
Details: "",
|
|
|
|
State: "",
|
|
|
|
TimeStamps: discordgo.TimeStamps{},
|
|
|
|
Assets: discordgo.Assets{},
|
|
|
|
ApplicationID: "",
|
|
|
|
Instance: -1,
|
|
|
|
},
|
|
|
|
AFK: false,
|
|
|
|
Status: "online",
|
|
|
|
}
|
|
|
|
err = bot.UpdateStatusComplex(usd)
|
|
|
|
if err != nil {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal("[!] Error: " + err.Error())
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For some reason the bot's status will regularly disappear...
|
2020-05-25 22:46:15 +00:00
|
|
|
for range time.Tick(10 * time.Minute) {
|
2020-04-11 14:16:09 +00:00
|
|
|
err := bot.UpdateStatusComplex(usd)
|
|
|
|
if err != nil {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal("[!] Error: " + err.Error())
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Catch SIGINT
|
|
|
|
sc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGKILL)
|
|
|
|
<-sc
|
|
|
|
|
|
|
|
err = bot.Close()
|
|
|
|
if err != nil {
|
2020-05-04 22:03:11 +00:00
|
|
|
log.Fatal("[!] Error: " + err.Error())
|
2020-04-11 14:16:09 +00:00
|
|
|
}
|
|
|
|
}
|