func (m Message) String() string {
return irc.Message(m).String()
}
+
+// Returns the tag of the corresponding tag key, or nil if not found
+func (m Message) GetTag(key string) *irc.Tag {
+ for _, tag := range m.Tags {
+ if tag.Key == key {
+ return &tag
+ }
+ }
+ return nil
+}
+++ /dev/null
-package messages
-
-import (
- "twitchchat/twitch/core/commands"
-)
-
-type PrivMsg Message
-
-func IsPrivateMessage(m Message) bool {
- return string(m.Command) == commands.PrivMsg
-}
-
-func (p PrivMsg) User() string {
- return p.Prefix.Name
-}
-
-func (p PrivMsg) Text() string {
- return p.Params.Trailing
-}
--- /dev/null
+package privmsg
+
+import (
+ "twitchchat/irc"
+ "twitchchat/twitch/core/commands"
+ "twitchchat/twitch/core/messages"
+)
+
+type PrivMsg messages.Message
+
+//go:generate tagprototype -out "privmsgtags.go" -type PrivMsgTag -prototype "@badge-info=<badge-info>;badges=<badges>;bits=<bits>client-nonce=<nonce>;color=<color>;display-name=<display-name>;emotes=<emotes>;first-msg=<first-msg>;flags=<flags>;id=<msg-id>;mod=<mod>;room-id=<room-id>;subscriber=<subscriber>;tmi-sent-ts=<timestamp>;turbo=<turbo>;user-id=<user-id>;user-type=<user-type>;reply-parent-msg-id=<reply-parent-msg-id>;reply-parent-user-id=<reply-parent-user-id>;reply-parent-user-login=<reply-parent-user-login>;reply-parent-display-name=<reply-parent-display-name>;reply-parent-msg-body=<reply-parent-msg-body>;reply-thread-parent-msg-id=<reply-thread-parent-msg-id>;reply-thread-parent-user-login=<reply-thread-parent-user-login>;vip=<vip>" -package privmsg
+
+func IsPrivateMessage(m messages.Message) bool {
+ return string(m.Command) == commands.PrivMsg
+}
+
+func (p PrivMsg) User() string {
+ return p.Prefix.Name
+}
+
+func (p PrivMsg) Text() string {
+ return p.Params.Trailing
+}
+
+func (p PrivMsg) GetTag(key PrivMsgTag) *irc.Tag {
+ return messages.Message(p).GetTag(string(key))
+}
--- /dev/null
+//Generated by twitch-chat/x/generate/tagprototype
+
+package privmsg
+
+type PrivMsgTag string
+
+const (
+ BadgeInfo PrivMsgTag = "badge-info"
+ Badges PrivMsgTag = "badges"
+ Bits PrivMsgTag = "bits"
+ Color PrivMsgTag = "color"
+ DisplayName PrivMsgTag = "display-name"
+ Emotes PrivMsgTag = "emotes"
+ FirstMsg PrivMsgTag = "first-msg"
+ Flags PrivMsgTag = "flags"
+ Id PrivMsgTag = "id"
+ Mod PrivMsgTag = "mod"
+ RoomId PrivMsgTag = "room-id"
+ Subscriber PrivMsgTag = "subscriber"
+ TmiSentTs PrivMsgTag = "tmi-sent-ts"
+ Turbo PrivMsgTag = "turbo"
+ UserId PrivMsgTag = "user-id"
+ UserType PrivMsgTag = "user-type"
+ ReplyParentMsgId PrivMsgTag = "reply-parent-msg-id"
+ ReplyParentUserId PrivMsgTag = "reply-parent-user-id"
+ ReplyParentUserLogin PrivMsgTag = "reply-parent-user-login"
+ ReplyParentDisplayName PrivMsgTag = "reply-parent-display-name"
+ ReplyParentMsgBody PrivMsgTag = "reply-parent-msg-body"
+ ReplyThreadParentMsgId PrivMsgTag = "reply-thread-parent-msg-id"
+ ReplyThreadParentUserLogin PrivMsgTag = "reply-thread-parent-user-login"
+ Vip PrivMsgTag = "vip"
+)
"strings"
"twitchchat/twitch/core"
"twitchchat/twitch/core/messages"
+ "twitchchat/twitch/core/privmsg"
)
var channel = flag.String("channel", "fl_gui", "channel to read")
var format string
rmax := NewRunningMax(50)
for m := range msgs {
- if messages.IsPrivateMessage(m) {
- priv := messages.PrivMsg(m)
+ if privmsg.IsPrivateMessage(m) {
+ priv := privmsg.PrivMsg(m)
u := priv.User()
mx := rmax.Push(len(u))
format = fmt.Sprintf("%%%ds: %%s\n", mx)
--- /dev/null
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "strings"
+ "unicode"
+)
+
+var prototypeString = flag.String("prototype", "", "The prototype of tags in the form of\n@tag-name-1=<tag-value-1>;tag-name-2=<tag-value-2>;...")
+var outFile = flag.String("out", "", "File to write to")
+var ofType = flag.String("type", "", "Type to wrap tag values in")
+var packageName = flag.String("package", "", "package for the file")
+
+func extractTagsFromPrototype() []string {
+ // trim leading "@", and split into key=<value>
+ parts := strings.Split((*prototypeString)[1:], ";")
+ res := make([]string, len(parts))
+ for i, p := range parts {
+ res[i] = p[0:strings.Index(p, "=")]
+ }
+
+ return res
+}
+
+var bob = strings.Builder{}
+
+func tagToName(t string) string {
+ bob.Reset()
+ var turnNextUpper = true
+ for _, r := range t {
+ if turnNextUpper {
+ bob.WriteRune(unicode.ToUpper(r))
+ turnNextUpper = false
+ } else if r == '-' {
+ turnNextUpper = true
+ } else {
+ bob.WriteRune(r)
+ }
+ }
+
+ return bob.String()
+}
+
+func main() {
+ flag.Parse()
+
+ // required arguments
+ if prototypeString == nil || outFile == nil || packageName == nil {
+ flag.Usage()
+ return
+ }
+
+ f, err := os.OpenFile(*outFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0755)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ }
+
+ fmt.Fprintf(f, "//Generated by twitch-chat/x/generate/tagprototype\n")
+ fmt.Fprintf(f, "\n")
+ fmt.Fprintf(f, "package %s\n", *packageName)
+ fmt.Fprintf(f, "\n")
+
+ var typ = "string"
+ if *ofType != "" {
+ fmt.Fprintf(f, "type %s string\n", *ofType)
+ fmt.Fprintf(f, "\n")
+ typ = *ofType
+ }
+
+ fmt.Fprintf(f, "const (\n")
+ for _, t := range extractTagsFromPrototype() {
+ fmt.Fprintf(f, "\t%s %s = %#v\n", tagToName(t), typ, t)
+ }
+ fmt.Fprintf(f, ")\n")
+}