Skip to content

Instantly share code, notes, and snippets.

@iwanbk
Created May 17, 2014 06:38
Show Gist options
  • Save iwanbk/c8efa94947b6d3c6cd55 to your computer and use it in GitHub Desktop.
Save iwanbk/c8efa94947b6d3c6cd55 to your computer and use it in GitHub Desktop.
useless irc bot
package main
import (
"fmt"
"github.com/iwanbk/ogric"
"log"
"os"
)
const myNick = "botku"
var client *ogric.Ogric
func main() {
user := "user"
server := "irc.freenode.net:6667"
client = ogric.NewOgric(myNick, user, server)
evtChan, err := client.Start()
if err != nil {
log.Fatal("failed to start ogric = " + err.Error())
}
for {
evt := <-evtChan
handleEvent(&evt)
}
}
//see https://www.alien.net.au/irc/irc2numerics.html for list of events
var fnMap = map[string]func(*ogric.Event){
"001": handle001,
"PRIVMSG": handlePrivMsg,
"JOIN": handleJoin,
"NICK": handleNick,
}
func printEvent(e *ogric.Event) {
fmt.Println("---event---")
fmt.Printf("code = %s, nick=%s\n", e.Code, e.Nick)
fmt.Printf("message=%s\n", e.Message)
fmt.Printf("arguments = %v\n", e.Arguments)
}
func handleEvent(e *ogric.Event) {
printEvent(e)
fn, ok := fnMap[e.Code]
if ok {
fn(e)
}
}
func handleNick(e *ogric.Event) {
fmt.Println("current nick = " + client.Nick)
}
func handleJoin(e *ogric.Event) {
fmt.Println("Joined...")
}
func handle001(e *ogric.Event) {
fmt.Println("handle001")
client.Join("#golang-id")
}
func handlePrivMsg(e *ogric.Event) {
fmt.Println("handlePrivMsg")
target := e.Arguments[0]
if target == myNick && e.Message == "die" && e.Nick == "ibk" {
fmt.Println("my master killed me. Dying....")
client.Stop()
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment