hacked poc for edits as an animation mechanism

This commit is contained in:
Scott Kiesel 2017-10-31 06:22:36 -04:00
parent 5c1b1abbf1
commit 34e2404e8b
9 changed files with 175 additions and 11 deletions

View File

@ -110,6 +110,10 @@ func (b *bot) DB() *sqlx.DB {
return b.db return b.db
} }
func (b *bot) Conn() Connector {
return b.conn
}
// Create any tables if necessary based on version of DB // Create any tables if necessary based on version of DB
// Plugins should create their own tables, these are only for official bot stuff // Plugins should create their own tables, these are only for official bot stuff
// Note: This does not return an error. Database issues are all fatal at this stage. // Note: This does not return an error. Database issues are all fatal at this stage.
@ -145,7 +149,7 @@ func (b *bot) migrateDB() {
// Adds a constructed handler to the bots handlers list // Adds a constructed handler to the bots handlers list
func (b *bot) AddHandler(name string, h Handler) { func (b *bot) AddHandler(name string, h Handler) {
b.plugins[strings.ToLower(name)] = h b.plugins[name] = h
b.pluginOrdering = append(b.pluginOrdering, name) b.pluginOrdering = append(b.pluginOrdering, name)
if entry := h.RegisterWeb(); entry != nil { if entry := h.RegisterWeb(); entry != nil {
b.httpEndPoints[name] = *entry b.httpEndPoints[name] = *entry

View File

@ -23,6 +23,8 @@ func (b *bot) MsgReceived(msg msg.Message) {
// msg := b.buildMessage(client, inMsg) // msg := b.buildMessage(client, inMsg)
// do need to look up user and fix it // do need to look up user and fix it
log.Println(msg.User.Name)
if strings.HasPrefix(msg.Body, "help ") && msg.Command { if strings.HasPrefix(msg.Body, "help ") && msg.Command {
parts := strings.Fields(strings.ToLower(msg.Body)) parts := strings.Fields(strings.ToLower(msg.Body))
b.checkHelp(msg.Channel, parts) b.checkHelp(msg.Channel, parts)
@ -65,6 +67,10 @@ func (b *bot) React(channel, reaction string, message msg.Message) {
b.conn.React(channel, reaction, message) b.conn.React(channel, reaction, message)
} }
func (b *bot) Edit(channel, newMessage, identifier string) {
b.conn.Edit(channel, newMessage, identifier)
}
func (b *bot) GetEmojiList() map[string]string { func (b *bot) GetEmojiList() map[string]string {
return b.conn.GetEmojiList() return b.conn.GetEmojiList()
} }

View File

@ -13,11 +13,13 @@ type Bot interface {
Config() *config.Config Config() *config.Config
DBVersion() int64 DBVersion() int64
DB() *sqlx.DB DB() *sqlx.DB
Conn() Connector
Who(string) []user.User Who(string) []user.User
AddHandler(string, Handler) AddHandler(string, Handler)
SendMessage(string, string) SendMessage(string, string)
SendAction(string, string) SendAction(string, string)
React(string, string, msg.Message) React(string, string, msg.Message)
Edit(string, string, string)
MsgReceived(msg.Message) MsgReceived(msg.Message)
EventReceived(msg.Message) EventReceived(msg.Message)
Filter(msg.Message, string) string Filter(msg.Message, string) string
@ -34,6 +36,7 @@ type Connector interface {
SendMessage(channel, message string) SendMessage(channel, message string)
SendAction(channel, message string) SendAction(channel, message string)
React(string, string, msg.Message) React(string, string, msg.Message)
Edit(string, string, string)
GetEmojiList() map[string]string GetEmojiList() map[string]string
Serve() error Serve() error

View File

@ -25,6 +25,7 @@ type MockBot struct {
func (mb *MockBot) Config() *config.Config { return &mb.Cfg } func (mb *MockBot) Config() *config.Config { return &mb.Cfg }
func (mb *MockBot) DBVersion() int64 { return 1 } func (mb *MockBot) DBVersion() int64 { return 1 }
func (mb *MockBot) DB() *sqlx.DB { return mb.db } func (mb *MockBot) DB() *sqlx.DB { return mb.db }
func (mb *MockBot) Conn() Connector { return nil }
func (mb *MockBot) Who(string) []user.User { return []user.User{} } func (mb *MockBot) Who(string) []user.User { return []user.User{} }
func (mb *MockBot) AddHandler(name string, f Handler) {} func (mb *MockBot) AddHandler(name string, f Handler) {}
func (mb *MockBot) SendMessage(ch string, msg string) { func (mb *MockBot) SendMessage(ch string, msg string) {
@ -40,6 +41,7 @@ func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Mess
func (mb *MockBot) CheckAdmin(nick string) bool { return false } func (mb *MockBot) CheckAdmin(nick string) bool { return false }
func (mb *MockBot) React(channel, reaction string, message msg.Message) {} func (mb *MockBot) React(channel, reaction string, message msg.Message) {}
func (mb *MockBot) Edit(channel, newMessage, identifier string) {}
func (mb *MockBot) GetEmojiList() map[string]string { return make(map[string]string) } func (mb *MockBot) GetEmojiList() map[string]string { return make(map[string]string) }
func (mb *MockBot) RegisterFilter(s string, f func(string) string) {} func (mb *MockBot) RegisterFilter(s string, f func(string) string) {}

View File

@ -103,6 +103,10 @@ func (i *Irc) React(channel, reaction string, message msg.Message) {
//we're not goign to do anything because it's IRC //we're not goign to do anything because it's IRC
} }
func (i *Irc) Edit(channel, newMessage, identifier string) {
//we're not goign to do anything because it's IRC
}
func (i *Irc) GetEmojiList() map[string]string { func (i *Irc) GetEmojiList() map[string]string {
//we're not goign to do anything because it's IRC //we're not goign to do anything because it's IRC
return make(map[string]string) return make(map[string]string)

View File

@ -21,6 +21,7 @@ import (
"github.com/velour/catbase/plugins/leftpad" "github.com/velour/catbase/plugins/leftpad"
"github.com/velour/catbase/plugins/reaction" "github.com/velour/catbase/plugins/reaction"
"github.com/velour/catbase/plugins/reminder" "github.com/velour/catbase/plugins/reminder"
"github.com/velour/catbase/plugins/rpgORdie"
"github.com/velour/catbase/plugins/rss" "github.com/velour/catbase/plugins/rss"
"github.com/velour/catbase/plugins/stats" "github.com/velour/catbase/plugins/stats"
"github.com/velour/catbase/plugins/talker" "github.com/velour/catbase/plugins/talker"
@ -69,6 +70,7 @@ func main() {
b.AddHandler("emojifyme", emojifyme.New(b)) b.AddHandler("emojifyme", emojifyme.New(b))
b.AddHandler("twitch", twitch.New(b)) b.AddHandler("twitch", twitch.New(b))
b.AddHandler("inventory", inventory.New(b)) b.AddHandler("inventory", inventory.New(b))
b.AddHandler("rpgORdie", rpgORdie.New(b))
// catches anything left, will always return true // catches anything left, will always return true
b.AddHandler("factoid", fact.New(b)) b.AddHandler("factoid", fact.New(b))

View File

@ -0,0 +1,61 @@
package rpgORdie
import (
"strings"
// "log"
"time"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/slack"
)
type RPGPlugin struct {
Bot bot.Bot
Slack *slack.Slack //nasty
}
func New(b bot.Bot) *RPGPlugin {
return &RPGPlugin{
Bot: b,
Slack: b.Conn().(*slack.Slack), //oh boy, this is just filthy
}
}
func (p *RPGPlugin) Message(message msg.Message) bool {
if strings.ToLower(message.Body) == "start rpg" {
p.Bot.SendMessage(message.Channel, "I'll edit this.")
ts := p.Slack.GetLastMessageId()
time.Sleep(2 * time.Second)
edited := ""
for i := 0; i <= 5; i++ {
p.Bot.Edit(message.Channel, edited, ts)
edited += ":fire:"
time.Sleep(2 * time.Second)
}
p.Bot.Edit(message.Channel, "HECK YES", ts)
}
return false
}
func (p *RPGPlugin) LoadData() {
}
func (p *RPGPlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, "Go find a walkthrough or something.")
}
func (p *RPGPlugin) Event(kind string, message msg.Message) bool {
return false
}
func (p *RPGPlugin) BotMessage(message msg.Message) bool {
return false
}
func (p *RPGPlugin) RegisterWeb() *string {
return nil
}

View File

@ -0,0 +1,4 @@
package rpgORdie
import (
)

View File

@ -15,7 +15,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync/atomic" // "sync/atomic"
"time" "time"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
@ -38,6 +38,8 @@ type Slack struct {
emoji map[string]string emoji map[string]string
lastMessageId string
eventReceived func(msg.Message) eventReceived func(msg.Message)
messageReceived func(msg.Message) messageReceived func(msg.Message)
} }
@ -160,6 +162,7 @@ func New(c *config.Config) *Slack {
lastRecieved: time.Now(), lastRecieved: time.Now(),
users: make(map[string]string), users: make(map[string]string),
emoji: make(map[string]string), emoji: make(map[string]string),
lastMessageId: "",
} }
} }
@ -172,17 +175,47 @@ func (s *Slack) RegisterMessageReceived(f func(msg.Message)) {
} }
func (s *Slack) SendMessageType(channel, messageType, subType, message string) error { func (s *Slack) SendMessageType(channel, messageType, subType, message string) error {
m := slackMessage{ resp, err := http.PostForm("https://slack.com/api/chat.postMessage",
ID: atomic.AddUint64(&idCounter, 1), url.Values{"token": {s.config.Slack.Token},
Type: messageType, "channel": {channel},
SubType: subType, "text": {message},
Channel: channel, })
Text: message,
}
err := websocket.JSON.Send(s.ws, m)
if err != nil { if err != nil {
log.Printf("Error sending Slack message: %s", err) log.Printf("Error sending Slack reaction: %s", err)
} }
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatalf("Error reading Slack API body: %s", err)
}
log.Println(string(body))
type MessageResponse struct {
OK bool `json:ok`
Channel string `json:channel`
Timestamp string `json:ts`
}
var mr MessageResponse
err = json.Unmarshal(body, &mr)
if err != nil {
log.Fatalf("Error parsing message response: %s", err)
}
bodyAsString := string(body)
//what the fuck
if strings.Contains(bodyAsString, "\"ts\":\"") {
mr.Timestamp = strings.Split(strings.Split(bodyAsString, "\"ts\":\"")[1], "\"")[0]
}
s.lastMessageId = mr.Timestamp
log.Println(mr)
return err return err
} }
@ -209,6 +242,46 @@ func (s *Slack) React(channel, reaction string, message msg.Message) {
log.Print(resp) log.Print(resp)
} }
func (s* Slack) GetLastMessageId() string {
return s.lastMessageId
}
func (s* Slack) PrintHistory(channel string, howMany int) {
resp, err := http.PostForm("https://slack.com/api/channels.history",
url.Values{"token": {s.config.Slack.Token},
"channel": {channel},
"count": {fmt.Sprintf("%d", howMany)}})
if err != nil {
log.Printf("Error getting slack history: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatalf("Error reading Slack API body: %s", err)
}
log.Println(string(body))
}
func (s *Slack) Edit(channel, newMessage, identifier string) {
log.Printf("Editing in (%s) %s: %s", identifier, channel, newMessage)
resp, err := http.PostForm("https://slack.com/api/chat.update",
url.Values{"token": {s.config.Slack.Token},
"channel": {channel},
"text": {newMessage},
"ts": {identifier}})
if err != nil {
log.Printf("Error sending Slack reaction: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatalf("Error reading Slack API body: %s", err)
}
log.Println(string(body))
}
func (s *Slack) GetEmojiList() map[string]string { func (s *Slack) GetEmojiList() map[string]string {
return s.emoji return s.emoji
} }
@ -275,6 +348,11 @@ func (s *Slack) Serve() error {
case "message": case "message":
if !msg.Hidden { if !msg.Hidden {
m := s.buildMessage(msg) m := s.buildMessage(msg)
log.Println()
log.Println(m)
log.Println()
if m.Time.Before(s.lastRecieved) { if m.Time.Before(s.lastRecieved) {
log.Printf("Ignoring message: %+v\nlastRecieved: %v msg: %v", msg.ID, s.lastRecieved, m.Time) log.Printf("Ignoring message: %+v\nlastRecieved: %v msg: %v", msg.ID, s.lastRecieved, m.Time)
} else { } else {