Skip to content

Commit

Permalink
fix(Random): everyone was able to reflip/-roll/-split the random comm…
Browse files Browse the repository at this point in the history
…and responses

Now only the original author can use these buttons.
  • Loading branch information
Kesuaheli committed Jan 1, 2025
1 parent 9ee2f3c commit 939cf7c
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/lang/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ discord.command:
option.teams.option.team_amount: gruppenanzahl
option.teams.option.team_amount.description: Wie viele Gruppen sollen erstellt werden?

msg.error.not_author: Das ist die Nachricht von %s. Du kannst das nicht verwenden!
msg.dice.roll: Du hast eine %d gewürfelt.
msg.teams.missing_option: "Fehler: Du musst entweder die Gruppengröße oder die Gruppenanzahl angeben!"
msg.teams.multiple_options: "Fehler: Du kannst nicht gleichzeitig die Gruppengröße und die Gruppenanzahl angeben!"
Expand Down
1 change: 1 addition & 0 deletions data/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ discord.command:
option.teams.option.team_amount: amount
option.teams.option.team_amount.description: How many teams should be created?

msg.error.not_author: This is the message of %s. You can't use this here!
msg.dice.roll: You rolled a %d
msg.teams.missing_option: "Error: You need to specify either the team size or the team amount!"
msg.teams.multiple_options: "Error: You can't specify both the team size and the team amount at the same time!"
Expand Down
6 changes: 6 additions & 0 deletions modules/random/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func (c Component) Handle(s *discordgo.Session, i *discordgo.InteractionCreate)
}
c.data = i.MessageComponentData()

if c.Interaction.Message.Type == discordgo.MessageTypeChatInputCommand {
c.originalAuthor = c.Interaction.Message.Interaction.User
} else {
c.originalAuthor = c.Interaction.Message.Author
}

ids := strings.Split(c.data.CustomID, ".")
// pop the first level identifier
util.ShiftL(ids)
Expand Down
4 changes: 4 additions & 0 deletions modules/random/handleSubcommandCoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (cmd subcommandCoin) handle() {
func (cmd subcommandCoin) handleComponent(ids []string) {
switch id := util.ShiftL(ids); id {
case "reflip":
if cmd.originalAuthor.ID != cmd.user.ID {
cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.error.not_author"), cmd.originalAuthor.Mention())
return
}
cmd.ReplyComplexUpdate(cmd.flip())
return
default:
Expand Down
4 changes: 4 additions & 0 deletions modules/random/handleSubcommandDice.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (cmd subcommandDice) handle() {
func (cmd subcommandDice) handleComponent(ids []string) {
switch id := util.ShiftL(ids); id {
case "reroll":
if cmd.originalAuthor.ID != cmd.user.ID {
cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.error.not_author"), cmd.originalAuthor.Mention())
return
}
diceRange, _ := strconv.Atoi(util.ShiftL(ids))
cmd.ReplyComplexUpdate(cmd.roll(diceRange))
return
Expand Down
8 changes: 8 additions & 0 deletions modules/random/handleSubcommandTeams.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func (cmd subcommandTeams) handle() {
func (cmd subcommandTeams) handleComponent(ids []string) {
switch id := util.ShiftL(ids); id {
case "resplit_size":
if cmd.originalAuthor.ID != cmd.user.ID {
cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.error.not_author"), cmd.originalAuthor.Mention())
return
}
teamSize, _ := strconv.Atoi(util.ShiftL(ids))
members, _, err := cmd.parseTeamEmbeds(cmd.Interaction.Message.Embeds)
if err != nil {
Expand All @@ -155,6 +159,10 @@ func (cmd subcommandTeams) handleComponent(ids []string) {
cmd.ReplyComplexUpdate(cmd.splitTeamsSize(members, teamSize))
return
case "resplit_amount":
if cmd.originalAuthor.ID != cmd.user.ID {
cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.error.not_author"), cmd.originalAuthor.Mention())
return
}
members, n, err := cmd.parseTeamEmbeds(cmd.Interaction.Message.Embeds)
if err != nil {
log.Printf("ERROR: could not parse team embeds: %+v", err)
Expand Down
2 changes: 2 additions & 0 deletions modules/random/randombase.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ type randomBase struct {
util.InteractionUtil
member *discordgo.Member
user *discordgo.User

originalAuthor *discordgo.User
}

0 comments on commit 939cf7c

Please sign in to comment.