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"
|
|
|
|
|
2021-05-23 02:07:57 +00:00
|
|
|
"github.com/bwmarrin/discordgo"
|
2020-04-11 14:16:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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-12-29 07:57:33 +00:00
|
|
|
bot.State.TrackChannels = true
|
|
|
|
bot.State.TrackMembers = true
|
2020-05-25 09:12:24 +00:00
|
|
|
|
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
|
2020-12-29 07:57:33 +00:00
|
|
|
g, err := bot.Guild(Config.GuildID)
|
2020-04-11 14:16:09 +00:00
|
|
|
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
|
2020-12-29 07:57:33 +00:00
|
|
|
p = permissions.New(&Config, g)
|
|
|
|
|
|
|
|
Config.GuildName = g.Name
|
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,
|
2020-06-22 05:35:15 +00:00
|
|
|
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,
|
2021-05-28 15:40:51 +00:00
|
|
|
AFK: false,
|
|
|
|
Status: "online",
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|