forked from Necroforger/dream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdream.go
99 lines (77 loc) · 1.99 KB
/
dream.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
package dream
import (
"io"
"os"
"sync"
"github.com/bwmarrin/discordgo"
)
//Generate the AddHandler and AddHandlerOnce methods
//go:generate go run tools/addhandlers/main.go
//Generate nextEvent functions
//go:generate go run tools/nextevent/main.go
//Session contains session information.
type Session struct {
sync.Mutex
// LogOutput is the Writer where all the log events are sent over.
// It is set to os.Stdout by default.
LogOutput io.Writer
DG *discordgo.Session
Config Config
// Client is the bot's user
Client *discordgo.User
// AudioDispatchers stores the Audio Dispatchers belonging to each guild
AudioDispatchers map[string]*AudioDispatcher
}
//Config represents dream's configuration
type Config struct {
// FfmpegPath is the path to use for the ffmpeg command.
// Default "ffmpeg"
FfmpegPath string
}
//NewConfig returns the default configuration options for the bot
func NewConfig() Config {
return Config{
FfmpegPath: "ffmpeg",
}
}
//New returns a new Session.
func New(conf Config, args ...interface{}) (*Session, error) {
bot := &Session{}
bot.Config = conf
bot.AudioDispatchers = map[string]*AudioDispatcher{}
bot.LogOutput = os.Stdout
session, err := discordgo.New(args...)
if err != nil {
return nil, err
}
bot.DG = session
return bot, nil
}
func (s *Session) addAudioDispatcher(ad *AudioDispatcher) {
s.Lock()
s.AudioDispatchers[ad.GuildID] = ad
s.Unlock()
}
// removeAudioDispatcher removes the audio dispatcher from the map with ID guildID
func (s *Session) removeAudioDispatcher(guildID string) {
s.Lock()
delete(s.AudioDispatchers, guildID)
s.Unlock()
}
// audioDispatcher returns an audio dispatcher by guild ID
func (s *Session) audioDispatcher(guildID string) (*AudioDispatcher, error) {
s.Lock()
defer s.Unlock()
if v, ok := s.AudioDispatchers[guildID]; ok {
return v, nil
}
return nil, ErrNotFound
}
// Open connects to discord websockets
func (s *Session) Open() error {
err := s.DG.Open()
if err != nil {
return err
}
return nil
}