This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
/
bot.go
147 lines (120 loc) · 3.34 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
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
package discorddgo
import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"os"
"github.com/bwmarrin/discordgo"
)
// A bot is a running discord session intended for usage
// with a Bot-type Login
type Bot struct {
context Context
evMux EventSink
}
// NewBot uses the given token to log into discord and return
// a Bot-type session.
//
// The "Bot " authorization string is automatically appended
// if not found.
func NewBot(token string) (*Bot, error) {
if !strings.HasPrefix(token, "Bot ") {
token = "Bot " + token
}
dg, err := discordgo.New()
if err != nil {
return nil, err
}
b := &Bot{
context: Context{intSession: dg, exit: make(chan bool)},
evMux: nil,
}
dg.AddHandler(b.execHandler)
err = dg.Open()
if err != nil {
return nil, err
}
return b, nil
}
// Sink returns the current EventSink
//
// This may be used by plugins to dispatch custom events.
func (b *Bot) Sink() EventSink {
return b.evMux
}
// SetSink sets the EventSink to be used by D.G
func (b *Bot) SetSink(ev EventSink) {
b.evMux = ev
}
// CurrentContext returns the current context pointer.
func (b *Bot) CurrentContext() Context {
return b.context
}
// Me returns the current bot user (using @me) or an error.
func (b *Bot) Me() (*User, error) {
return b.context.UserFromID("@me")
}
func (b *Bot) execHandler(s *discordgo.Session, ev interface{}) error {
switch t := ev.(type) {
case *discordgo.MessageCreate:
return b.evMux.Dispatch(&EventNewMessage{context: b.context, m: t})
case *discordgo.MessageUpdate:
return b.evMux.Dispatch(&EventMessageUpdate{context: b.context, m: t})
case *discordgo.TypingStart:
return b.evMux.Dispatch(&EventUserTyping{context: b.context, ev: t})
}
return nil
}
// SetAvatarFromReader will read the given io.Reader and
// set the content as avatar
//
// Remember to close the reader after use if applicable.
func (b *Bot) SetAvatarFromReader(image io.Reader) error {
data, err := ioutil.ReadAll(image)
if err != nil {
return err
}
me, err := b.Me()
if err != nil {
return err
}
b64 := base64.StdEncoding.EncodeToString(data)
avatar := fmt.Sprintf("data:%s;base64,%s", http.DetectContentType(data), b64)
_, err = b.context.intSession.UserUpdate("", "", me.internalUser.Username, avatar, "")
return err
}
// SetAvatarFromURI will download the URI data and set it as avatar
func (b *Bot) SetAvatarFromURI(uri string) error {
resp, err := http.Get(uri)
if err != nil {
return err
}
defer resp.Body.Close()
return b.SetAvatarFromReader(resp.Body)
}
// SetAvatarFromFile will read the file and set it as avatar
func (b *Bot) SetAvatarFromFile(file string) error {
dat, err := os.Open(file)
if err != nil {
return err
}
defer dat.Close()
return b.SetAvatarFromReader(dat)
}
// BlockForExit will block on the exit channel of the bot.
// Any code that has access to the application context can unblock
// this method, which usually means exiting the application.
// Exiting the entire application is not enforced, this function only blocks,
// so it may be used to restart the bot too.
func (b *Bot) BlockForExit() {
<-b.context.exit
}
// GetDiscordgoSession returns the underlying session the bot instance is using.
// This is not recommended unless the functionality you want to use is not
// implemented on d.g yet.
func (b *Bot) GetDiscordgoSession() *discordgo.Session {
return b.context.intSession
}