Skip to content

Commit

Permalink
plugins: (suggest-topic) Update to new permission system
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT2 committed Oct 13, 2022
1 parent a9be821 commit 6795682
Showing 1 changed file with 85 additions and 86 deletions.
171 changes: 85 additions & 86 deletions plugins/suggest-topic/suggest-topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,114 +42,113 @@ func InitPlugin(_ *plugins.PluginInit) *plugins.Plugin {
}

func TopicConfigCommand(c bot.Command) error {
err := cmd.HasPermission("channels", c)
if err == nil {
channels := []int64{int64(c.E.ChannelID)}
if err := cmd.HasPermission(c, cmd.PermChannels); err != nil {
return err
}

if argChannels, err := cmd.ParseChannelSliceArg(c.Args, 2, -1); err == nil && len(argChannels) != 0 {
channels = argChannels
}
channelsStr := util.JoinInt64Slice(channels, ", ", "<#", ">")
channels := []int64{int64(c.E.ChannelID)}

if arg1, _ := cmd.ParseStringArg(c.Args, 1, true); err != nil {
return err
} else {
switch arg1 {
case "enable":
bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
for _, channel := range channels {
if !util.SliceContains(g.EnabledTopicChannels, channel) {
g.EnabledTopicChannels = append(g.EnabledTopicChannels, channel)
}
if argChannels, err := cmd.ParseChannelSliceArg(c.Args, 2, -1); err == nil && len(argChannels) != 0 {
channels = argChannels
}
channelsStr := util.JoinInt64Slice(channels, ", ", "<#", ">")

if arg1, err := cmd.ParseStringArg(c.Args, 1, true); err != nil {
return err
} else {
switch arg1 {
case "enable":
bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
for _, channel := range channels {
if !util.SliceContains(g.EnabledTopicChannels, channel) {
g.EnabledTopicChannels = append(g.EnabledTopicChannels, channel)
}
return g, "TopicConfigCommand: topic enable"
})
_, err := cmd.SendEmbed(c.E, "Configure Topics", "✅ Added "+channelsStr+" to the allowed topic channels", bot.SuccessColor)
return err
case "disable":
bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
for _, channel := range channels {
if util.SliceContains(g.EnabledTopicChannels, channel) {
g.EnabledTopicChannels = util.SliceRemove(g.EnabledTopicChannels, channel)
}
}
return g, "TopicConfigCommand: topic enable"
})
_, err := cmd.SendEmbed(c.E, "Configure Topics", "✅ Added "+channelsStr+" to the allowed topic channels", bot.SuccessColor)
return err
case "disable":
bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
for _, channel := range channels {
if util.SliceContains(g.EnabledTopicChannels, channel) {
g.EnabledTopicChannels = util.SliceRemove(g.EnabledTopicChannels, channel)
}
return g, "TopicConfigCommand: topic disable"
})
_, err := cmd.SendEmbed(c.E, "Configure Topics", "⛔ Removed "+channelsStr+" from the allowed topic channels", bot.ErrorColor)
return err
case "emoji":
arg2, animated, err3 := cmd.ParseEmojiArg(c.Args, 2, true)
if err3 != nil {
return err3
}
return g, "TopicConfigCommand: topic disable"
})
_, err := cmd.SendEmbed(c.E, "Configure Topics", "⛔ Removed "+channelsStr+" from the allowed topic channels", bot.ErrorColor)
return err
case "emoji":
arg2, animated, err3 := cmd.ParseEmojiArg(c.Args, 2, true)
if err3 != nil {
return err3
}

if arg2 == nil {
if emoji, err := util.GuildTopicVoteEmoji(c.E.GuildID); err != nil {
return err
} else {
_, err = cmd.SendEmbed(c.E, "Current Topic Vote Emoji:", emoji, bot.DefaultColor)
return err
}
if arg2 == nil {
if emoji, err := util.GuildTopicVoteEmoji(c.E.GuildID); err != nil {
return err
} else {
configEmoji := util.ApiEmojiAsConfig(arg2, animated)
emoji, err := util.FormatEncodedEmoji(configEmoji)
if err != nil {
return err
}

bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
g.TopicVoteEmoji = configEmoji
return g, "TopicConfigCommand: update TopicVoteEmoji"
})

_, err = cmd.SendEmbed(c.E, "Set Topic Vote Emoji To:", emoji, bot.SuccessColor)
_, err = cmd.SendEmbed(c.E, "Current Topic Vote Emoji:", emoji, bot.DefaultColor)
return err
}
case "threshold":
arg2, err2 := cmd.ParseInt64Arg(c.Args, 2)
if err2 != nil {
return err2
} else {
configEmoji := util.ApiEmojiAsConfig(arg2, animated)
emoji, err := util.FormatEncodedEmoji(configEmoji)
if err != nil {
return err
}

bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
if arg2 <= 0 {
arg2 = 3
}

g.TopicVoteThreshold = arg2
return g, "TopicConfigCommand: update topic vote threshold"
g.TopicVoteEmoji = configEmoji
return g, "TopicConfigCommand: update TopicVoteEmoji"
})

_, err := cmd.SendEmbed(c.E, "Set Topic Vote Threshold To:", strconv.FormatInt(arg2, 10), bot.SuccessColor)
_, err = cmd.SendEmbed(c.E, "Set Topic Vote Emoji To:", emoji, bot.SuccessColor)
return err
case "list":
noTopicChan := false
formattedChannels := ""

bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
formattedChannels = util.JoinInt64Slice(g.EnabledTopicChannels, "\n", "✅ <#", ">")
noTopicChan = len(g.EnabledTopicChannels) == 0
return g, "TopicConfigCommand: get enabled topic channels"
})
}
case "threshold":
arg2, err2 := cmd.ParseInt64Arg(c.Args, 2)
if err2 != nil {
return err2
}

if noTopicChan {
_, err := cmd.SendEmbed(c.E, "Configure Topics", "There are currently no allowed topic channels", bot.DefaultColor)
return err
bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
if arg2 <= 0 {
arg2 = 3
}

_, err := cmd.SendEmbed(c.E, "Configure Topics", "Allowed Topic Channels:\n\n"+formattedChannels, bot.DefaultColor)
return err
g.TopicVoteThreshold = arg2
return g, "TopicConfigCommand: update topic vote threshold"
})

default:
_, err := cmd.SendEmbed(c.E,
"Configure Topics",
"Available arguments are:\n- `list`\n- `threshold [threshold]`\n- `enable|disable [channel]`",
bot.DefaultColor)
_, err := cmd.SendEmbed(c.E, "Set Topic Vote Threshold To:", strconv.FormatInt(arg2, 10), bot.SuccessColor)
return err
case "list":
noTopicChan := false
formattedChannels := ""

bot.GuildContext(c.E.GuildID, func(g *bot.GuildConfig) (*bot.GuildConfig, string) {
formattedChannels = util.JoinInt64Slice(g.EnabledTopicChannels, "\n", "✅ <#", ">")
noTopicChan = len(g.EnabledTopicChannels) == 0
return g, "TopicConfigCommand: get enabled topic channels"
})

if noTopicChan {
_, err := cmd.SendEmbed(c.E, "Configure Topics", "There are currently no allowed topic channels", bot.DefaultColor)
return err
}

_, err := cmd.SendEmbed(c.E, "Configure Topics", "Allowed Topic Channels:\n\n"+formattedChannels, bot.DefaultColor)
return err

default:
_, err := cmd.SendEmbed(c.E,
"Configure Topics",
"Available arguments are:\n- `list`\n- `threshold [threshold]`\n- `enable|disable [channel]`",
bot.DefaultColor)
return err
}
} else {
return err
}
}

Expand Down

0 comments on commit 6795682

Please sign in to comment.