girc-atomic/example_test.go

114 lines
2.6 KiB
Go
Raw Normal View History

2017-02-06 07:45:31 +00:00
// Copyright (c) Liam Stanley <me@liamstanley.io>. 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 (
"fmt"
"log"
2017-02-08 08:23:31 +00:00
"os"
"strings"
2017-04-18 18:51:34 +00:00
"time"
"github.com/lrstanley/girc"
)
func ExampleNew() {
client := girc.New(girc.Config{
Server: "irc.byteirc.org",
Port: 6667,
Nick: "test",
User: "user",
SASL: &girc.SASLAuth{User: "user1", Pass: "securepass1"},
Out: os.Stdout,
})
log.Fatal(client.Connect())
}
2017-02-08 08:23:31 +00:00
// The bare-minimum needed to get started with girc. Just connects and idles.
func Example_bare() {
client := girc.New(girc.Config{
2017-03-31 18:30:52 +00:00
Server: "irc.byteirc.org",
Port: 6667,
Nick: "test",
User: "user",
Debug: os.Stdout,
2017-02-08 08:23:31 +00:00
})
2017-03-31 18:30:52 +00:00
log.Fatal(client.Connect())
2017-02-08 08:23:31 +00:00
}
// Very simple example that connects, joins a channel, and responds to
// "hello" with "hello world!".
2017-02-08 08:23:31 +00:00
func Example_simple() {
client := girc.New(girc.Config{
2017-02-05 10:23:05 +00:00
Server: "irc.byteirc.org",
Port: 6667,
Nick: "test",
User: "user",
Name: "Example bot",
2017-03-30 09:02:43 +00:00
Debug: os.Stdout,
2017-02-08 08:23:31 +00:00
})
2017-02-12 03:51:05 +00:00
client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
2017-02-13 12:52:29 +00:00
c.Commands.Join("#dev")
})
2017-02-12 03:51:05 +00:00
client.Handlers.Add(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {
if strings.Contains(e.Trailing, "hello") {
2017-02-13 12:52:29 +00:00
c.Commands.Message(e.Params[0], "hello world!")
}
})
2017-04-18 18:51:34 +00:00
// An example of how you would add reconnect logic.
for {
log.Printf("error: %s", client.Connect())
log.Println("reconnecting in 30 seconds...")
time.Sleep(30 * time.Second)
}
}
// Another basic example, however with this, we add simple !<command>
// responses to things. E.g. "!hello", "!stop", and "!restart".
func Example_commands() {
2017-02-08 08:23:31 +00:00
client := girc.New(girc.Config{
2017-02-05 10:23:05 +00:00
Server: "irc.byteirc.org",
Port: 6667,
Nick: "test",
User: "user",
Name: "Example bot",
2017-03-31 18:30:52 +00:00
Out: os.Stdout,
2017-02-08 08:23:31 +00:00
})
2017-02-12 03:51:05 +00:00
client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
2017-02-13 12:52:29 +00:00
c.Commands.Join("#channel", "#other-channel")
})
2017-02-12 03:51:05 +00:00
client.Handlers.Add(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {
if strings.HasPrefix(e.Trailing, "!hello") {
2017-02-13 12:52:29 +00:00
c.Commands.Message(e.Params[0], "hello world!")
return
}
if strings.HasPrefix(e.Trailing, "!stop") {
2017-03-31 18:30:52 +00:00
c.Close(true)
return
}
})
if err := client.Connect(); err != nil {
log.Fatalf("an error occurred while attempting to connect to %s: %s", client.Server(), err)
}
}
func ExampleGlob() {
fmt.Println(girc.Glob("The quick brown fox jumps over the lazy dog", "*brown fox*")) // True.
fmt.Println(girc.Glob("The quick brown fox jumps over the lazy dog", "*yellow dog*")) // False.
// Output:
// true
// false
}