girc-atomic/example_test.go

116 lines
2.5 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 (
"log"
2017-02-08 08:23:31 +00:00
"os"
"strings"
2017-04-18 18:51:34 +00:00
"time"
2021-10-09 13:25:17 +00:00
"github.com/yunginnanet/girc-atomic"
)
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,
})
2017-05-07 17:00:02 +00:00
if err := client.Connect(); err != nil {
log.Fatal(err)
}
}
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",
2017-02-08 08:23:31 +00:00
})
2017-05-07 17:00:02 +00:00
if err := client.Connect(); err != nil {
log.Fatal(err)
}
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-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-06-15 08:46:10 +00:00
c.Cmd.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.Last(), "hello") {
2022-10-23 08:56:01 +00:00
_ = c.Cmd.ReplyTo(e, "hello world!")
2017-06-07 10:49:13 +00:00
return
}
2017-05-07 17:00:02 +00:00
if strings.Contains(e.Last(), "quit") {
2017-05-07 17:00:02 +00:00
c.Close()
}
})
2017-04-18 18:51:34 +00:00
// An example of how you would add reconnect logic.
for {
2017-05-07 17:00:02 +00:00
if err := client.Connect(); err != nil {
log.Printf("error: %s", err)
2017-04-18 18:51:34 +00:00
2017-05-07 17:00:02 +00:00
log.Println("reconnecting in 30 seconds...")
time.Sleep(30 * time.Second)
} else {
return
}
2017-04-18 18:51:34 +00:00
}
}
// 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-06-15 08:46:10 +00:00
c.Cmd.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.Last(), "!hello") {
2022-10-23 08:56:01 +00:00
_ = c.Cmd.ReplyTo(e, girc.Fmt("{b}hello{b} {blue}world{c}!"))
return
}
if strings.HasPrefix(e.Last(), "!stop") {
2017-05-07 16:39:16 +00:00
c.Close()
return
}
})
if err := client.Connect(); err != nil {
log.Fatalf("an error occurred while attempting to connect to %s: %s", client.Server(), err)
}
}