Skip to content

Commit

Permalink
add edit modal
Browse files Browse the repository at this point in the history
  • Loading branch information
maribowman committed Dec 28, 2023
1 parent c680541 commit 5a39315
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
65 changes: 37 additions & 28 deletions app/service/grocery_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/maribowman/roastbeef-swag/app/model"
"github.com/rs/zerolog/log"
"gopkg.in/ini.v1"
"regexp"
"slices"
"strconv"
Expand All @@ -14,6 +13,11 @@ import (

const (
GroceriesChannelName = "groceries"

EditButton = "edit-button"
DoneButton = "done-button"
EditModal = "edit-modal"
EditModalInput = "edit-modal-input"
)

var (
Expand All @@ -25,7 +29,7 @@ var (
type GroceryBot struct {
botID string
channelID string
shoppingList []model.ShoppingEntry
shoppingList []model.ShoppingListItem
previousShoppingListTable string
}

Expand All @@ -37,6 +41,10 @@ func NewGroceryBot(botID string, channelID string) model.DiscordBot {
}
}

func (bot *GroceryBot) ReadyEvent(*discordgo.Session, *discordgo.Ready) {
// TODO impl bootstrap init
}

func (bot *GroceryBot) MessageEvent(session *discordgo.Session, message *discordgo.MessageCreate) {
if message.Author.ID == bot.botID {
return
Expand All @@ -60,11 +68,13 @@ func (bot *GroceryBot) MessageEvent(session *discordgo.Session, message *discord
bot.shoppingList = model.FromShoppingListTable(msg.Content)
continue
}
} else {
message.Content += "\n" + msg.Content
}
removableMessageIDs = append(removableMessageIDs, msg.ID)
}

for _, line := range strings.Split(message.Content, ini.LineBreak) {
for _, line := range strings.Split(message.Content, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
Expand All @@ -84,37 +94,33 @@ func (bot *GroceryBot) MessageEvent(session *discordgo.Session, message *discord
bot.publish(session, lastMessage)
}

func (bot *GroceryBot) InteractionEvent(session *discordgo.Session,
interaction *discordgo.InteractionCreate) {
func (bot *GroceryBot) MessageComponentInteractionEvent(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
var response *discordgo.InteractionResponse

switch interaction.MessageComponentData().CustomID {
case "edit":
case EditButton:
// TODO send modal to edit list
response = &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseModal,
Data: &discordgo.InteractionResponseData{
Content: "TEST TEST",
//Components: []discordgo.MessageComponent{
// discordgo.SelectMenu{
// MenuType: 0,
// CustomID: "",
// Placeholder: "",
// MinValues: nil,
// MaxValues: 0,
// Options: nil,
// Disabled: false,
// ChannelTypes: nil,
// },
//},
//Flags: 0,
CustomID: "edit_modal_" + interaction.Member.Nick,
Title: "Edit shopping list",
CustomID: EditModal + interaction.Interaction.Member.User.ID,
Title: "Edit grocery list",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: EditModalInput,
Style: discordgo.TextInputParagraph,
Value: model.ToShoppingList(bot.shoppingList),
},
},
},
},
},
}
break
case "done":
bot.shoppingList = []model.ShoppingEntry{}
case DoneButton:
bot.shoppingList = []model.ShoppingListItem{}
response = &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseUpdateMessage,
Data: &discordgo.InteractionResponseData{
Expand All @@ -128,8 +134,11 @@ func (bot *GroceryBot) InteractionEvent(session *discordgo.Session,
_ = session.InteractionRespond(interaction.Interaction, response)
}

func (bot *GroceryBot) ModalSubmitInteractionEvent(*discordgo.Session, *discordgo.InteractionCreate) {
}

func (bot *GroceryBot) remove(line string) {
var tempShoppingList []model.ShoppingEntry
var tempShoppingList []model.ShoppingListItem
removeAllExcept := false

// CAPTURE GROUP 0: entire string
Expand Down Expand Up @@ -199,7 +208,7 @@ func (bot *GroceryBot) add(line string) {
amount = 1
}

bot.shoppingList = append(bot.shoppingList, model.ShoppingEntry{
bot.shoppingList = append(bot.shoppingList, model.ShoppingListItem{
ID: len(bot.shoppingList) + 1,
Item: strings.TrimSpace(line),
Amount: amount,
Expand Down Expand Up @@ -235,14 +244,14 @@ func createMessageButtons() []discordgo.MessageComponent {
Name: "📝",
},
Style: discordgo.SecondaryButton,
CustomID: "edit",
CustomID: EditButton,
},
discordgo.Button{
Emoji: discordgo.ComponentEmoji{
Name: "🏁",
},
Style: discordgo.SecondaryButton,
CustomID: "done",
CustomID: DoneButton,
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions app/service/grocery_bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ func TestAdd(t *testing.T) {
// given
tests := map[string]struct {
content string
expected []model.ShoppingEntry
expected []model.ShoppingListItem
}{
"simple add": {
content: "bacon",
expected: []model.ShoppingEntry{{
expected: []model.ShoppingListItem{{
ID: 1,
Item: "bacon",
Amount: 1,
Expand All @@ -100,7 +100,7 @@ func TestAdd(t *testing.T) {
},
"simple multi word add": {
content: "butter scotch",
expected: []model.ShoppingEntry{{
expected: []model.ShoppingListItem{{
ID: 1,
Item: "butter scotch",
Amount: 1,
Expand All @@ -109,7 +109,7 @@ func TestAdd(t *testing.T) {
},
"simple hyphened add": {
content: "dry-gin",
expected: []model.ShoppingEntry{{
expected: []model.ShoppingListItem{{
ID: 1,
Item: "dry-gin",
Amount: 1,
Expand All @@ -118,7 +118,7 @@ func TestAdd(t *testing.T) {
},
"add with trailing quantity": {
content: "bacon 5",
expected: []model.ShoppingEntry{{
expected: []model.ShoppingListItem{{
ID: 1,
Item: "bacon",
Amount: 5,
Expand All @@ -127,7 +127,7 @@ func TestAdd(t *testing.T) {
},
"add with leading quantity": {
content: "13 bacon",
expected: []model.ShoppingEntry{{
expected: []model.ShoppingListItem{{
ID: 1,
Item: "bacon",
Amount: 13,
Expand All @@ -136,7 +136,7 @@ func TestAdd(t *testing.T) {
},
"add with numbered name": {
content: "2 monkey47",
expected: []model.ShoppingEntry{{
expected: []model.ShoppingListItem{{
ID: 1,
Item: "monkey47",
Amount: 2,
Expand Down

0 comments on commit 5a39315

Please sign in to comment.