|
|
|
@ -0,0 +1,205 @@
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
"strings"
|
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Variables used for command line parameters
|
|
|
|
|
var (
|
|
|
|
|
Token string
|
|
|
|
|
games = make([][]string, 0)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// The list of games is currently a hardcoded slice of slices. The first element of
|
|
|
|
|
// a game slice should be the default ("display") name of the game, second one is a short name, the other ones are aliases.
|
|
|
|
|
func getGames() [][]string {
|
|
|
|
|
return [][]string{
|
|
|
|
|
{"Garry's Mod", "gmod", "garrys mod"},
|
|
|
|
|
{"Hunt Showdown", "hunt"},
|
|
|
|
|
{"Risk of Rain", "risk", "risky"},
|
|
|
|
|
{"Fistful of Frags", "fof", "fistful"},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getFiles() {
|
|
|
|
|
fmt.Println("Checking files for Europe region.")
|
|
|
|
|
for _, i := range getGames() {
|
|
|
|
|
_, err := os.Stat("games/eu/" + i[1])
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
fmt.Println("games/eu/" + i[1] + " does not exist, attempting creation")
|
|
|
|
|
file, err := os.Create("games/eu/" + i[1])
|
|
|
|
|
defer file.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("games/eu/" + i[1] + " exists")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("Checking files for America region.")
|
|
|
|
|
for _, i := range getGames() {
|
|
|
|
|
_, err := os.Stat("games/us/" + i[1])
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
fmt.Println("games/us/" + i[1] + " does not exist, attempting creation")
|
|
|
|
|
file, err := os.Create("games/us/" + i[1])
|
|
|
|
|
defer file.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("games/us/" + i[1] + " exists")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
|
|
fmt.Println("Checking files.")
|
|
|
|
|
getFiles()
|
|
|
|
|
|
|
|
|
|
// Create a new Discord session using the provided bot token.
|
|
|
|
|
dg, err := discordgo.New("Bot " + "NzU3MzY4MjEwMzYxNDE3NzM4.X2fYHA.hLHKA_go3ZcXnxWbqdjbaPm7oVI")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("error creating Discord session,", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register the messageCreate func as a callback for MessageCreate events.
|
|
|
|
|
dg.AddHandler(messageCreate)
|
|
|
|
|
|
|
|
|
|
// In this example, we only care about receiving message events.
|
|
|
|
|
dg.Identify.Intents = discordgo.IntentsGuildMessages
|
|
|
|
|
|
|
|
|
|
// Open a websocket connection to Discord and begin listening.
|
|
|
|
|
err = dg.Open()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("error opening connection,", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait here until CTRL-C or other term signal is received.
|
|
|
|
|
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
|
|
|
|
sc := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
|
|
|
|
<-sc
|
|
|
|
|
|
|
|
|
|
// Cleanly close down the Discord session.
|
|
|
|
|
dg.Close()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseArgs(s string) string {
|
|
|
|
|
args := strings.Split(s, " ")
|
|
|
|
|
args = args[1:]
|
|
|
|
|
return strings.ToLower(strings.Join(args, " "))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseArgsRegion(s string) (string, string) {
|
|
|
|
|
args := strings.Split(s, " ")
|
|
|
|
|
args = args[1:]
|
|
|
|
|
region := ""
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return "", region
|
|
|
|
|
} else if args[0] == "EU" || args[0] == "US" {
|
|
|
|
|
region = args[0]
|
|
|
|
|
}
|
|
|
|
|
return strings.ToLower(strings.Join(args[1:], " ")), region
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkGame(s string) (bool, string, string) {
|
|
|
|
|
found := false
|
|
|
|
|
game := ""
|
|
|
|
|
gameShort := ""
|
|
|
|
|
for _, i := range getGames() {
|
|
|
|
|
for _, x := range i {
|
|
|
|
|
if strings.ToLower(x) == s {
|
|
|
|
|
found = true
|
|
|
|
|
game = i[0]
|
|
|
|
|
gameShort = i[1]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return found, game, gameShort
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function will be called (due to AddHandler above) every time a new
|
|
|
|
|
// message is created on any channel that the authenticated bot has access to.
|
|
|
|
|
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
|
|
|
|
|
|
|
|
// Ignore all messages created by the bot itself
|
|
|
|
|
// This isn't required in this specific example but it's a good practice.
|
|
|
|
|
if m.Author.ID == s.State.User.ID {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if m.Author.ID == "866299723082498058" || m.Author.ID == "226107558287507457" {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, "1 blocked message")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(m.Content, "pp!summon") {
|
|
|
|
|
args, region := parseArgsRegion(m.Content)
|
|
|
|
|
found, game, gameShort := checkGame(args)
|
|
|
|
|
if region == "" {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, "You have to provide a region (EU or US)")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if found {
|
|
|
|
|
content, err := ioutil.ReadFile("games/" + gameShort)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, err.Error())
|
|
|
|
|
}
|
|
|
|
|
gamers := strings.Split(string(content), "\n")
|
|
|
|
|
for n, i := range gamers {
|
|
|
|
|
gamers[n] = "<@" + i + ">"
|
|
|
|
|
}
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, "Summoning "+strings.Join(gamers, " ")+" for "+game+"!")
|
|
|
|
|
} else {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, "Game not found!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(m.Content, "pp!register") {
|
|
|
|
|
found, game, gameShort := checkGame(parseArgs(m.Content))
|
|
|
|
|
if found {
|
|
|
|
|
f, err := os.OpenFile("games/"+gameShort, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, err.Error())
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
if _, err := f.WriteString(m.Author.ID + "\n"); err != nil {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, err.Error())
|
|
|
|
|
}
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, "Registered for "+game+"!")
|
|
|
|
|
} else {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, "Game not found!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(m.Content, "pp!gamers") {
|
|
|
|
|
found, game, gameShort := checkGame(parseArgs(m.Content))
|
|
|
|
|
if found {
|
|
|
|
|
content, err := ioutil.ReadFile("games/" + gameShort)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, err.Error())
|
|
|
|
|
}
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, "Gamers for "+game+":\n"+string(content))
|
|
|
|
|
} else {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, "Game not found!")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(m.Content, "pp!roll") {
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, fmt.Sprint(rand.Intn(100)))
|
|
|
|
|
}
|
|
|
|
|
}
|