Skip to content

Commit

Permalink
feat(Secret Santa): added secret santa admin commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Dec 14, 2024
1 parent 7b4b245 commit 8307905
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 23 deletions.
11 changes: 11 additions & 0 deletions data/lang/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ discord.command:

title: Wichteln

cmd:
base: wichteln
base.description: Admin Commands für das Wichteln
option.show: auflisten
option.show.description: Alle Teilnehmer anzeigen
option.update: aktualisieren
option.update.description: Die Nachricht all Teilnehmer aktualisieren

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
Expand All @@ -138,6 +146,9 @@ discord.command:
msg.setup.error: "%d Einladungen konnten nicht verschickt werden."
msg.setup.success: Einladungen wurden verschickt!

msg.cmd.update.error: "%d Einladung(en) konnte(n) nicht aktualisiert werden.%s"
msg.cmd.update.success: "%d Einladungen wurden aktualisiert!"

msg.invite.title: Einladung zum Wichteln.
msg.invite.description: Du nimmst am Cake4Everyone Wichteln teil. Klicke die Knöpfe unten, um deinen Partner zu sehen und deine Adresse einzutragen.
msg.invite.set_address.match: Dein Partner hat eine Adresse eingetragen
Expand Down
11 changes: 11 additions & 0 deletions data/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ discord.command:

title: Secret Santa

cmd:
base: secretsanta
base.description: Admin commands for the Secret Santa Game
option.show: show
option.show.description: Show all participants
option.update: update
option.update.description: Update the message off all participants

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
Expand All @@ -138,6 +146,9 @@ discord.command:
msg.setup.error: Failed to send %d invites.
msg.setup.success: Invites sent!

msg.cmd.update.error: Failed to update %d invites.%s
msg.cmd.update.success: "%d invites updated!"

msg.invite.title: Invite for secret santa.
msg.invite.description: You are participating in Cake4Everyone Secret Santa. Click the buttons below to see your partner and set your address.
msg.invite.set_address.match: Your partner has set an address
Expand Down
1 change: 1 addition & 0 deletions event/command/commandBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func Register(s *discordgo.Session, guildID string) error {
commandsList = append(commandsList, &birthday.Chat{})
commandsList = append(commandsList, &info.Chat{})
commandsList = append(commandsList, &adventcalendar.Chat{})
commandsList = append(commandsList, &secretsanta.Chat{})
commandsList = append(commandsList, &secretsanta.MsgCmd{})
// messsage commands
// user commands
Expand Down
72 changes: 72 additions & 0 deletions modules/secretsanta/chatCommand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package secretsanta

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

"github.com/bwmarrin/discordgo"
)

// The Chat (slash) command of the secret santa package.
type Chat struct {
secretSantaBase
ID string
}

// AppCmd (ApplicationCommand) returns the definition of the chat command
func (Chat) AppCmd() *discordgo.ApplicationCommand {
return &discordgo.ApplicationCommand{
Name: lang.GetDefault(tp + "cmd.base"),
NameLocalizations: util.TranslateLocalization(tp + "cmd.base"),
Description: lang.GetDefault(tp + "cmd.base.description"),
DescriptionLocalizations: util.TranslateLocalization(tp + "cmd.base.description"),
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: lang.GetDefault(tp + "cmd.option.show"),
NameLocalizations: *util.TranslateLocalization(tp + "cmd.option.show"),
Description: lang.GetDefault(tp + "cmd.option.show.description"),
DescriptionLocalizations: *util.TranslateLocalization(tp + "cmd.option.show.description"),
},
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: lang.GetDefault(tp + "cmd.option.update"),
NameLocalizations: *util.TranslateLocalization(tp + "cmd.option.update"),
Description: lang.GetDefault(tp + "cmd.option.update.description"),
DescriptionLocalizations: *util.TranslateLocalization(tp + "cmd.option.update.description"),
},
},
}
}

// Handle handles the functionality of a command
func (cmd Chat) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) {
cmd.InteractionUtil = util.InteractionUtil{Session: s, Interaction: i}
cmd.member = i.Member
cmd.user = i.User
if i.Member != nil {
cmd.user = i.Member.User
} else if i.User != nil {
cmd.member = &discordgo.Member{User: i.User}
}

switch i.ApplicationCommandData().Options[0].Name {
case lang.GetDefault(tp + "cmd.option.show"):
cmd.handleSubcommandShow()
return
case lang.GetDefault(tp + "cmd.option.update"):
cmd.handleSubcommandUpdate()
return
}

}

// SetID sets the registered command ID for internal uses after uploading to discord
func (cmd *Chat) SetID(id string) {
cmd.ID = id
}

// GetID gets the registered command ID
func (cmd Chat) GetID() string {
return cmd.ID
}
19 changes: 19 additions & 0 deletions modules/secretsanta/handleChatCommandShow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package secretsanta

// handleSubcommandShow handles the functionality of the show subcommand
func (cmd Chat) handleSubcommandShow() {
players, err := cmd.getPlayers()
if err != nil {
cmd.ReplyError()
return
}

var list string
for _, p := range players {
list += "- " +
p.Mention() +
" - (`" + p.User.Username + "`)" +
"\n"
}
cmd.ReplyHiddenSimpleEmbed(0x690042, list)
}
53 changes: 53 additions & 0 deletions modules/secretsanta/handleChatCommandUpdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package secretsanta

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

"github.com/bwmarrin/discordgo"
)

// handleSubcommandUpdate handles the functionality of the update subcommand
func (cmd Chat) handleSubcommandUpdate() {
cmd.ReplyDeferedHidden()
players, err := cmd.getPlayers()
if err != nil {
cmd.ReplyError()
return
}

var failedToSend string
for _, p := range players {
var DMChannel *discordgo.Channel
DMChannel, err = cmd.Session.UserChannelCreate(p.User.ID)
if err != nil {
log.Printf("ERROR: could not create DM channel for user %s: %+v", p.User.ID, err)
failedToSend += "\n- " + p.Mention()
continue
}

if p.MessageID == "" {
var msg *discordgo.Message
msg, err = cmd.Session.ChannelMessageSendComplex(DMChannel.ID, cmd.inviteMessage(p))
if err != nil {
log.Printf("ERROR: could not send invite message for %s: %+v", p.DisplayName(), err)
failedToSend += "\n- " + p.Mention()
continue
}
p.MessageID = msg.ID
} else {
_, err = cmd.Session.ChannelMessageEditComplex(util.MessageComplexEdit(cmd.inviteMessage(p), DMChannel.ID, p.MessageID))
if err != nil {
log.Printf("ERROR: could not update bot message for %s '%s/%s': %+v", p.DisplayName(), cmd.Interaction.ChannelID, p.MessageID, err)
failedToSend += "\n- " + p.Mention()
return
}
}
}

if failedToSend != "" {
cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.cmd.update.error"), failedToSend)
return
}
cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.cmd.update.success"), len(players))
}
24 changes: 1 addition & 23 deletions modules/secretsanta/handleComponentSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package secretsanta
import (
"cake4everybot/data/lang"
"cake4everybot/util"
"fmt"

"github.com/bwmarrin/discordgo"
)
Expand Down Expand Up @@ -33,26 +32,6 @@ func (c Component) handleSetupInvite() {
return
}

inviteMessage := &discordgo.MessageSend{
Embeds: make([]*discordgo.MessageEmbed, 1),
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"),
),
}},
},
}

var failedToSend string
for _, player := range players {
var DMChannel *discordgo.Channel
Expand All @@ -63,9 +42,8 @@ func (c Component) handleSetupInvite() {
continue
}

inviteMessage.Embeds[0] = player.InviteEmbed(c.Session)
var msg *discordgo.Message
msg, err = c.Session.ChannelMessageSendComplex(DMChannel.ID, inviteMessage)
msg, err = c.Session.ChannelMessageSendComplex(DMChannel.ID, c.inviteMessage(player))
if err != nil {
log.Printf("ERROR: could not send invite: %+v", err)
failedToSend += "\n- " + player.Mention()
Expand Down
23 changes: 23 additions & 0 deletions modules/secretsanta/secretsantabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ func (ssb secretSantaBase) setPlayers(players map[string]*player) (err error) {
return nil
}

// inviteMessage returns the message to send to the player to invite them to play.
func (ssb secretSantaBase) inviteMessage(p *player) *discordgo.MessageSend {
return &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{p.InviteEmbed(ssb.Session)},
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{Components: []discordgo.MessageComponent{
util.CreateButtonComponent(
fmt.Sprintf("secretsanta.invite.show_match.%s", ssb.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", ssb.Interaction.GuildID),
lang.GetDefault(tp+"msg.invite.button.set_address"),
discordgo.SecondaryButton,
util.GetConfigComponentEmoji("secretsanta.invite.set_address"),
),
}},
},
}
}

// player is a player in the secret santa game
type player struct {
*discordgo.Member
Expand Down
28 changes: 28 additions & 0 deletions util/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,31 @@ func componentEmoji[E *discordgo.Emoji | *discordgo.ComponentEmoji](e E) *discor
}
panic("Given generic type is not an emoji or component emoji")
}

// MessageComplexEdit converts a [discordgo.MessageSend] to a [discordgo.MessageEdit]
func MessageComplexEdit(src *discordgo.MessageSend, channel, id string) *discordgo.MessageEdit {
return &discordgo.MessageEdit{
Content: &src.Content,
Components: &src.Components,
Embeds: &src.Embeds,
AllowedMentions: src.AllowedMentions,
Flags: src.Flags,
Files: src.Files,

Channel: channel,
ID: id,
}
}

// MessageComplexSend converts a [discordgo.MessageEdit] to a [discordgo.MessageSend]
func MessageComplexSend(src *discordgo.MessageEdit) *discordgo.MessageSend {
return &discordgo.MessageSend{
Content: *src.Content,
Components: *src.Components,
Embeds: *src.Embeds,
AllowedMentions: src.AllowedMentions,
Flags: src.Flags,
Files: src.Files,
}

}

0 comments on commit 8307905

Please sign in to comment.