Initial commit

This commit is contained in:
skeletable 2022-03-06 20:12:38 +01:00
commit caa74a9ad0
15 changed files with 237 additions and 0 deletions

1
dat1 Normal file
View File

@ -0,0 +1 @@
0.604660

0
games/eu/fof Normal file
View File

0
games/eu/gmod Normal file
View File

0
games/eu/hunt Normal file
View File

0
games/eu/risk Normal file
View File

2
games/gmod Normal file
View File

@ -0,0 +1,2 @@
228911057316347905
871107178809937960

5
games/hunt Normal file
View File

@ -0,0 +1,5 @@
kekkoken
jordy
cox228911057316347905
228911057316347905
228911057316347905

0
games/us/fof Normal file
View File

0
games/us/gmod Normal file
View File

0
games/us/hunt Normal file
View File

0
games/us/risk Normal file
View File

10
go.mod Normal file
View File

@ -0,0 +1,10 @@
module inaba9000
go 1.17
require github.com/bwmarrin/discordgo v0.23.2
require (
github.com/gorilla/websocket v1.4.0 // indirect
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 // indirect
)

6
go.sum Normal file
View File

@ -0,0 +1,6 @@
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/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=

205
main.go Normal file
View File

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

8
workspace.code-workspace Normal file
View File

@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}