added discord bot

This commit is contained in:
BroodjeAap 2022-12-26 15:22:52 +00:00
parent 23bb6459ff
commit 3175dab9ee
4 changed files with 95 additions and 1 deletions

2
go.mod
View file

@ -25,6 +25,7 @@ require (
github.com/antchfx/xpath v1.2.1 // indirect
github.com/aws/aws-sdk-go v1.32.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bwmarrin/discordgo v0.26.1 // indirect
github.com/cbroglie/mustache v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cheggaaa/pb/v3 v3.0.5 // indirect
@ -40,6 +41,7 @@ require (
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect

4
go.sum
View file

@ -66,6 +66,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89FRkE=
github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/cbroglie/mustache v1.0.1 h1:ivMg8MguXq/rrz2eu3tw6g3b16+PQhoTn6EZAhst2mw=
github.com/cbroglie/mustache v1.0.1/go.mod h1:R/RUa+SobQ14qkP4jtx5Vke5sDytONDQXNLPY/PO69g=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@ -215,6 +217,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
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/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

View file

@ -160,12 +160,18 @@ func (web *Web) initCronJobs() {
func (web *Web) initNotifiers() {
web.notifiers = make(map[string]notifiers.Notifier, 5)
if viper.IsSet("telegram") {
if viper.IsSet("notifiers.telegram") {
telegramBot := notifiers.TelegramNotifier{}
if telegramBot.Open() {
web.notifiers["Telegram"] = &telegramBot
}
}
if viper.IsSet("notifiers.discord") {
discordBot := notifiers.DiscordNotifier{}
if discordBot.Open() {
web.notifiers["Discord"] = &discordBot
}
}
}
func (web *Web) pruneDB() {

82
notifiers/discord.go Normal file
View file

@ -0,0 +1,82 @@
package notifiers
import (
"log"
"github.com/bwmarrin/discordgo"
"github.com/spf13/viper"
)
type DiscordNotifier struct {
Bot *discordgo.Session
Token string
UserID string
UserChannel *discordgo.Channel
ServerID string
ChannelID string
ServerChannel *discordgo.Channel
Debug bool
}
func (discord *DiscordNotifier) Open() bool {
if !viper.IsSet("notifiers.discord.userID") && !viper.IsSet("notifiers.discord.server") {
log.Println("Net either 'serverID' or 'userID' for Discord")
return false
}
bot, err := discordgo.New("Bot " + viper.GetString("notifiers.discord.token"))
if err != nil {
log.Println("Could not start Discord notifier:\n", err)
return false
}
if viper.IsSet("notifiers.discord.userID") {
discord.UserID = viper.GetString("notifiers.discord.userID")
channel, err := bot.UserChannelCreate(discord.UserID)
if err != nil {
log.Println("Could not connect to user channel:", discord.UserID, err)
return false
}
discord.UserChannel = channel
log.Println("Authorized discord bot for:", channel.Recipients)
}
if viper.IsSet("notifiers.discord.server") {
discord.ServerID = viper.GetString("notifiers.discord.server.ID")
discord.ChannelID = viper.GetString("notifiers.discord.server.channel")
channels, err := bot.GuildChannels(discord.ServerID)
if err != nil {
log.Println("Could not connect to server channel:", discord.ServerID, err)
return false
}
foundChannel := false
for i := range channels {
channel := channels[i]
if channel.ID == discord.ChannelID {
foundChannel = true
discord.ServerChannel = channel
break
}
}
if !foundChannel {
log.Println("Did not find channel with '"+discord.ChannelID+"' in server:", discord.ServerID)
return false
}
log.Println("Authorized discord bot for:", discord.ServerChannel.Name)
}
discord.Bot = bot
bot.Debug = viper.GetBool("notifiers.discord.debug")
return true
}
func (discord *DiscordNotifier) Message(message string) bool {
if discord.UserChannel != nil {
discord.Bot.ChannelMessageSend(discord.UserChannel.ID, message)
}
if discord.ServerChannel != nil {
discord.Bot.ChannelMessageSend(discord.ServerChannel.ID, message)
}
return true
}
func (discord *DiscordNotifier) Close() bool {
discord.Bot.Close()
return true
}