-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
example_test.go
117 lines (99 loc) · 2.52 KB
/
example_test.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
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package girc_test
import (
"log"
"os"
"strings"
"time"
"github.com/lrstanley/girc"
)
func ExampleNew() {
client := girc.New(girc.Config{
Server: "irc.byteirc.org",
Port: 6667,
Nick: "test",
User: "user",
SASL: &girc.SASLPlain{User: "user1", Pass: "securepass1"},
Out: os.Stdout,
})
if err := client.Connect(); err != nil {
log.Fatal(err)
}
}
// The bare-minimum needed to get started with girc. Just connects and idles.
func Example_bare() {
client := girc.New(girc.Config{
Server: "irc.byteirc.org",
Port: 6667,
Nick: "test",
User: "user",
Debug: os.Stdout,
})
if err := client.Connect(); err != nil {
log.Fatal(err)
}
}
// Very simple example that connects, joins a channel, and responds to
// "hello" with "hello world!".
func Example_simple() {
client := girc.New(girc.Config{
Server: "irc.byteirc.org",
Port: 6667,
Nick: "test",
User: "user",
Name: "Example bot",
Debug: os.Stdout,
})
client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
c.Cmd.Join("#dev")
})
client.Handlers.Add(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {
if strings.Contains(e.Last(), "hello") {
c.Cmd.ReplyTo(e, "hello world!")
return
}
if strings.Contains(e.Last(), "quit") {
c.Close()
}
})
// An example of how you would add reconnect logic.
for {
if err := client.Connect(); err != nil {
log.Printf("error: %s", err)
log.Println("reconnecting in 30 seconds...")
time.Sleep(30 * time.Second)
} else {
return
}
}
}
// Another basic example, however with this, we add simple !<command>
// responses to things. E.g. "!hello", "!stop", and "!restart".
func Example_commands() {
client := girc.New(girc.Config{
Server: "irc.byteirc.org",
Port: 6667,
Nick: "test",
User: "user",
Name: "Example bot",
Out: os.Stdout,
})
client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
c.Cmd.Join("#channel", "#other-channel")
})
client.Handlers.Add(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {
if strings.HasPrefix(e.Last(), "!hello") {
c.Cmd.ReplyTo(e, girc.Fmt("{b}hello{b} {blue}world{c}!"))
return
}
if strings.HasPrefix(e.Last(), "!stop") {
c.Close()
return
}
})
if err := client.Connect(); err != nil {
log.Fatalf("an error occurred while attempting to connect to %s: %s", client.Server(), err)
}
}