diff --git a/coldbrewcrew/iworkout/discord.go b/coldbrewcrew/iworkout/discord.go index 4fb0d78..665fb03 100644 --- a/coldbrewcrew/iworkout/discord.go +++ b/coldbrewcrew/iworkout/discord.go @@ -17,18 +17,6 @@ const botToken = "NzA5Nzk3NTE4MDMyMjQwNzUx.XryNQQ.vx4n9mcdsJr8bFCz8pOgrr2YD0A" const iworkoutChannelID = "492391546411417620" -// In-memory caches of data retrieved through the API. -var ( - // users is a map of user ID to user. - users = map[string]discordgo.User{} - - // messages is a two-dimensional map of channel ID to message ID to message. - messages = map[string]map[string]discordgo.Message{} - - // reactions is a map of message ID to a "set" of users who reacted to that message (with any reaction). - reactions = map[string]map[string]struct{}{} -) - // init is called automatically on boot. func init() { discord, err := discordgo.New(fmt.Sprintf("Bot %s", botToken)) @@ -57,7 +45,7 @@ func handleReactionAdd(s *discordgo.Session, r *discordgo.MessageReactionAdd) { reactions[m.ID][u.ID] = struct{}{} - fmt.Printf("%s reaction added: \"%s\" had a %s added by %v\n", logPrefix, m.Content, r.Emoji.Name, u) + fmt.Printf("%s reaction added: \"%s\" had a %s added by %v\n", logPrefix, m.Content, r.Emoji.Name, u.Username) } // handleReactionRemove is called by discordgo when an emoji reaction is removed from a message. @@ -67,7 +55,7 @@ func handleReactionRemove(s *discordgo.Session, r *discordgo.MessageReactionRemo delete(reactions[m.ID], u.ID) - fmt.Printf("%s reaction removed: \"%s\" had a %s removed by %v\n", logPrefix, m.Content, r.Emoji.Name, u) + fmt.Printf("%s reaction removed: \"%s\" had a %s removed by %v\n", logPrefix, m.Content, r.Emoji.Name, u.Username) } // fetchUser gets a user through the Discord API by ID and caches it, if it is not already stored in the local cache. @@ -130,6 +118,7 @@ func buildMessagesState(s *discordgo.Session, channelID string) { } messages[msg.ChannelID][msg.ID] = *msg + users[msg.Author.ID] = *msg.Author if earliest.ID == "" { earliest = *msg diff --git a/coldbrewcrew/iworkout/message.go b/coldbrewcrew/iworkout/message.go new file mode 100644 index 0000000..e086188 --- /dev/null +++ b/coldbrewcrew/iworkout/message.go @@ -0,0 +1,11 @@ +package iworkout + +import "github.com/bwmarrin/discordgo" + +// messages is a two-dimensional map of channel ID to message ID to message. +var messages = map[string]map[string]discordgo.Message{} + +// Messages returns all messages we've catalogued as a map of message ID to message. +func Messages() map[string]discordgo.Message { + return messages[iworkoutChannelID] +} diff --git a/coldbrewcrew/iworkout/reaction.go b/coldbrewcrew/iworkout/reaction.go new file mode 100644 index 0000000..0843f45 --- /dev/null +++ b/coldbrewcrew/iworkout/reaction.go @@ -0,0 +1,9 @@ +package iworkout + +// reactions is a map of message ID to a "set" of user IDs who reacted to that message (with any reaction). +var reactions = map[string]map[string]struct{}{} + +// Reactions returns all reactions we've catalogued as a map of message ID to a "set" of user IDs who have reacted to that message (with any emoji). +func Reactions() map[string]map[string]struct{} { + return reactions +} diff --git a/coldbrewcrew/iworkout/user.go b/coldbrewcrew/iworkout/user.go new file mode 100644 index 0000000..49cb72c --- /dev/null +++ b/coldbrewcrew/iworkout/user.go @@ -0,0 +1,11 @@ +package iworkout + +import "github.com/bwmarrin/discordgo" + +// users is a map of user ID to user. +var users = map[string]discordgo.User{} + +// Users returns all users we've catalogued as a map of user ID to user. +func Users() map[string]discordgo.User { + return users +} diff --git a/html/iworkout.html b/html/iworkout.html new file mode 100644 index 0000000..39c794a --- /dev/null +++ b/html/iworkout.html @@ -0,0 +1,109 @@ + + + + twos.dev + + + + + +
+

#i-work-out standings

+ +

2020-05

+ +

+ Calculated from reaction emojis in Discord. +

+ + + + {{end}} + + + {{range $messageID, $userIDSet := .Reactions}} + {{if $userIDSet}} + + + {{range $.Users}} + + {{end}} + + {{end}} + {{end}} +
+ {{range .Users}}{{.Username}}
{{(index $.Messages $messageID).Content}} + {{if (index $userIDSet .ID)}} + X + {{end}} +
+ +
+ +
+ Index
+ github.com/glacials
+ twitter.com/glcls
+ ben@twos.dev +
+ + + \ No newline at end of file diff --git a/main.go b/main.go index 30647c6..6dcbb63 100644 --- a/main.go +++ b/main.go @@ -2,22 +2,27 @@ package main import ( "fmt" + "html/template" "io/ioutil" "log" "net/http" "time" + "github.com/bwmarrin/discordgo" + "github.com/glacials/mainframe/coldbrewcrew/iworkout" _ "github.com/glacials/mainframe/coldbrewcrew/iworkout" ) const startMsg = "Starting mainframe..." const port = 9000 -const refreshInterval = 2 * time.Second +const refreshInterval = 1 * time.Second + +var iworkoutTemplate *template.Template func main() { var ( - index []byte - err error + index, iworkoutFile []byte + err error ) go func() { for { @@ -27,6 +32,23 @@ func main() { return } + iworkoutFile, err = ioutil.ReadFile("html/iworkout.html") + if err != nil { + log.Fatalf("Cannot read html/index.html: %s", err) + return + } + + iworkoutTemplate = template.New("iworkout") + iworkoutTemplate = iworkoutTemplate.Funcs(map[string]interface{}{ + "AddOne": AddOne, + }) + + iworkoutTemplate, err = iworkoutTemplate.Parse(string(iworkoutFile)) + if err != nil { + log.Printf("Cannot parse template html/iworkout.html: %s", err) + return + } + time.Sleep(refreshInterval) } }() @@ -35,6 +57,32 @@ func main() { w.Write(index) }) + type IworkoutParams struct { + // Users is a map of all users we've catalogued, as a map of user ID to user. + Users map[string]discordgo.User + + // Messages is a map of message IDs to messages. + Messages map[string]discordgo.Message + + // Reactions is a map of message IDs to users who reacted to that message. + Reactions map[string]map[string]struct{} + } + + http.HandleFunc("/iworkout", func(w http.ResponseWriter, r *http.Request) { + params := IworkoutParams{ + Users: iworkout.Users(), + Reactions: iworkout.Reactions(), + Messages: iworkout.Messages(), + } + if err := iworkoutTemplate.Execute(w, params); err != nil { + log.Printf("Error executing template: %s", err) + } + }) + fmt.Printf("%s listening on %s:%d.\n", startMsg, "localhost", port) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) } + +func AddOne(i int) int { + return i + 1 +}