2017-01-03 16:05:27 +00:00
|
|
|
// Copyright 2016-2017 Liam Stanley <me@liamstanley.io>. All rights reserved.
|
2016-12-09 12:10:33 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
package girc_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/lrstanley/girc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Very simple example that connects, joins a channel, and responds to
|
|
|
|
// "hello" with "hello world!".
|
|
|
|
func Example() {
|
|
|
|
conf := girc.Config{
|
2017-02-05 10:23:05 +00:00
|
|
|
Server: "irc.byteirc.org",
|
|
|
|
Port: 6667,
|
|
|
|
Nick: "test",
|
|
|
|
User: "user",
|
|
|
|
Name: "Example bot",
|
2016-12-09 12:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client := girc.New(conf)
|
|
|
|
|
|
|
|
client.Callbacks.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
|
2016-12-13 15:23:19 +00:00
|
|
|
c.Join("#dev")
|
2016-12-09 12:10:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client.Callbacks.Add(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {
|
|
|
|
if strings.Contains(e.Trailing, "hello") {
|
|
|
|
c.Message(e.Params[0], "hello world!")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := client.Connect(); err != nil {
|
|
|
|
log.Fatalf("an error occurred while attempting to connect to %s: %s", client.Server(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Loop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Another basic example, however with this, we add simple !<command>
|
|
|
|
// responses to things. E.g. "!hello", "!stop", and "!restart".
|
|
|
|
func Example_commands() {
|
|
|
|
conf := girc.Config{
|
2017-02-05 10:23:05 +00:00
|
|
|
Server: "irc.byteirc.org",
|
|
|
|
Port: 6667,
|
|
|
|
Nick: "test",
|
|
|
|
User: "user",
|
|
|
|
Name: "Example bot",
|
2016-12-09 12:10:33 +00:00
|
|
|
}
|
|
|
|
channels := []string{"#dev"}
|
|
|
|
|
|
|
|
client := girc.New(conf)
|
|
|
|
|
|
|
|
client.Callbacks.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
|
2016-12-13 15:45:08 +00:00
|
|
|
c.Join(channels...)
|
2016-12-09 12:10:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client.Callbacks.Add(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {
|
|
|
|
if strings.HasPrefix(e.Trailing, "!hello") {
|
|
|
|
c.Message(e.Params[0], "hello world!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(e.Trailing, "!stop") {
|
|
|
|
c.Quit("goodbye!")
|
|
|
|
c.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(e.Trailing, "!restart") {
|
|
|
|
go c.Reconnect()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := client.Connect(); err != nil {
|
|
|
|
log.Fatalf("an error occurred while attempting to connect to %s: %s", client.Server(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Loop()
|
|
|
|
}
|