Skip to content

Commit

Permalink
extract bot ID and pre-process message event
Browse files Browse the repository at this point in the history
  • Loading branch information
maribowman committed Jan 6, 2024
1 parent 3ceea28 commit 4c82070
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 62 deletions.
4 changes: 2 additions & 2 deletions app/service/discord_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func NewDiscordBot() model.DiscordBot {
bot := DiscordBot{
session: session,
handlers: map[string]model.BotHandler{
config.Config.Discord.Channels[GroceriesChannel]: NewGroceryHandler(config.Config.Discord.BotID, config.Config.Discord.Channels[GroceriesChannel]),
config.Config.Discord.Channels[TkGoodsChannel]: NewTkHandler(config.Config.Discord.BotID, config.Config.Discord.Channels[TkGoodsChannel]),
config.Config.Discord.Channels[GroceriesChannel]: NewGroceryHandler(config.Config.Discord.Channels[GroceriesChannel]),
config.Config.Discord.Channels[TkGoodsChannel]: NewTkHandler(config.Config.Discord.Channels[TkGoodsChannel]),
},
}

Expand Down
34 changes: 5 additions & 29 deletions app/service/grocery_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (
)

type GroceryHandler struct {
botID string
channelID string
shoppingList []model.PantryItem
lastShoppingList string
}

func NewGroceryHandler(botID string, channelID string) model.BotHandler {
func NewGroceryHandler(channelID string) model.BotHandler {
log.Debug().Msg("Registering grocery handler")
return &GroceryHandler{
botID: botID,
channelID: channelID,
}
}
Expand All @@ -28,51 +26,29 @@ func (handler *GroceryHandler) ReadyEvent(session *discordgo.Session, ready *dis
}

func (handler *GroceryHandler) MessageEvent(session *discordgo.Session, message *discordgo.MessageCreate) {
channelMessages, err := session.ChannelMessages(handler.channelID, 100, "", "", "")
items, lastBotMessageID, content, removableMessageIDs, err := PreProcessMessageEvent(session, message)
if err != nil {
return
}

var lastBotMessage *discordgo.Message
var content string
var removableMessageIDs []string

for _, msg := range channelMessages {
if msg.Author.ID == handler.botID {
if lastBotMessage == nil {
lastBotMessage = msg
handler.shoppingList = model.FromMarkdownTable(msg.Content)
continue
} else if lastBotMessage.Timestamp.After(msg.Timestamp) {
removableMessageIDs = append(removableMessageIDs, lastBotMessage.ID)
lastBotMessage = msg
handler.shoppingList = model.FromMarkdownTable(msg.Content)
continue
}
} else {
content += "\n" + msg.Content
}
removableMessageIDs = append(removableMessageIDs, msg.ID)
}

for _, line := range strings.Split(content, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}

if removeRegex.MatchString(line) {
handler.shoppingList = Remove(handler.shoppingList, line)
handler.shoppingList = Remove(items, line)
} else {
handler.shoppingList = Add(handler.shoppingList, line)
handler.shoppingList = Add(items, line)
}
}

if err := session.ChannelMessagesBulkDelete(handler.channelID, removableMessageIDs); err != nil {
log.Error().Err(err).Msg("Could not bulk delete channel messages")
}

PublishItems(handler.shoppingList, session, lastBotMessage.ChannelID, lastBotMessage.ID)
PublishItems(handler.shoppingList, session, handler.channelID, lastBotMessageID)
}

func (handler *GroceryHandler) MessageComponentInteractionEvent(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
Expand Down
40 changes: 40 additions & 0 deletions app/service/pantry_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"github.com/bwmarrin/discordgo"
"github.com/maribowman/roastbeef-swag/app/config"
"github.com/maribowman/roastbeef-swag/app/model"
"github.com/rs/zerolog/log"
"regexp"
Expand All @@ -27,6 +28,45 @@ var (
trailingQuantity = regexp.MustCompile(`\s(\d+)$`)
)

func PreProcessMessageEvent(
session *discordgo.Session,
message *discordgo.MessageCreate,
) (
items []model.PantryItem,
lastBotMessageID string,
content string,
removableMessageIDs []string,
err error,
) {
channelMessages, err_ := session.ChannelMessages(message.ChannelID, 100, "", "", "")
if err_ != nil {
err = err_
return
}

var lastBotMessage *discordgo.Message
for _, msg := range channelMessages {
if msg.Author.ID == config.Config.Discord.BotID {
if lastBotMessage == nil {
lastBotMessage = msg
lastBotMessageID = msg.ID
items = model.FromMarkdownTable(msg.Content)
continue
} else if lastBotMessage.Timestamp.After(msg.Timestamp) {
removableMessageIDs = append(removableMessageIDs, lastBotMessageID) // remove previous bot msg
lastBotMessage = msg
lastBotMessageID = msg.ID
items = model.FromMarkdownTable(msg.Content)
continue
}
} else {
content += "\n" + msg.Content
}
removableMessageIDs = append(removableMessageIDs, msg.ID)
}
return
}

func Remove(items []model.PantryItem, line string) []model.PantryItem {
result := make([]model.PantryItem, 0)
removeAllExcept := false
Expand Down
38 changes: 7 additions & 31 deletions app/service/tk_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,47 @@ import (
)

type TkHandler struct {
botID string
channelID string
inventory []model.PantryItem
lastInventory string
}

func NewTkHandler(botID string, channelID string) model.BotHandler {
func NewTkHandler(channelID string) model.BotHandler {
log.Debug().Msg("Registering tk handler")
return &TkHandler{
botID: botID,
channelID: channelID,
}
}

func (handler *TkHandler) ReadyEvent(session *discordgo.Session, ready *discordgo.Ready) {
handler.MessageEvent(session, &discordgo.MessageCreate{Message: &discordgo.Message{Author: &discordgo.User{ID: "init"}}})
log.Debug().Msg("Initialized grocery handler")
log.Debug().Msg("Initialized tk handler")
}

func (handler *TkHandler) MessageEvent(session *discordgo.Session, message *discordgo.MessageCreate) {
channelMessages, err := session.ChannelMessages(message.ChannelID, 100, "", "", "")
items, lastBotMessageID, content, removableMessageIDs, err := PreProcessMessageEvent(session, message)
if err != nil {
return
}

var lastBotMessage *discordgo.Message
var content string
var removableMessageIDs []string

for _, msg := range channelMessages {
if msg.Author.ID == handler.botID {
if lastBotMessage == nil {
lastBotMessage = msg
handler.inventory = model.FromMarkdownTable(msg.Content)
continue
} else if lastBotMessage.Timestamp.After(msg.Timestamp) {
removableMessageIDs = append(removableMessageIDs, lastBotMessage.ID)
lastBotMessage = msg
handler.inventory = model.FromMarkdownTable(msg.Content)
continue
}
} else {
content += "\n" + msg.Content
}
removableMessageIDs = append(removableMessageIDs, msg.ID)
}

for _, line := range strings.Split(content, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}

if removeRegex.MatchString(line) {
handler.inventory = Remove(handler.inventory, line)
handler.inventory = Remove(items, line)
} else {
handler.inventory = Add(handler.inventory, line)
handler.inventory = Add(items, line)
}
}

if err := session.ChannelMessagesBulkDelete(message.ChannelID, removableMessageIDs); err != nil {
log.Error().Err(err).Msg("Could not bulk delete channel messages")
}

PublishItems(handler.inventory, session, lastBotMessage.ChannelID, lastBotMessage.ID)
PublishItems(handler.inventory, session, handler.channelID, lastBotMessageID)
}

func (handler *TkHandler) MessageComponentInteractionEvent(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
Expand Down Expand Up @@ -103,7 +79,7 @@ func (handler *TkHandler) MessageComponentInteractionEvent(session *discordgo.Se
response = &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseUpdateMessage,
Data: &discordgo.InteractionResponseData{
Content: model.ToMarkdownTable(handler.inventory, ""),
Content: model.ToMarkdownTable(handler.inventory, "02.01.06"),
Components: CreateMessageButtons(),
},
}
Expand Down

0 comments on commit 4c82070

Please sign in to comment.