/
discordbee.go
153 lines (133 loc) 路 3.83 KB
/
discordbee.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (C) 2019 CalmBit
* 2014-2019 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* CalmBit <calmbit@posto.net>
* Christian Muehlhaeuser <muesli@gmail.com>
*/
// Package discordbee is a bee for sending and receiving messages with Discord
// servers.
package discordbee
import (
"github.com/bwmarrin/discordgo"
"github.com/muesli/beehive/bees"
)
// DiscordBee is a bee for sending and receiving messages with Discord
// servers.
type DiscordBee struct {
bees.Bee
apiToken string
triggerPhrase string
eventChan chan bees.Event
discord *discordgo.Session
}
// Run executes the Bee's event loop.
func (mod *DiscordBee) Run(eventChan chan bees.Event) {
mod.eventChan = eventChan
mod.LogDebugf("Starting discord bee with apiToken %s", mod.apiToken)
d, err := discordgo.New("Bot " + mod.apiToken)
if err != nil {
mod.LogFatal("Unable to start discordbee! Error: ", err)
}
mod.discord = d
mod.LogDebugf("Done!")
mod.discord.AddHandler(mod.isReady)
mod.discord.AddHandler(mod.onSend)
err = mod.discord.Open()
if err != nil {
mod.LogFatal("Error opening websocket: ", err)
}
defer mod.discord.Close()
select {
case <-mod.SigChan:
return
}
}
func (mod *DiscordBee) isReady(s *discordgo.Session, event *discordgo.Ready) {
// Default status - once the persistance layer exists:
// TODO: Persist status, use it here.
mod.LogDebugf("isReady called")
s.UpdateStatus(0, "with bees")
}
func (mod *DiscordBee) onSend(s *discordgo.Session, event *discordgo.MessageCreate) {
if event.Author.ID == s.State.User.ID {
return
}
c, e := s.Channel(event.ChannelID)
if e != nil {
mod.LogErrorf("Unable to find channel with id %s", event.ChannelID)
return
}
ev := bees.Event{
Bee: mod.Name(),
Name: "message",
Options: []bees.Placeholder{
{
Name: "contents",
Type: "string",
Value: event.Content,
},
{
Name: "username",
Type: "string",
Value: event.Author.String(),
},
{
Name: "channel_id",
Type: "string",
Value: event.ChannelID,
},
{
Name: "channel_name",
Type: "string",
Value: c.Name,
},
},
}
mod.eventChan <- ev
}
// Action triggers the action passed to it.
func (mod *DiscordBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}
switch action.Name {
case "send":
var contents string
var channelID string
action.Options.Bind("contents", &contents)
action.Options.Bind("channel_id", &channelID)
_, err := mod.discord.ChannelMessageSend(channelID, contents)
if err != nil {
mod.LogErrorf("Unable to send message: %v", err)
}
case "set_status":
var status string
action.Options.Bind("status", &status)
err := mod.discord.UpdateStatus(0, status)
if err != nil {
mod.LogErrorf("Unable to update status: %v", err)
}
default:
panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
}
return outs
}
// ReloadOptions parses the config options and initializes the Bee.
func (mod *DiscordBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
options.Bind("api_token", &mod.apiToken)
options.Bind("trigger_phrase", &mod.triggerPhrase)
}