Rework lysenko to use latest hugot, removing go-ircevent

This commit is contained in:
2016-10-14 23:34:58 +01:00
parent c093390efa
commit 62eaba8408
3 changed files with 44 additions and 93 deletions

View File

@@ -4,10 +4,10 @@
package deadline
import (
"context"
"time"
"github.com/tcolgate/hugot"
"golang.org/x/net/context"
)
type HearsDeadline struct {

View File

@@ -3,24 +3,15 @@
package quotedb
import (
"context"
"fmt"
"log"
"math/rand"
"regexp"
"sort"
"strconv"
"strings"
"github.com/tcolgate/hugot"
"golang.org/x/net/context"
)
var (
hearAddQuote = regexp.MustCompile(`^\!addquote (.*)$`)
hearLastQuote = regexp.MustCompile(`^\!lastquote$`)
hearFindQuote = regexp.MustCompile(`^\!findquote (.*)$`)
hearRandQuote = regexp.MustCompile(`^\!randquote$`)
hearQuote = regexp.MustCompile(`^\!quote (\d+)$`)
)
type AddQuoteHandler struct {
@@ -47,55 +38,41 @@ func (h *AddQuoteHandler) Describe() (string, string) {
return "addquote", "Add a quote to the database"
}
func (h *AddQuoteHandler) Hears() *regexp.Regexp {
return hearAddQuote
}
func (h *AddQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
if len(submatches) != 1 || len(submatches[0]) != 2 {
log.Printf("Submatches weird! %+v", submatches)
return
func (h *AddQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 {
return nil
}
var rsp string
quote, err := h.QuoteDB.AddQuote(m.Channel, submatches[0][1], m.From)
quote, err := h.QuoteDB.AddQuote(m.Channel, parts[1], m.From)
if err != nil {
log.Printf("%s: AddQuote error: %v", m.Channel, err)
rsp = "Failed to add quote, sorry!"
} else {
rsp = fmt.Sprintf("Quote %d added", quote.QuoteId)
return fmt.Errorf("Failed to add quote, sorry!")
}
w.Send(ctx, m.Reply(rsp))
w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d added", quote.QuoteId)))
return nil
}
func (h *LastQuoteHandler) Describe() (string, string) {
return "lastquote", "Show the last-added quote"
}
func (h *LastQuoteHandler) Hears() *regexp.Regexp {
return hearLastQuote
}
func (h *LastQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
func (h *LastQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
quote, err := h.QuoteDB.FindLastQuote(m.Channel)
if err != nil {
log.Printf("%s: Error in LastQuote: %v", m.Channel, err)
return
return nil
}
rsp := fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data)
w.Send(ctx, m.Reply(rsp))
w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data)))
return nil
}
func (h *FindQuoteHandler) Describe() (string, string) {
return "findquote", "Search for a quote by substring"
}
func (h *FindQuoteHandler) Hears() *regexp.Regexp {
return hearFindQuote
}
func (h *FindQuoteHandler) find(channel, term string) string {
quotes, err := h.QuoteDB.FindQuotesByString(term, channel)
if err != nil {
@@ -140,43 +117,35 @@ func (h *FindQuoteHandler) find(channel, term string) string {
return fmt.Sprintf("%s matches. IDs: %s. Most recent: %s: %s", quoteCount, idStr, quote.Author, quote.Data)
}
func (h *FindQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
if len(submatches) != 1 || len(submatches[0]) != 2 {
log.Printf("Submatches weird! %+v", submatches)
return
func (h *FindQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 {
return nil
}
rsp := h.find(m.Channel, submatches[0][1])
w.Send(ctx, m.Reply(rsp))
w.Send(ctx, m.Reply(h.find(m.Channel, parts[1])))
return nil
}
func (h *RandQuoteHandler) Describe() (string, string) {
return "randquote", "Show a random quote"
}
func (h *RandQuoteHandler) Hears() *regexp.Regexp {
return hearRandQuote
}
func (h *RandQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
func (h *RandQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
quote, err := h.QuoteDB.FindRandomQuote(m.Channel)
if err != nil {
log.Printf("%s: Error in RandQuote: %v", m.Channel, err)
return
return nil
}
rsp := fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data)
w.Send(ctx, m.Reply(rsp))
w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data)))
return nil
}
func (h *QuoteHandler) Describe() (string, string) {
return "quote", "show a specific quote, identified by ID"
}
func (h *QuoteHandler) Hears() *regexp.Regexp {
return hearQuote
}
func (h *QuoteHandler) show(channel, qid string) string {
id, err := strconv.Atoi(qid)
if err != nil {
@@ -200,12 +169,12 @@ func (h *QuoteHandler) show(channel, qid string) string {
return fmt.Sprintf("%s: %s", quote.Author, quote.Data)
}
func (h *QuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
if len(submatches) != 1 || len(submatches[0]) != 2 {
log.Printf("Submatches weird! %+v", submatches)
return
func (h *QuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 {
return nil
}
rsp := h.show(m.Channel, submatches[0][1])
w.Send(ctx, m.Reply(rsp))
w.Send(ctx, m.Reply(h.show(m.Channel, parts[1])))
return nil
}