Gregory Rudolph
3 years ago
7 changed files with 123 additions and 3 deletions
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"io/ioutil" |
||||
) |
||||
|
||||
func loadConfig() { |
||||
var c Config |
||||
confFile, err := ioutil.ReadFile(configFile) |
||||
if err != nil { |
||||
LogMsg("%+v\n", err) |
||||
return |
||||
} |
||||
err = json.Unmarshal([]byte(confFile), &c) |
||||
if err != nil { |
||||
LogMsg("%+v\n", err) |
||||
return |
||||
} |
||||
config = c |
||||
|
||||
LogMsg("Setup completed using config file.") |
||||
} |
||||
|
||||
func saveConfig() { |
||||
// defer log.PanicSafe()
|
||||
file, err := json.Marshal(config) |
||||
if err != nil { |
||||
LogMsg("%+v\n", err) |
||||
// log.LogErrorType(err)
|
||||
} |
||||
err = ioutil.WriteFile(configFile, file, 0600) |
||||
if err != nil { |
||||
LogMsg("%+v\n", err) |
||||
// log.LogErrorType(err)
|
||||
} |
||||
} |
@ -1,5 +1,59 @@
@@ -1,5 +1,59 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
"os/signal" |
||||
"strings" |
||||
"syscall" |
||||
|
||||
"github.com/bwmarrin/discordgo" |
||||
) |
||||
|
||||
var ( |
||||
dg *discordgo.Session |
||||
) |
||||
|
||||
func startDiscord() { |
||||
return |
||||
|
||||
dg, _ = discordgo.New("Bot " + config.BotToken) |
||||
dg.AddHandler(messageCreate) |
||||
_ = dg.Open() |
||||
sc := make(chan os.Signal, 1) |
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) |
||||
<-sc |
||||
dg.Close() |
||||
} |
||||
|
||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { |
||||
LogMsg("Detected incoming message.") |
||||
// Return if message came from a bot, or doesn't mention this bot
|
||||
if m.Author.Bot || !strings.Contains(m.Content, s.State.User.ID) || !strings.Contains(config.GuildID, m.GuildID) { |
||||
LogMsg("Ignoring message.") |
||||
return |
||||
} |
||||
// Split input for use in command functions
|
||||
parts := strings.Split(m.Content, " ") |
||||
b := BotCommand{ |
||||
Session: s, |
||||
Channel: m.ChannelID, |
||||
Message: m, |
||||
Command: parts[1], |
||||
DiscordID: m.Author.ID, |
||||
Parts: parts, |
||||
} |
||||
LogMsg("Command detected: %+v", b) |
||||
|
||||
// No valid command found
|
||||
b.Reply(fmt.Sprintf("Unknown command: %+v. Ping me with **iaadd** followed by a number or a link to the in-game screenshot of your score to record your Infinity Arena high score. Ping me with **pvpadd** followed by a number or a link to the in-game screenshot of your leaderboard high score to record your PvP leaderboard score. Ping me with **iacheck** or **pvpcheck** to request the information you have inserted.", b.Command)) |
||||
|
||||
} |
||||
|
||||
// Reply will reply to the BotCommand.Message, tagging the sender. If b.Response is set, it will use that otherwise the string will be used
|
||||
func (b BotCommand) Reply(s string) { |
||||
if len(b.Response) > 0 { |
||||
b.Session.ChannelMessageSend(b.Channel, fmt.Sprintf("<@%+v>: %+v", b.DiscordID, b.Response)) |
||||
} else { |
||||
b.Session.ChannelMessageSend(b.Channel, fmt.Sprintf("<@%+v>: %+v", b.DiscordID, s)) |
||||
} |
||||
} |
@ -1,2 +1,8 @@
@@ -1,2 +1,8 @@
|
||||
github.com/bwmarrin/discordgo v0.23.2 h1:BzrtTktixGHIu9Tt7dEE6diysEF9HWnXeHuoJEt2fH4= |
||||
github.com/bwmarrin/discordgo v0.23.2/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M= |
||||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= |
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= |
||||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= |
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= |
||||
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= |
||||
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= |
||||
|
@ -1,5 +1,7 @@
@@ -1,5 +1,7 @@
|
||||
package main |
||||
|
||||
|
||||
|
||||
func startRest() { |
||||
return |
||||
} |
Loading…
Reference in new issue