Skip to content

Commit

Permalink
reminders: verify that member can run remindme in target channel
Browse files Browse the repository at this point in the history
  • Loading branch information
jo3-l committed Jun 19, 2024
1 parent be8b956 commit caa9d95
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions reminders/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,36 @@ var cmds = []*commands.YAGCommand{

id := parsed.ChannelID
if c := parsed.Switch("channel"); c.Value != nil {
id = c.Value.(*dstate.ChannelState).ID
cs := c.Value.(*dstate.ChannelState)
mention, _ := cs.Mention()

hasPerms, err := bot.AdminOrPermMS(parsed.GuildData.GS.ID, id, parsed.GuildData.MS, discordgo.PermissionSendMessages|discordgo.PermissionViewChannel)
hasPerms, err := bot.AdminOrPermMS(parsed.GuildData.GS.ID, cs.ID, parsed.GuildData.MS, discordgo.PermissionSendMessages|discordgo.PermissionViewChannel)
if err != nil {
return "Failed checking permissions; please try again or join the support server.", err
}

if !hasPerms {
return "You do not have permissions to send messages there", nil
return fmt.Sprintf("You do not have permissions to send messages in %s", mention), nil
}

// Ensure the member can run the `remindme` command in the
// target channel according to the configured command and
// channel overrides, if any.
yc := parsed.Cmd.Command.(*commands.YAGCommand)
settings, err := yc.GetSettings(parsed.ContainerChain, cs, parsed.GuildData.GS)
if err != nil {
return "Failed fetching command settings", err
}

if !settings.Enabled {
return fmt.Sprintf("The `remindme` command is disabled in %s", mention), nil
}

ms := parsed.GuildData.MS
hasAnyRequiredRole := len(settings.RequiredRoles) > 0 && memberHasAnyRole(ms, settings.RequiredRoles)
hasAnyIgnoredRole := memberHasAnyRole(ms, settings.IgnoreRoles)
if !hasAnyRequiredRole || hasAnyIgnoredRole {
return fmt.Sprintf("You cannot use the `remindme` command in %s", mention), nil
}
}

Expand Down Expand Up @@ -230,6 +251,15 @@ var cmds = []*commands.YAGCommand{
},
}

func memberHasAnyRole(ms *dstate.MemberState, roles []int64) bool {
for _, r := range ms.Member.Roles {
if common.ContainsInt64Slice(roles, r) {
return true
}
}
return false
}

func checkUserScheduledEvent(evt *seventsmodels.ScheduledEvent, data interface{}) (retry bool, err error) {
// IMPORTANT: evt.GuildID can be 1 in cases where it was migrated from the
// legacy scheduled event system.
Expand Down

0 comments on commit caa9d95

Please sign in to comment.