Skip to content

Commit 9dc3f4f

Browse files
committed
added secret santa admin commands
1 parent 7b4b245 commit 9dc3f4f

9 files changed

+219
-23
lines changed

data/lang/de.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ discord.command:
129129

130130
title: Wichteln
131131

132+
cmd:
133+
base: wichteln
134+
base.description: Admin Commands für das Wichteln
135+
option.show: auflisten
136+
option.show.description: Alle Teilnehmer anzeigen
137+
option.update: aktualisieren
138+
option.update.description: Die Nachricht all Teilnehmer aktualisieren
139+
132140
msg.setup.no_reactions: Diese Nachricht hat keine Reaktionen. Nur Leute, die mit %s reagiert haben, werden eingeschlossen.
133141
msg.setup.not_enough_reactions: Nicht genug Reaktionen um zu starten. Es werden mindestens %d Reaktionen benötigt.
134142
msg.setup.users: Teilnehmer
@@ -138,6 +146,9 @@ discord.command:
138146
msg.setup.error: "%d Einladungen konnten nicht verschickt werden."
139147
msg.setup.success: Einladungen wurden verschickt!
140148

149+
msg.cmd.update.error: "%d Einladung(en) konnte(n) nicht aktualisiert werden.%s"
150+
msg.cmd.update.success: "%d Einladungen wurden aktualisiert!"
151+
141152
msg.invite.title: Einladung zum Wichteln.
142153
msg.invite.description: Du nimmst am Cake4Everyone Wichteln teil. Klicke die Knöpfe unten, um deinen Partner zu sehen und deine Adresse einzutragen.
143154
msg.invite.set_address.match: Dein Partner hat eine Adresse eingetragen

data/lang/en.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ discord.command:
129129

130130
title: Secret Santa
131131

132+
cmd:
133+
base: secretsanta
134+
base.description: Admin commands for the Secret Santa Game
135+
option.show: show
136+
option.show.description: Show all participants
137+
option.update: update
138+
option.update.description: Update the message off all participants
139+
132140
msg.setup.no_reactions: This message doesn't have any vote reactions. Only members who reated with %s are included.
133141
msg.setup.not_enough_reactions: Not enough votes to start a game. At least %d votes are required.
134142
msg.setup.users: Members
@@ -138,6 +146,9 @@ discord.command:
138146
msg.setup.error: Failed to send %d invites.
139147
msg.setup.success: Invites sent!
140148

149+
msg.cmd.update.error: Failed to update %d invites.%s
150+
msg.cmd.update.success: "%d invites updated!"
151+
141152
msg.invite.title: Invite for secret santa.
142153
msg.invite.description: You are participating in Cake4Everyone Secret Santa. Click the buttons below to see your partner and set your address.
143154
msg.invite.set_address.match: Your partner has set an address

event/command/commandBase.go

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func Register(s *discordgo.Session, guildID string) error {
7070
commandsList = append(commandsList, &birthday.Chat{})
7171
commandsList = append(commandsList, &info.Chat{})
7272
commandsList = append(commandsList, &adventcalendar.Chat{})
73+
commandsList = append(commandsList, &secretsanta.Chat{})
7374
commandsList = append(commandsList, &secretsanta.MsgCmd{})
7475
// messsage commands
7576
// user commands

modules/secretsanta/chatCommand.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package secretsanta
2+
3+
import (
4+
"cake4everybot/data/lang"
5+
"cake4everybot/util"
6+
7+
"github.com/bwmarrin/discordgo"
8+
)
9+
10+
// The Chat (slash) command of the secret santa package.
11+
type Chat struct {
12+
secretSantaBase
13+
ID string
14+
}
15+
16+
// AppCmd (ApplicationCommand) returns the definition of the chat command
17+
func (Chat) AppCmd() *discordgo.ApplicationCommand {
18+
return &discordgo.ApplicationCommand{
19+
Name: lang.GetDefault(tp + "cmd.base"),
20+
NameLocalizations: util.TranslateLocalization(tp + "cmd.base"),
21+
Description: lang.GetDefault(tp + "cmd.base.description"),
22+
DescriptionLocalizations: util.TranslateLocalization(tp + "cmd.base.description"),
23+
Options: []*discordgo.ApplicationCommandOption{
24+
{
25+
Type: discordgo.ApplicationCommandOptionSubCommand,
26+
Name: lang.GetDefault(tp + "cmd.option.show"),
27+
NameLocalizations: *util.TranslateLocalization(tp + "cmd.option.show"),
28+
Description: lang.GetDefault(tp + "cmd.option.show.description"),
29+
DescriptionLocalizations: *util.TranslateLocalization(tp + "cmd.option.show.description"),
30+
},
31+
{
32+
Type: discordgo.ApplicationCommandOptionSubCommand,
33+
Name: lang.GetDefault(tp + "cmd.option.update"),
34+
NameLocalizations: *util.TranslateLocalization(tp + "cmd.option.update"),
35+
Description: lang.GetDefault(tp + "cmd.option.update.description"),
36+
DescriptionLocalizations: *util.TranslateLocalization(tp + "cmd.option.update.description"),
37+
},
38+
},
39+
}
40+
}
41+
42+
// Handle handles the functionality of a command
43+
func (cmd Chat) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) {
44+
cmd.InteractionUtil = util.InteractionUtil{Session: s, Interaction: i}
45+
cmd.member = i.Member
46+
cmd.user = i.User
47+
if i.Member != nil {
48+
cmd.user = i.Member.User
49+
} else if i.User != nil {
50+
cmd.member = &discordgo.Member{User: i.User}
51+
}
52+
53+
switch i.ApplicationCommandData().Options[0].Name {
54+
case lang.GetDefault(tp + "cmd.option.show"):
55+
cmd.handleSubcommandShow()
56+
return
57+
case lang.GetDefault(tp + "cmd.option.update"):
58+
cmd.handleSubcommandUpdate()
59+
return
60+
}
61+
62+
}
63+
64+
// SetID sets the registered command ID for internal uses after uploading to discord
65+
func (cmd *Chat) SetID(id string) {
66+
cmd.ID = id
67+
}
68+
69+
// GetID gets the registered command ID
70+
func (cmd Chat) GetID() string {
71+
return cmd.ID
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package secretsanta
2+
3+
// handleSubcommandShow handles the functionality of the show subcommand
4+
func (cmd Chat) handleSubcommandShow() {
5+
players, err := cmd.getPlayers()
6+
if err != nil {
7+
cmd.ReplyError()
8+
return
9+
}
10+
11+
var list string
12+
for _, p := range players {
13+
list += "- " +
14+
p.Mention() +
15+
" - (`" + p.User.Username + "`)" +
16+
"\n"
17+
}
18+
cmd.ReplyHiddenSimpleEmbed(0x690042, list)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package secretsanta
2+
3+
import (
4+
"cake4everybot/data/lang"
5+
"cake4everybot/util"
6+
7+
"github.com/bwmarrin/discordgo"
8+
)
9+
10+
// handleSubcommandUpdate handles the functionality of the update subcommand
11+
func (cmd Chat) handleSubcommandUpdate() {
12+
cmd.ReplyDeferedHidden()
13+
players, err := cmd.getPlayers()
14+
if err != nil {
15+
cmd.ReplyError()
16+
return
17+
}
18+
19+
var failedToSend string
20+
for _, p := range players {
21+
var DMChannel *discordgo.Channel
22+
DMChannel, err = cmd.Session.UserChannelCreate(p.User.ID)
23+
if err != nil {
24+
log.Printf("ERROR: could not create DM channel for user %s: %+v", p.User.ID, err)
25+
failedToSend += "\n- " + p.Mention()
26+
continue
27+
}
28+
29+
if p.MessageID == "" {
30+
var msg *discordgo.Message
31+
msg, err = cmd.Session.ChannelMessageSendComplex(DMChannel.ID, cmd.inviteMessage(p))
32+
if err != nil {
33+
log.Printf("ERROR: could not send invite message for %s: %+v", p.DisplayName(), err)
34+
failedToSend += "\n- " + p.Mention()
35+
continue
36+
}
37+
p.MessageID = msg.ID
38+
} else {
39+
_, err = cmd.Session.ChannelMessageEditComplex(util.MessageComplexEdit(cmd.inviteMessage(p), DMChannel.ID, p.MessageID))
40+
if err != nil {
41+
log.Printf("ERROR: could not update bot message for %s '%s/%s': %+v", p.DisplayName(), cmd.Interaction.ChannelID, p.MessageID, err)
42+
failedToSend += "\n- " + p.Mention()
43+
return
44+
}
45+
}
46+
}
47+
48+
if failedToSend != "" {
49+
cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.cmd.update.error"), failedToSend)
50+
return
51+
}
52+
cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.cmd.update.success"), len(players))
53+
}

modules/secretsanta/handleComponentSetup.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package secretsanta
33
import (
44
"cake4everybot/data/lang"
55
"cake4everybot/util"
6-
"fmt"
76

87
"github.com/bwmarrin/discordgo"
98
)
@@ -33,26 +32,6 @@ func (c Component) handleSetupInvite() {
3332
return
3433
}
3534

36-
inviteMessage := &discordgo.MessageSend{
37-
Embeds: make([]*discordgo.MessageEmbed, 1),
38-
Components: []discordgo.MessageComponent{
39-
discordgo.ActionsRow{Components: []discordgo.MessageComponent{
40-
util.CreateButtonComponent(
41-
fmt.Sprintf("secretsanta.invite.show_match.%s", c.Interaction.GuildID),
42-
lang.GetDefault(tp+"msg.invite.button.show_match"),
43-
discordgo.PrimaryButton,
44-
util.GetConfigComponentEmoji("secretsanta.invite.show_match"),
45-
),
46-
util.CreateButtonComponent(
47-
fmt.Sprintf("secretsanta.invite.set_address.%s", c.Interaction.GuildID),
48-
lang.GetDefault(tp+"msg.invite.button.set_address"),
49-
discordgo.SecondaryButton,
50-
util.GetConfigComponentEmoji("secretsanta.invite.set_address"),
51-
),
52-
}},
53-
},
54-
}
55-
5635
var failedToSend string
5736
for _, player := range players {
5837
var DMChannel *discordgo.Channel
@@ -63,9 +42,8 @@ func (c Component) handleSetupInvite() {
6342
continue
6443
}
6544

66-
inviteMessage.Embeds[0] = player.InviteEmbed(c.Session)
6745
var msg *discordgo.Message
68-
msg, err = c.Session.ChannelMessageSendComplex(DMChannel.ID, inviteMessage)
46+
msg, err = c.Session.ChannelMessageSendComplex(DMChannel.ID, c.inviteMessage(player))
6947
if err != nil {
7048
log.Printf("ERROR: could not send invite: %+v", err)
7149
failedToSend += "\n- " + player.Mention()

modules/secretsanta/secretsantabase.go

+23
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ func (ssb secretSantaBase) setPlayers(players map[string]*player) (err error) {
9898
return nil
9999
}
100100

101+
// inviteMessage returns the message to send to the player to invite them to play.
102+
func (ssb secretSantaBase) inviteMessage(p *player) *discordgo.MessageSend {
103+
return &discordgo.MessageSend{
104+
Embeds: []*discordgo.MessageEmbed{p.InviteEmbed(ssb.Session)},
105+
Components: []discordgo.MessageComponent{
106+
discordgo.ActionsRow{Components: []discordgo.MessageComponent{
107+
util.CreateButtonComponent(
108+
fmt.Sprintf("secretsanta.invite.show_match.%s", ssb.Interaction.GuildID),
109+
lang.GetDefault(tp+"msg.invite.button.show_match"),
110+
discordgo.PrimaryButton,
111+
util.GetConfigComponentEmoji("secretsanta.invite.show_match"),
112+
),
113+
util.CreateButtonComponent(
114+
fmt.Sprintf("secretsanta.invite.set_address.%s", ssb.Interaction.GuildID),
115+
lang.GetDefault(tp+"msg.invite.button.set_address"),
116+
discordgo.SecondaryButton,
117+
util.GetConfigComponentEmoji("secretsanta.invite.set_address"),
118+
),
119+
}},
120+
},
121+
}
122+
}
123+
101124
// player is a player in the secret santa game
102125
type player struct {
103126
*discordgo.Member

util/discord.go

+28
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,31 @@ func componentEmoji[E *discordgo.Emoji | *discordgo.ComponentEmoji](e E) *discor
194194
}
195195
panic("Given generic type is not an emoji or component emoji")
196196
}
197+
198+
// MessageComplexEdit converts a [discordgo.MessageSend] to a [discordgo.MessageEdit]
199+
func MessageComplexEdit(src *discordgo.MessageSend, channel, id string) *discordgo.MessageEdit {
200+
return &discordgo.MessageEdit{
201+
Content: &src.Content,
202+
Components: &src.Components,
203+
Embeds: &src.Embeds,
204+
AllowedMentions: src.AllowedMentions,
205+
Flags: src.Flags,
206+
Files: src.Files,
207+
208+
Channel: channel,
209+
ID: id,
210+
}
211+
}
212+
213+
// MessageComplexSend converts a [discordgo.MessageEdit] to a [discordgo.MessageSend]
214+
func MessageComplexSend(src *discordgo.MessageEdit) *discordgo.MessageSend {
215+
return &discordgo.MessageSend{
216+
Content: *src.Content,
217+
Components: *src.Components,
218+
Embeds: *src.Embeds,
219+
AllowedMentions: src.AllowedMentions,
220+
Flags: src.Flags,
221+
Files: src.Files,
222+
}
223+
224+
}

0 commit comments

Comments
 (0)