Skip to content

Commit

Permalink
Disable custom command groups (Suggestion #2224) (#1681)
Browse files Browse the repository at this point in the history
* Disable cc groups

* Update schema.go

* custom_command_groups.go, disable custom command groups

* Update custom_commands.go

* check if the group is disabled

* Added a switch to disable the group and a small warning to the group name

* Added a switch to disable the group and a small warning to the group name

* remove a space

Co-authored-by: Joe L. <[email protected]>

* remove getDisabledGroups and use cmd.R.Group.Disabled

* remove getDisabledGroups and use cmd.R.Group.Disabled

* fix & made the code better

* don't exec the command if the group is disabled

* made the code better

* removed commented-out code

---------

Co-authored-by: Joe L. <[email protected]>
  • Loading branch information
Borbot33 and jo3-l authored Jun 27, 2024
1 parent 013e5aa commit f03f5e8
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 33 deletions.
2 changes: 1 addition & 1 deletion customcommands/assets/customcommands-editcmd.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h2>Custom commands</h2>
class="nav-item {{if $dot.CurrentCommandGroup}}{{if eq $dot.CurrentCommandGroup.ID .ID}}active{{end}}{{end}}">
<a data-partial-load="true"
class="nav-link show {{if $dot.CurrentCommandGroup}}{{if eq $dot.CurrentCommandGroup.ID .ID}}active{{end}}{{end}}"
href="/manage/{{$dot.ActiveGuild.ID}}/customcommands/groups/{{.ID}}">{{.Name}}</a>
href="/manage/{{$dot.ActiveGuild.ID}}/customcommands/groups/{{.ID}}">{{.Name}}{{if .Disabled}} <code>Disabled</code>{{end}}</a>
</li>
{{end}}
<li class="nav-item">
Expand Down
6 changes: 5 additions & 1 deletion customcommands/assets/customcommands.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h2>Custom commands</h2>
class="nav-item {{if $dot.CurrentCommandGroup}}{{if eq $dot.CurrentCommandGroup.ID .ID}}active{{end}}{{end}}">
<a data-partial-load="true"
class="nav-link show {{if $dot.CurrentCommandGroup}}{{if eq $dot.CurrentCommandGroup.ID .ID}}active{{end}}{{end}}"
href="/manage/{{$dot.ActiveGuild.ID}}/customcommands/groups/{{.ID}}">{{.Name}}</a>
href="/manage/{{$dot.ActiveGuild.ID}}/customcommands/groups/{{.ID}}">{{.Name}}{{if .Disabled}} <code>Disabled</code>{{end}}</a>
</li>
{{end}}
<li class="nav-item">
Expand Down Expand Up @@ -106,6 +106,10 @@ <h2>Custom commands</h2>
{{textChannelOptionsMulti .ActiveGuild.Channels .CurrentCommandGroup.IgnoreChannels }}
</select>
</div>
<div class="form-group">
<label>Group disabled</label><br>
{{checkbox "disabled" "disabled" "Enables or Disables the group" .CurrentCommandGroup.Disabled}}
</div>
<div class="form-group">
<button type="submit"
title="Group #{{.CurrentCommandGroup.ID}} - {{.CurrentCommandGroup.Name}} &#013;Deleted group's commands become ungrouped."
Expand Down
18 changes: 13 additions & 5 deletions customcommands/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,14 @@ func StringCommands(ccs []*models.CustomCommand, gMap map[int64]string) string {

func handleDelayedRunCC(evt *schEventsModels.ScheduledEvent, data interface{}) (retry bool, err error) {
dataCast := data.(*DelayedRunCCData)
cmd, err := models.CustomCommands(qm.Where("guild_id = ? AND local_id = ?", evt.GuildID, dataCast.CmdID)).OneG(context.Background())
cmd, err := models.CustomCommands(qm.Where("guild_id = ? AND local_id = ?", evt.GuildID, dataCast.CmdID), qm.Load("Group")).OneG(context.Background())
if err != nil {
return false, errors.WrapIf(err, "find_command")
}

if cmd.R.Group != nil && cmd.R.Group.Disabled {
return false, errors.New("custom command group is disabled")
}

if cmd.Disabled {
return false, errors.New("custom command is disabled")
Expand Down Expand Up @@ -465,11 +469,15 @@ func handleDelayedRunCC(evt *schEventsModels.ScheduledEvent, data interface{}) (
}

func handleNextRunScheduledEVent(evt *schEventsModels.ScheduledEvent, data interface{}) (retry bool, err error) {
cmd, err := models.CustomCommands(qm.Where("guild_id = ? AND local_id = ?", evt.GuildID, (data.(*NextRunScheduledEvent)).CmdID)).OneG(context.Background())
cmd, err := models.CustomCommands(qm.Where("guild_id = ? AND local_id = ?", evt.GuildID, (data.(*NextRunScheduledEvent)).CmdID), qm.Load("Group")).OneG(context.Background())
if err != nil {
return false, errors.WrapIf(err, "find_command")
}

if cmd.R.Group != nil && cmd.R.Group.Disabled {
return false, errors.New("custom command group is disabled")
}

if cmd.Disabled {
return false, errors.New("custom command is disabled")
}
Expand Down Expand Up @@ -841,7 +849,7 @@ func findMessageTriggerCustomCommands(ctx context.Context, cs *dstate.ChannelSta

var matched []*TriggeredCC
for _, cmd := range cmds {
if cmd.Disabled || !CmdRunsInChannel(cmd, common.ChannelOrThreadParentID(cs)) || !CmdRunsForUser(cmd, ms) {
if cmd.Disabled || !CmdRunsInChannel(cmd, common.ChannelOrThreadParentID(cs)) || !CmdRunsForUser(cmd, ms) || cmd.R.Group != nil && cmd.R.Group.Disabled {
continue
}

Expand Down Expand Up @@ -877,7 +885,7 @@ func findReactionTriggerCustomCommands(ctx context.Context, cs *dstate.ChannelSt

var matched []*TriggeredCC
for _, cmd := range cmds {
if cmd.Disabled || !CmdRunsInChannel(cmd, common.ChannelOrThreadParentID(cs)) {
if cmd.Disabled || !CmdRunsInChannel(cmd, common.ChannelOrThreadParentID(cs)) || cmd.R.Group != nil && cmd.R.Group.Disabled {
continue
}

Expand Down Expand Up @@ -935,7 +943,7 @@ func findComponentOrModalTriggerCustomCommands(ctx context.Context, cs *dstate.C

var matched []*TriggeredCC
for _, cmd := range cmds {
if cmd.Disabled || !CmdRunsInChannel(cmd, common.ChannelOrThreadParentID(cs)) {
if cmd.Disabled || !CmdRunsInChannel(cmd, common.ChannelOrThreadParentID(cs)) || cmd.R.Group != nil && cmd.R.Group.Disabled {
continue
}

Expand Down
20 changes: 18 additions & 2 deletions customcommands/models/custom_command_groups.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 72 additions & 21 deletions customcommands/models/custom_commands.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions customcommands/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ CREATE INDEX IF NOT EXISTS templates_user_database_expires_idx ON templates_user
ALTER TABLE custom_commands ADD COLUMN IF NOT EXISTS name TEXT;
`, `
ALTER TABLE custom_commands ADD COLUMN IF NOT EXISTS public BOOLEAN NOT NULL DEFAULT false;
`, `
ALTER TABLE custom_command_groups ADD COLUMN IF NOT EXISTS disabled BOOLEAN NOT NULL DEFAULT false;
`}
Loading

0 comments on commit f03f5e8

Please sign in to comment.