This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
110 lines (100 loc) · 3.67 KB
/
bot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"os"
"os/signal"
"strings"
"syscall"
)
// SessionCreate creates a new Discord session
func SessionCreate(token string) *discordgo.Session {
bot, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatal(err)
}
return bot
}
// StartBot adds event handlers and starts the main bot function
func StartBot(bot *discordgo.Session, bitbucketAPI1 *API, jiraAPI1 *API) {
// Register events
bot.AddHandler(Ready)
bot.AddHandler(MessageCreate(bitbucketAPI1, jiraAPI1))
// Create connection to bot
err := bot.Open()
if err != nil {
log.Fatal("Error opening Discord session: ", err)
}
// Routine to listen to webhooks
go ReceiveBitbucketWebhook(bot)
// Wait here until CTRL-C or other term signal is received.
// TODO Only call this if running in debug mode otherwise create background thread
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.
err = bot.Close()
if err != nil {
log.Fatal(err)
}
}
// Ready is called upon a ready event and sets the bots status
func Ready(session *discordgo.Session, _ *discordgo.Ready) {
err := session.UpdateGameStatus(0, "") // Add whatever game you want here
if err != nil {
log.Fatal(err)
}
}
// MessageCreate is called upon a received message and conditionally answers it
func MessageCreate(bitbucketAPI1 *API, jiraAPI1 *API) func(session *discordgo.Session, message *discordgo.MessageCreate) {
// We need to return a function here so we can pass over the api object
return func(session *discordgo.Session, message *discordgo.MessageCreate) {
var err error
if strings.HasPrefix(message.Content, "!post ") {
_, err = session.ChannelMessageSend(cfg["RELAY_CHANNEL"], PostMessage(message.Content))
}
if strings.HasPrefix(message.Content, "!uwu ") {
_, err = session.ChannelMessageSend(cfg["RELAY_CHANNEL"], PostUwUMessage(message.Content))
}
// This part is just for shits and giggles
if message.Author.ID == cfg["VIP"] {
switch message.Content {
case "!help":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, HelpMessage(true))
case "!allpullrequests":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, GetAllPullRequests(bitbucketAPI1, true))
case "!mypullrequests":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, GetMyPullRequests(bitbucketAPI1, message.Author.ID, true))
case "!myreviews":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, GetMyReviews(bitbucketAPI1, message.Author.ID, true))
case "!about":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, AboutMessage(true))
default:
return
}
} else {
// This part is the more serious portion of code here
switch message.Content {
case "!help":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, HelpMessage(false))
case "!allpullrequests":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, GetAllPullRequests(bitbucketAPI1, false))
case "!mypullrequests":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, GetMyPullRequests(bitbucketAPI1, message.Author.ID, false))
case "!myreviews":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, GetMyReviews(bitbucketAPI1, message.Author.ID, false))
//case "!sprint":
// _, err = session.ChannelMessageSendEmbed(message.ChannelID, GetActiveSprintMessage(jiraAPI1, false))
case "!about":
_, err = session.ChannelMessageSendEmbed(message.ChannelID, AboutMessage(false))
default:
return
}
}
if err != nil {
log.Println(err)
}
}
}