update examples to be multiple different files
This commit is contained in:
parent
e1f615a88f
commit
4f1db5bec3
@ -37,7 +37,7 @@
|
||||
|
||||
## Examples
|
||||
|
||||
See [girc/examples/](https://github.com/Liamraystanley/girc/tree/master/example) for some examples.
|
||||
See [girc/examples/](https://github.com/Liamraystanley/girc/tree/master/examples) for some examples.
|
||||
|
||||
## Contributing
|
||||
|
||||
|
91
examples/cli-prompt/main.go
Normal file
91
examples/cli-prompt/main.go
Normal file
@ -0,0 +1,91 @@
|
||||
// Copyright 2016 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 main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"os"
|
||||
|
||||
"github.com/Liamraystanley/girc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conf := girc.Config{
|
||||
Server: "irc.byteirc.org",
|
||||
Port: 6667,
|
||||
Nick: "test",
|
||||
User: "user",
|
||||
Name: "Example bot",
|
||||
MaxRetries: 3,
|
||||
Logger: os.Stdout,
|
||||
}
|
||||
channels := []string{"#dev"}
|
||||
|
||||
client := girc.New(conf)
|
||||
|
||||
client.AddCallback(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
|
||||
for _, ircchan := range channels {
|
||||
c.Join(ircchan, "")
|
||||
}
|
||||
})
|
||||
|
||||
client.AddCallback(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {
|
||||
if strings.HasPrefix(e.Trailing, "!stop") {
|
||||
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") {
|
||||
c.Reconnect()
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
if err := client.Connect(); err != nil {
|
||||
log.Fatalf("an error occurred while attempting to connect to %s: %s", client.Server(), err)
|
||||
}
|
||||
|
||||
go client.Loop()
|
||||
|
||||
// Everything after this line is just for fancy prompt stuff. Not needed.
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
client.AddCallback(girc.ALLEVENTS, func(c *girc.Client, e girc.Event) {
|
||||
fmt.Print("\r>> ")
|
||||
})
|
||||
|
||||
for {
|
||||
fmt.Print("\r>> ")
|
||||
input, _ := reader.ReadString('\n')
|
||||
input = strings.TrimSpace(input)
|
||||
if len(input) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if input == "quit" {
|
||||
client.Quit("g'day")
|
||||
client.Stop()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
e := girc.ParseEvent(input)
|
||||
if e == nil {
|
||||
fmt.Println("ERRONOUS INPUT")
|
||||
continue
|
||||
}
|
||||
|
||||
client.Send(e)
|
||||
}
|
||||
}
|
59
examples/commands/main.go
Normal file
59
examples/commands/main.go
Normal file
@ -0,0 +1,59 @@
|
||||
// Copyright 2016 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 main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"os"
|
||||
|
||||
"github.com/Liamraystanley/girc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conf := girc.Config{
|
||||
Server: "irc.byteirc.org",
|
||||
Port: 6667,
|
||||
Nick: "test",
|
||||
User: "user",
|
||||
Name: "Example bot",
|
||||
MaxRetries: 3,
|
||||
Logger: os.Stdout,
|
||||
}
|
||||
channels := []string{"#dev"}
|
||||
|
||||
client := girc.New(conf)
|
||||
|
||||
client.AddCallback(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
|
||||
for _, ircchan := range channels {
|
||||
c.Join(ircchan, "")
|
||||
}
|
||||
})
|
||||
|
||||
client.AddCallback(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()
|
||||
}
|
@ -18,7 +18,7 @@ func main() {
|
||||
Server: "irc.byteirc.org",
|
||||
Port: 6667,
|
||||
Nick: "test",
|
||||
User: "test1",
|
||||
User: "user",
|
||||
Name: "Example bot",
|
||||
MaxRetries: 3,
|
||||
Logger: os.Stdout,
|
||||
@ -31,12 +31,8 @@ func main() {
|
||||
})
|
||||
|
||||
client.AddCallback(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {
|
||||
if !e.IsAction() && strings.Contains(e.Trailing, "hello") {
|
||||
c.Message(e.Params[0], "Hello World!")
|
||||
}
|
||||
|
||||
if e.IsAction() && strings.Contains(e.Trailing, "hello") {
|
||||
c.Action(e.Params[0], "says Hello World!")
|
||||
if strings.Contains(e.Trailing, "hello") {
|
||||
c.Message(e.Params[0], "hello world!")
|
||||
}
|
||||
})
|
||||
|
||||
@ -44,5 +40,6 @@ func main() {
|
||||
log.Fatalf("an error occurred while attempting to connect to %s: %s", client.Server(), err)
|
||||
}
|
||||
|
||||
// Don't push this into a goroutine normally.
|
||||
client.Loop()
|
||||
}
|
16
main.go
16
main.go
@ -2,15 +2,15 @@
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Package girc provides a high level, yet flexible IRC library for use
|
||||
// with interacting with IRC servers. girc has support for user/channel
|
||||
// tracking, as well as a few other neat features (like auto-reconnect).
|
||||
// Package girc provides a high level, yet flexible IRC library for use with
|
||||
// interacting with IRC servers. girc has support for user/channel tracking,
|
||||
// as well as a few other neat features (like auto-reconnect).
|
||||
//
|
||||
// Much of what girc can do, can also be disabled. The goal is to
|
||||
// provide a solid API that you don't necessarily have to work with out
|
||||
// of the box if you don't want to.
|
||||
// Much of what girc can do, can also be disabled. The goal is to provide a
|
||||
// solid API that you don't necessarily have to work with out of the box if
|
||||
// you don't want to.
|
||||
//
|
||||
// See "example/main.go" for a brief and very useful example taking
|
||||
// See "examples/simple/main.go" for a brief and very useful example taking
|
||||
// advantage of girc, that should give you a general idea of how the API
|
||||
// works.
|
||||
package girc
|
||||
@ -127,7 +127,7 @@ func New(config Config) *Client {
|
||||
func (c *Client) Quit(message string) {
|
||||
c.state.hasQuit = true
|
||||
defer func() {
|
||||
// aaaand, unset c.hasQuit, so we can reconnect if we want to.
|
||||
// Unset c.hasQuit, so we can reconnect if we want to.
|
||||
c.state.hasQuit = false
|
||||
}()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user