Browse Source

Add skeleton for a Discord bot

master
Gregory Rudolph 3 years ago
parent
commit
7425ed6736
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 37
      config.go
  2. 56
      discord.go
  3. 5
      go.mod
  4. 6
      go.sum
  5. 8
      main.go
  6. 2
      rest-api.go
  7. 12
      types.go

37
config.go

@ -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)
}
}

56
discord.go

@ -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))
}
}

5
go.mod

@ -2,4 +2,7 @@ module git.hugfreevikings.wtf/GlassPorts/GlassPorts @@ -2,4 +2,7 @@ module git.hugfreevikings.wtf/GlassPorts/GlassPorts
go 1.16
require github.com/go-sql-driver/mysql v1.6.0
require (
github.com/bwmarrin/discordgo v0.23.2
github.com/go-sql-driver/mysql v1.6.0
)

6
go.sum

@ -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=

8
main.go

@ -21,7 +21,13 @@ func init() { @@ -21,7 +21,13 @@ func init() {
}
func main() {
config.GuildID = ""
if configFile == "" {
fmt.Println("No config specified.")
return
} else {
loadConfig()
}
LogMsg("Config: %+v\n", config)
if strings.Contains("discord", mode) {
startDiscord()
} else if strings.Contains("api", mode) {

2
rest-api.go

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
package main
func startRest() {
return
}

12
types.go

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
package main
import "github.com/bwmarrin/discordgo"
type Config struct {
GuildID string
@ -92,3 +94,13 @@ type AuthToken struct { @@ -92,3 +94,13 @@ type AuthToken struct {
PreviousHash string
Current string
}
type BotCommand struct {
Channel string
DiscordID string
Command string
Message *discordgo.MessageCreate
Session *discordgo.Session
Parts []string
Response string
}

Loading…
Cancel
Save