Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "encoding/json"
- "fmt"
- "log"
- "net"
- "net/http"
- "net/textproto"
- "strconv"
- "strings"
- "time"
- )
- // Bot 123
- type Bot struct {
- login string
- host string
- nick string
- conn net.Conn
- parser parse
- epm map[string]EPMEmote
- }
- var (
- globalTwitchEmotes []int
- )
- // EPMEmote struct for single emoteEPM data
- type EPMEmote struct {
- Channel string
- EmoteID string
- EPM int
- Emotes int
- }
- // NewBot xD
- func NewBot() *Bot {
- return &Bot{
- login: "spamchamp",
- host: cfg.IrcHost,
- nick: "justinfan123321",
- conn: nil,
- parser: *new(parse),
- epm: make(map[string]EPMEmote),
- }
- }
- // Connect asd
- func (bot *Bot) Connect() (conn net.Conn, err error) {
- log.Println(bot.host)
- conn, err = net.Dial("tcp", bot.host)
- if err != nil {
- Log.Fatal("unable to connect to IRC server ", err)
- }
- bot.conn = conn
- log.Printf("Connected to IRC server %s (%s)\n", bot.host, bot.conn.RemoteAddr())
- return bot.conn, nil
- }
- func (bot *Bot) startBot() {
- conn, _ := bot.Connect()
- fmt.Fprintf(conn, "LOGIN %s\r\n", bot.login)
- fmt.Fprintf(conn, "NICK %s\r\n", bot.nick)
- bot.joinChannel("#gempir")
- defer conn.Close()
- //go bot.joinTopChannels()
- go func() {
- t := time.NewTicker(time.Second)
- for {
- bot.calculateEPM()
- bot.syncEPM()
- <-t.C
- }
- }()
- reader := bufio.NewReader(conn)
- tp := textproto.NewReader(reader)
- for {
- line, err := tp.ReadLine()
- if err != nil {
- log.Println(err)
- break // break loop on errors
- }
- bot.handleMessage(line)
- }
- }
- func (bot *Bot) handleMessage(message string) {
- pmsg := bot.parser.Parse(message)
- if len(pmsg.Emotes) == 0 ||
- strings.Contains(pmsg.Emotes[0].Name, ")") ||
- strings.Contains(pmsg.Emotes[0].Name, "(") ||
- strings.Contains(pmsg.Emotes[0].Name, "<") ||
- strings.Contains(pmsg.Emotes[0].Name, ">") ||
- strings.Contains(pmsg.Emotes[0].Name, "/") ||
- strings.Contains(pmsg.Emotes[0].Name, "\\") ||
- strings.Contains(pmsg.Emotes[0].Name, ";") ||
- strings.Contains(pmsg.Emotes[0].Name, ":") {
- return
- }
- id, err := strconv.Atoi(pmsg.Emotes[0].ID)
- if err != nil {
- log.Println(err)
- }
- if !stringInSlice(id, globalTwitchEmotes) {
- return
- }
- e, exists := bot.epm[pmsg.Emotes[0].Name]
- if exists {
- e.Emotes++
- bot.epm[pmsg.Emotes[0].Name] = e
- } else {
- bot.epm[pmsg.Emotes[0].Name] = EPMEmote{
- Channel: pmsg.Channel,
- EmoteID: pmsg.Emotes[0].ID,
- EPM: 0,
- Emotes: 1,
- }
- }
- e, _ = bot.epm[pmsg.Emotes[0].Name]
- bot.updateRedisScore(pmsg.Channel, pmsg.Emotes[0].Name, pmsg.Emotes[0].ID, e.EPM)
- }
- func (bot *Bot) calculateEPM() {
- for _, emote := range bot.epm {
- // log.Println(emote.EmoteID)
- // log.Println("inert", emote.EPM)
- // emote.EPM = (emote.EPM + (emote.Emotes * 60)) / 2
- // emote.Emotes = 0
- // log.Println(emote.EPM)
- log.Println(emote.EPM)
- emote.EPM = 123
- log.Println(emote.EPM)
- }
- }
- func (bot *Bot) joinChannel(channel string) {
- log.Println("joining", channel)
- fmt.Fprintf(bot.conn, "JOIN %s\r\n", channel)
- }
- // TopChannels top channels on twitch
- type TopChannels struct {
- Streams []Stream `json:"streams"`
- }
- // Stream single stream object
- type Stream struct {
- Channel struct {
- Name string `json:"name"`
- } `json:"channel"`
- }
- func (bot *Bot) joinTopChannels() {
- res, err := httpRequest("https://api.twitch.tv/kraken/streams?limit=100&offset=0")
- if err != nil {
- log.Panic(err)
- }
- var topChannels TopChannels
- json.Unmarshal(res, &topChannels)
- log.Println(topChannels.Streams[0].Channel.Name)
- for _, channel := range topChannels.Streams {
- go bot.joinChannel("#" + channel.Channel.Name)
- }
- }
- func (bot *Bot) syncEPM() {
- //log.Println("syncing EPM")
- for index, emote := range bot.epm {
- if emote.EPM < 3 {
- return
- }
- data := new(emoteData)
- data.Channel = emote.Channel
- data.Epm = emote.EPM
- data.Emote.Bttv = false
- data.Emote.EmoteCode = index
- var err error
- data.Emote.EmoteID, err = strconv.Atoi(emote.EmoteID)
- if err != nil {
- log.Println(err)
- continue
- }
- emitEmote(data)
- }
- }
- func (bot *Bot) updateRedisScore(channel string, emote string, emoteID string, score int) {
- res, err := rclient.HGet("spamchamp:records", emoteID).Result()
- if err != nil {
- rclient.HSet("spamchamp:records", emoteID, emote+"[:]"+channel+"[:]"+strconv.Itoa(score))
- } else {
- rscore, err := strconv.Atoi(strings.Split(res, "[:]")[2])
- if err != nil {
- log.Println(err)
- return
- }
- if score > rscore {
- rclient.HSet("spamchamp:records", emoteID, emote+"[:]"+channel+"[:]"+strconv.Itoa(score))
- }
- }
- }
- func serveRecords(w http.ResponseWriter, r *http.Request) {
- var records []emoteData
- res, err := rclient.HGetAll("spamchamp:records").Result()
- if err != nil {
- log.Println(err)
- return
- }
- for index, element := range res {
- split := strings.Split(element, "[:]")
- data := new(emoteData)
- data.Channel = split[1]
- data.Epm, err = strconv.Atoi(split[2])
- if err != nil {
- log.Println(err)
- continue
- }
- data.Emote.Bttv = false
- data.Emote.EmoteCode = split[0]
- var err error
- data.Emote.EmoteID, err = strconv.Atoi(index)
- if err != nil {
- log.Println(err)
- continue
- }
- records = append(records, *data)
- }
- json.NewEncoder(w).Encode(records)
- }
- // GlobalTwitchEmotes struct for twitchemotes data
- type GlobalTwitchEmotes struct {
- Emotes map[string]TwitchEmotesEmote `json:"emotes"`
- }
- // TwitchEmotesEmote single emote from twitch emotes
- type TwitchEmotesEmote struct {
- ImageID int `json:"image_id"`
- }
- func loadTwitchEmotes() {
- res, err := httpRequest("https://twitchemotes.com/api_cache/v2/global.json")
- if err != nil {
- log.Panic(err)
- }
- var globalEmotes GlobalTwitchEmotes
- json.Unmarshal(res, &globalEmotes)
- for _, emoteID := range globalEmotes.Emotes {
- globalTwitchEmotes = append(globalTwitchEmotes, emoteID.ImageID)
- }
- }
- func stringInSlice(a int, list []int) bool {
- for _, b := range list {
- if b == a {
- return true
- }
- }
- return false
- }
- func round(v float64, decimals int) float64 {
- var pow float64 = 1
- for i := 0; i < decimals; i++ {
- pow *= 10
- }
- return float64(int((v*pow)+0.5)) / pow
- }
Advertisement
Add Comment
Please, Sign In to add comment