From 3175dab9eed66062341bd55617a7fb5257294cd7 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Mon, 26 Dec 2022 15:22:52 +0000 Subject: [PATCH] added discord bot --- go.mod | 2 ++ go.sum | 4 +++ main.go | 8 ++++- notifiers/discord.go | 82 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 notifiers/discord.go diff --git a/go.mod b/go.mod index 73cef2f..1ce4291 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 85751cd..0ba2b63 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 072aacc..3eca76c 100644 --- a/main.go +++ b/main.go @@ -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() { diff --git a/notifiers/discord.go b/notifiers/discord.go new file mode 100644 index 0000000..58d5509 --- /dev/null +++ b/notifiers/discord.go @@ -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 +}