Skip to content

Instantly share code, notes, and snippets.

@blizzarddreams
Last active December 15, 2015 01:47
Show Gist options
  • Select an option

  • Save blizzarddreams/b69ae6c4c3602fd5a904 to your computer and use it in GitHub Desktop.

Select an option

Save blizzarddreams/b69ae6c4c3602fd5a904 to your computer and use it in GitHub Desktop.
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
)
func main() {
bot := &Bot{}
file, err := ioutil.ReadFile("config.yaml")
if err != nil {
log.Fatal(err)
}
yaml.Unmarshal(file, bot)
bot.Connect()
//go bot.PingPong()
go bot.Console()
}
package main
import (
"bufio"
"fmt"
"log"
"net"
"net/textproto"
"strings"
"os"
)
type Bot struct {
Nick string
Pass string
Channels []string
Server string
Symbol string
Conn net.Conn
}
func (b Bot) Console() {
reader := bufio.NewReader(os.Stdin)
for {
text, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
if len(text) > 0 {
fmt.Println("Text entered")
}
}
}
func (b *Bot) Connect() {
b.Conn, _ = net.Dial("tcp", b.Server)
defer b.Conn.Close()
fmt.Fprintf(b.Conn, "USER %s 8 * :%s\r\n", b.Nick, b.Nick)
fmt.Fprintf(b.Conn, "NICK %s\r\n", b.Nick)
for _, channel := range b.Channels {
if strings.HasPrefix(channel, "#") {
fmt.Fprintf(b.Conn, "JOIN %s\r\n", channel)
} else {
fmt.Fprintf(b.Conn, "JOIN #%s\r\n", channel)
}
}
for {
msg, err := textproto.NewReader(bufio.NewReader(b.Conn)).ReadLine()
if err != nil {
log.Fatal(err)
}
if strings.Contains(msg, " :"+b.Symbol) {
message := b.Symbol+strings.Split(msg, " :"+b.Symbol)[1]
cmd := strings.Split(message, b.Symbol)[1]
cmd = strings.Split(cmd, " ")[0]
args_array := strings.Split(message, cmd+" ")
var args string
if len(args_array) == 1 {
args = ""
} else {
args = args_array[1]
}
room := strings.Split(msg, "PRIVMSG #")[1]
room = strings.Split(room, " ")[0]
go func() {
function := Commands[cmd](args)
fmt.Fprintf(b.Conn, "PRIVMSG #%v :%v\r\n", room, function)
}()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment