Skip to content

Commit

Permalink
added secret santa invitation message
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Nov 16, 2024
1 parent 78cd750 commit 5a9b71c
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 2 deletions.
12 changes: 12 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ event:
#id:
#animated: true
secretsanta: vote.yes
secretsanta.invite.show_match:
name: 🎁
#id:
#animated: true
secretsanta.invite.set_address:
name: 🏠
#id:
#animated: true
secretsanta.invite.show_address:
name: 🏠
#id:
#animated: true

webserver:
favicon: webserver/favicon.png
Expand Down
8 changes: 8 additions & 0 deletions data/lang/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ discord.command:
msg.setup.no_reactions: Diese Nachricht hat keine Reaktionen. Nur Leute, die mit %s reagiert haben, werden eingeschlossen.
msg.setup.not_enough_reactions: Nicht genug Reaktionen um zu starten. Es werden mindestens %d Reaktionen benötigt.
msg.setup.users: Teilnehmer
msg.setup.invite: Einladen
msg.setup.error: "%d Einladungen konnten nicht verschickt werden."
msg.setup.success: Einladungen wurden verschickt!

msg.invite.title: Einladung zum Wichteln.
msg.invite.button.show_match: Partner anzeigen
msg.invite.button.set_address: Deine Adresse eintragen
msg.invite.button.show_address: Adresse deines Partners anzeigen

module:
adventcalendar:
Expand Down
8 changes: 8 additions & 0 deletions data/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ discord.command:
msg.setup.no_reactions: This message doesn't have any vote reactions. Only members who reated with %s are included.
msg.setup.not_enough_reactions: Not enough votes to start a game. At least %d votes are required.
msg.setup.users: Members
msg.setup.invite: Invite
msg.setup.error: Failed to send %d invites.
msg.setup.success: Invites sent!

msg.invite.title: Invite for secret santa.
msg.invite.button.show_match: Show match
msg.invite.button.set_address: Set your address
msg.invite.button.show_address: Show your partners address

module:
adventcalendar:
Expand Down
2 changes: 2 additions & 0 deletions event/component/componentBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package component

import (
"cake4everybot/modules/adventcalendar"
"cake4everybot/modules/secretsanta"
"log"

"github.com/bwmarrin/discordgo"
Expand Down Expand Up @@ -31,6 +32,7 @@ func Register() {
var componentList []Component

componentList = append(componentList, adventcalendar.Component{})
componentList = append(componentList, secretsanta.Component{})

if len(componentList) == 0 {
return
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/kesuaheli/twitchgo v0.2.7
github.com/spf13/viper v1.19.0
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
)

require (
Expand All @@ -27,7 +28,6 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
47 changes: 47 additions & 0 deletions modules/secretsanta/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package secretsanta

import (
"cake4everybot/util"
"strings"

"github.com/bwmarrin/discordgo"
)

// The Component of the secret santa package.
type Component struct {
secretSantaBase
data discordgo.MessageComponentInteractionData
}

// ID returns the custom ID of the modal to identify the module

Check failure on line 16 in modules/secretsanta/component.go

View workflow job for this annotation

GitHub Actions / Test

comment on exported method Component.Handle should be of the form "Handle ..."
func (c Component) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) {
c.InteractionUtil = util.InteractionUtil{Session: s, Interaction: i}
c.member = i.Member
c.user = i.User
if i.Member != nil {
c.user = i.Member.User
} else if i.User != nil {
c.member = &discordgo.Member{User: i.User}
}
c.data = i.MessageComponentData()

ids := strings.Split(c.data.CustomID, ".")
// pop the first level identifier
util.ShiftL(ids)

switch util.ShiftL(ids) {
case "setup":
c.handleSetup(s, ids)
return
case "invite":
c.handleInvite(s, ids)
default:
log.Printf("Unknown component interaction ID: %s", c.data.CustomID)
}

}

// Custom ID of the modal to identify the module

Check failure on line 44 in modules/secretsanta/component.go

View workflow job for this annotation

GitHub Actions / Test

comment on exported method Component.ID should be of the form "ID ..."
func (c Component) ID() string {
return "secretsanta"
}
64 changes: 64 additions & 0 deletions modules/secretsanta/handleComponentInvite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package secretsanta

import (
"cake4everybot/util"

"github.com/bwmarrin/discordgo"
)

func (c Component) handleInvite(s *discordgo.Session, ids []string) {
switch util.ShiftL(ids) {
case "show_match":
c.handleInviteShowMatch(s, ids)
return
case "set_address":
c.handleInviteSetAddress(s, ids)
return
case "show_address":
c.handleInviteShowAddress(s, ids)
return
default:
log.Printf("Unknown component interaction ID: %s", c.data.CustomID)
}
}

func (c Component) handleInviteShowMatch(s *discordgo.Session, ids []string) {
guildID := util.ShiftL(ids)
_, err := c.getPlayers()
if err != nil {
log.Printf("ERROR: could not get players: %+v", err)
c.ReplyError()
return
}
players := allPlayers[guildID]
if len(players) == 0 {
log.Printf("ERROR: no players in guild %s", guildID)
c.ReplyError()
return
}
var player *player
for _, p := range players {
if p.User.ID == c.Interaction.User.ID {
player = p
}
}

if player == nil {
log.Printf("ERROR: could not find player %s in guild %s: %+v", c.Interaction.User.ID, guildID, c.Interaction.User.ID)
c.ReplyError()
return
}

e := util.AuthoredEmbed(s, player.Match.Member, tp+"display")

util.SetEmbedFooter(s, tp+"display", e)
c.ReplyHiddenEmbed(e)
}

func (c Component) handleInviteSetAddress(s *discordgo.Session, ids []string) {

}

func (c Component) handleInviteShowAddress(s *discordgo.Session, ids []string) {

}
84 changes: 84 additions & 0 deletions modules/secretsanta/handleComponentSetup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package secretsanta

import (
"cake4everybot/data/lang"
"cake4everybot/util"
"fmt"

"github.com/bwmarrin/discordgo"
)

func (c Component) handleSetup(s *discordgo.Session, ids []string) {
switch util.ShiftL(ids) {
case "invite":
c.handleSetupInvite(s)
return
default:
log.Printf("Unknown component interaction ID: %s", c.data.CustomID)
}
}

func (c Component) handleSetupInvite(s *discordgo.Session) {
players, err := c.getPlayers()
if err != nil {
log.Printf("ERROR: could not get players: %+v", err)
c.ReplyError()
return
}
c.ReplyDeferedHidden()

inviteMessage := &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{{
Title: lang.GetDefault(tp + "msg.invite.title"),
Fields: []*discordgo.MessageEmbedField{},
}},
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{Components: []discordgo.MessageComponent{
util.CreateButtonComponent(
fmt.Sprintf("secretsanta.invite.show_match.%s", c.Interaction.GuildID),
lang.GetDefault(tp+"msg.invite.button.show_match"),
discordgo.PrimaryButton,
util.GetConfigComponentEmoji("secretsanta.invite.show_match"),
),
util.CreateButtonComponent(
fmt.Sprintf("secretsanta.invite.set_address.%s", c.Interaction.GuildID),
lang.GetDefault(tp+"msg.invite.button.set_address"),
discordgo.SecondaryButton,
util.GetConfigComponentEmoji("secretsanta.invite.set_address"),
),
util.CreateButtonComponent(
fmt.Sprintf("secretsanta.invite.show_address.%s", c.Interaction.GuildID),
lang.GetDefault(tp+"msg.invite.button.show_address"),
discordgo.SecondaryButton,
util.GetConfigComponentEmoji("secretsanta.invite.show_address"),
),
}},
},
}

var errCount int
for _, player := range players {
var DMChannel *discordgo.Channel
DMChannel, err = s.UserChannelCreate(player.User.ID)
if err != nil {
log.Printf("ERROR: could not create DM channel for user %s: %+v", player.User.ID, err)
errCount++
continue
}

_, err = s.ChannelMessageSendComplex(DMChannel.ID, inviteMessage)
if err != nil {
log.Printf("ERROR: could not send invite: %+v", err)
errCount++
continue
}
log.Printf("Sent invite to user %s in channel %s", player.User.ID, DMChannel.ID)
}

if errCount > 0 {
c.ReplyHiddenf("Failed to send %d invites!", errCount)
return
}

c.ReplyHidden(lang.GetDefault(tp + "msg.setup.success"))
}
8 changes: 7 additions & 1 deletion modules/secretsanta/handlerMessageSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func (cmd MsgCmd) handler() {
return
}

components := []discordgo.MessageComponent{
discordgo.ActionsRow{Components: []discordgo.MessageComponent{
util.CreateButtonComponent("secretsanta.setup.invite", "Invite", discordgo.SuccessButton, nil),
}},
}

util.SetEmbedFooter(cmd.Session, tp+"display", e)
cmd.ReplyHiddenEmbed(e)
cmd.ReplyComponentsHiddenEmbed(components, e)
}

0 comments on commit 5a9b71c

Please sign in to comment.