diff --git a/bot.go b/bot.go index 8787cd2..d141af4 100644 --- a/bot.go +++ b/bot.go @@ -21,8 +21,14 @@ const ( discordPrefix = "!" ) +type reaction struct { + messageID string + emojiID string +} + type message struct { *discordgo.MessageSend + *reaction channelID string } @@ -36,7 +42,7 @@ func parseFields(fields []string, author *discordgo.User, channelID string) (res case "status": for i := range config.Servers { msg := serverStatus(config.Servers[i]) - sendChan <- message{msg, channelID} + sendChan <- message{MessageSend: msg, channelID: channelID} } case "skill": if len(fields) == 1 { @@ -102,11 +108,24 @@ func parseFields(fields []string, author *discordgo.User, channelID string) (res return } +func processThreadMessage(s *discordgo.Session, m *discordgo.MessageCreate, t thread) { + if !t.Meme { + return + } + if len(m.Attachments) == 0 && !strings.Contains(m.Content, "https://") && !strings.Contains(m.Content, "http://") { + return + } + sendChan <- message{channelID: m.ChannelID, reaction: &reaction{messageID: m.ID, emojiID: "\U0001F44D"}} +} + func handleCommand(s *discordgo.Session, m *discordgo.MessageCreate) { if s.State.User.ID == m.Author.ID { return } msg := m.Message.Content + if t, ok := config.Threads[m.ChannelID]; ok { + processThreadMessage(s, m, t) + } if !strings.HasPrefix(msg, cmdPrefix) { return } @@ -118,19 +137,23 @@ func handleCommand(s *discordgo.Session, m *discordgo.MessageCreate) { response = &discordgo.MessageSend{Content: "Error: " + err.Error()} } if response != nil { - sendChan <- message{response, m.ChannelID} + sendChan <- message{MessageSend: response, channelID: m.ChannelID} } } } func sendMsg(c chan message, s *discordgo.Session) { - for { - msg := <-c + for msg := range c { channelID := msg.channelID if channelID == "" { channelID = config.ChannelID } - s.ChannelMessageSendComplex(channelID, msg.MessageSend) + if msg.MessageSend != nil { + s.ChannelMessageSendComplex(channelID, msg.MessageSend) + } + if msg.reaction != nil { + s.MessageReactionAdd(channelID, msg.messageID, msg.emojiID) + } time.Sleep(time.Second) } } @@ -168,6 +191,11 @@ func bot() (err error) { config.Servers[i].restartChan = restartChan go query(config.Servers[i]) } + for tid := range config.Threads { + if err := dg.ThreadJoin(tid); err != nil { + log.Printf("Error joining thread %s: %s", tid, err) + } + } 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) diff --git a/config.go b/config.go index 2581d34..07ab3d6 100644 --- a/config.go +++ b/config.go @@ -67,14 +67,18 @@ type seeding struct { Cooldown int } +type thread struct { + Meme bool +} + var config struct { Token string - SteamKey string `json:"steam_key"` - ChannelID string `json:"channel_id"` - LevelDBPath string `json:"ldb_database_path"` - BoltDBPath string `json:"bdb_database_path"` - QueryInterval time.Duration `json:"query_interval"` - FailureLimit int `json:"failure_limit"` + SteamKey string `json:"steam_key"` + ChannelID string `json:"channel_id"` + Threads map[string]thread `json:"threads"` + BoltDBPath string `json:"bdb_database_path"` + QueryInterval time.Duration `json:"query_interval"` + FailureLimit int `json:"failure_limit"` Servers []*ns2server Seeding seeding } diff --git a/config_sample.json b/config_sample.json index 6139498..9f05343 100644 --- a/config_sample.json +++ b/config_sample.json @@ -5,6 +5,11 @@ "bdb_database_path": "/path/to/botdb/", "query_interval": 60, "failure_limit": 10, + "threads": { + "899251801575026708": { + "meme": true + } + }, "servers": [ { "name": "Server 1", diff --git a/go.mod b/go.mod index 1fa768d..213b549 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,11 @@ module rkfg.me/ns2query go 1.15 require ( - github.com/Philipp15b/go-steamapi v0.0.0-20200122161829-728086d96bab + github.com/Philipp15b/go-steamapi v0.0.0-20210114153316-ec4fdd23b4c1 github.com/bwmarrin/discordgo v0.23.2 github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 github.com/rumblefrog/go-a2s v1.0.1 go.etcd.io/bbolt v1.3.6 ) -// temporary override until pull requests #11 and #12 are merged -replace github.com/Philipp15b/go-steamapi => github.com/rkfg/go-steamapi v0.0.0-20201114201332-f991dbb01c37 +replace github.com/bwmarrin/discordgo => github.com/vysiondev/discordgo v0.23.3-0.20210824070826-857485731eb0 diff --git a/go.sum b/go.sum index 63b1d85..14e94dc 100644 --- a/go.sum +++ b/go.sum @@ -1,24 +1,21 @@ -github.com/bwmarrin/discordgo v0.22.0 h1:uBxY1HmlVCsW1IuaPjpCGT6A2DBwRn0nvOguQIxDdFM= -github.com/bwmarrin/discordgo v0.22.0/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M= -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/Philipp15b/go-steamapi v0.0.0-20210114153316-ec4fdd23b4c1 h1:PD13eMe9XAgPQ0SYWyirqwyOJG90TlEWApCw8A699l0= +github.com/Philipp15b/go-steamapi v0.0.0-20210114153316-ec4fdd23b4c1/go.mod h1:eQR7Xf64m2ALDAQE7Nr9ylFZhav1izvF3zzysKPhb0I= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= -github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= -github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= -github.com/rkfg/go-steamapi v0.0.0-20201114201332-f991dbb01c37 h1:KoCNFgiN3yjLsO0AI6AGGjUuEfx9sAdmezhJ6o/l1DY= -github.com/rkfg/go-steamapi v0.0.0-20201114201332-f991dbb01c37/go.mod h1:XWS07Cev9jhIvpAoBhLce/SvddN/x5nV5VbUnd97gFE= -github.com/rumblefrog/go-a2s v1.0.0 h1:9IIVIOQ1bXZJeTilmzkJDeGa/9W1c089VciTbp+Wp1Y= -github.com/rumblefrog/go-a2s v1.0.0/go.mod h1:JwbTgMTRGZcWzr3T2MUfDusrJU5Bdg8biEeZzPtN0So= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/rumblefrog/go-a2s v1.0.1 h1:0M4QUDB1Tz00tCz36r/AWB4YFO2ajksvPnd7Abzz9SE= github.com/rumblefrog/go-a2s v1.0.1/go.mod h1:JwbTgMTRGZcWzr3T2MUfDusrJU5Bdg8biEeZzPtN0So= -go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0= -go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= +github.com/vysiondev/discordgo v0.23.3-0.20210824070826-857485731eb0 h1:8JPoBMBQtdACFeRkNZvPIWV/IQO7bVOk7OseyDzreys= +github.com/vysiondev/discordgo v0.23.3-0.20210824070826-857485731eb0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= -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= -golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0= -golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d h1:L/IKR6COd7ubZrs2oTnTi73IhgqJ71c9s80WsQnh0Es= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=