-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
75 lines (60 loc) · 1.68 KB
/
example_test.go
File metadata and controls
75 lines (60 loc) · 1.68 KB
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
package discordbotrus_test
import (
"io"
"os"
"testing"
"github.com/bwmarrin/discordgo"
"github.com/outdead/discordbotrus"
"github.com/sirupsen/logrus"
)
func TestExample(t *testing.T) {
cfg := discordbotrus.NewDefaultConfig(os.Getenv("LDH_TOKEN"), os.Getenv("LDH_CHANNEL"))
hook, err := discordbotrus.New(cfg)
if err != nil {
t.Fatalf("expected nil got error: %s", err)
}
defer func() {
if err := hook.Close(); err != nil {
t.Fatalf("expected nil got error: %s", err)
}
}()
logger := &logrus.Logger{
Out: io.Discard,
Formatter: new(logrus.JSONFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}
logger.AddHook(hook)
logger.Info("My spoon is too big")
}
func TestExampleWithSession(t *testing.T) {
token := os.Getenv("LDH_TOKEN")
channelID := os.Getenv("LDH_CHANNEL")
session, err := discordgo.New("Bot " + token)
if err != nil {
t.Fatalf("expected nil got error: %s", err)
}
// In this case, you can use the session without opening a web socket.
// But to establish a stable connection, it is better to do this.
if err := session.Open(); err != nil {
t.Fatalf("open discord session error: %s", err)
}
defer func() {
if err := session.Close(); err != nil {
t.Fatalf("expected nil got error: %s", err)
}
}()
cfg := &discordbotrus.Config{ChannelID: channelID}
hook, err := discordbotrus.New(cfg, discordbotrus.WithSession(session))
if err != nil {
t.Fatalf("expected nil got error: %s", err)
}
logger := &logrus.Logger{
Out: io.Discard,
Formatter: new(logrus.JSONFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}
logger.AddHook(hook)
logger.Info("My spoon is too big")
}