set permission requirements for more commands
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/disgoorg/disgo/discord"
|
"github.com/disgoorg/disgo/discord"
|
||||||
"github.com/disgoorg/disgo/events"
|
"github.com/disgoorg/disgo/events"
|
||||||
|
"github.com/disgoorg/json"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ var cmd_addemoji Command = Command{
|
|||||||
Definition: discord.SlashCommandCreate{
|
Definition: discord.SlashCommandCreate{
|
||||||
Name: "add-emoji",
|
Name: "add-emoji",
|
||||||
Description: "Add an external emoji directly to the server.",
|
Description: "Add an external emoji directly to the server.",
|
||||||
|
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuildExpressions),
|
||||||
Options: []discord.ApplicationCommandOption{
|
Options: []discord.ApplicationCommandOption{
|
||||||
&discord.ApplicationCommandOptionString{
|
&discord.ApplicationCommandOptionString{
|
||||||
Name: "emoji",
|
Name: "emoji",
|
||||||
|
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"github.com/disgoorg/disgo/discord"
|
"github.com/disgoorg/disgo/discord"
|
||||||
"github.com/disgoorg/disgo/events"
|
"github.com/disgoorg/disgo/events"
|
||||||
|
"github.com/disgoorg/json"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -10,6 +11,7 @@ var cmd_autopublish Command = Command{
|
|||||||
Definition: discord.SlashCommandCreate{
|
Definition: discord.SlashCommandCreate{
|
||||||
Name: "autopublish",
|
Name: "autopublish",
|
||||||
Description: "Toggle automatically publishing every post in a announcement channel",
|
Description: "Toggle automatically publishing every post in a announcement channel",
|
||||||
|
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels),
|
||||||
Contexts: []discord.InteractionContextType{
|
Contexts: []discord.InteractionContextType{
|
||||||
discord.InteractionContextTypeGuild,
|
discord.InteractionContextTypeGuild,
|
||||||
discord.InteractionContextTypePrivateChannel},
|
discord.InteractionContextTypePrivateChannel},
|
||||||
|
@@ -3,27 +3,73 @@ package main
|
|||||||
import (
|
import (
|
||||||
"github.com/disgoorg/disgo/discord"
|
"github.com/disgoorg/disgo/discord"
|
||||||
"github.com/disgoorg/disgo/events"
|
"github.com/disgoorg/disgo/events"
|
||||||
|
"github.com/disgoorg/json"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmd_blockpolls Command = Command{
|
var cmd_blockpolls Command = Command{
|
||||||
Definition: discord.SlashCommandCreate{
|
Definition: discord.SlashCommandCreate{
|
||||||
Name: "block-polls",
|
Name: "block-polls",
|
||||||
Description: "Toggle blocking polls from beeing posted in this channel.",
|
Description: "Block polls from beeing posted in this channel.",
|
||||||
|
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels),
|
||||||
Contexts: []discord.InteractionContextType{
|
Contexts: []discord.InteractionContextType{
|
||||||
discord.InteractionContextTypeGuild,
|
discord.InteractionContextTypeGuild,
|
||||||
discord.InteractionContextTypePrivateChannel},
|
discord.InteractionContextTypePrivateChannel},
|
||||||
IntegrationTypes: []discord.ApplicationIntegrationType{
|
IntegrationTypes: []discord.ApplicationIntegrationType{
|
||||||
discord.ApplicationIntegrationTypeGuildInstall},
|
discord.ApplicationIntegrationTypeGuildInstall},
|
||||||
|
Options: []discord.ApplicationCommandOption{
|
||||||
|
&discord.ApplicationCommandOptionSubCommand{
|
||||||
|
Name: "toggle",
|
||||||
|
Description: "Toggle blocking polls from beeing posted in this channel.",
|
||||||
|
Options: []discord.ApplicationCommandOption{
|
||||||
|
&discord.ApplicationCommandOptionBool{
|
||||||
|
Name: "global",
|
||||||
|
Description: "If polls are blocked server wide or only in the current channel.",
|
||||||
|
},
|
||||||
|
&discord.ApplicationCommandOptionRole{
|
||||||
|
Name: "allowed-role",
|
||||||
|
Description: "The role that bypasses this block role.",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
/*&discord.ApplicationCommandOptionSubCommand{
|
||||||
|
Name: "list",
|
||||||
|
Description: "List the current block polls rules for this server.",
|
||||||
|
},*/
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Interact: func(e *events.ApplicationCommandInteractionCreate) {
|
Interact: func(e *events.ApplicationCommandInteractionCreate) {
|
||||||
if toggleBlockPolls(e.GuildID().String(), e.Channel().ID().String()) {
|
switch *e.SlashCommandInteractionData().SubCommandName {
|
||||||
|
case "toggle":
|
||||||
|
isGlobal := isGlobalBlockPolls(e.GuildID().String())
|
||||||
|
if isGlobal && !e.SlashCommandInteractionData().Bool("global") {
|
||||||
|
e.CreateMessage(discord.NewMessageCreateBuilder().SetContent("Polls are currently globally blocked. Disable global blocking to enable channel specific blocking.").SetEphemeral(true).Build())
|
||||||
|
} else {
|
||||||
|
exists, isGlobal := toggleBlockPolls(e.GuildID().String(), e.Channel().ID().String(), e.SlashCommandInteractionData().Bool("global"), e.SlashCommandInteractionData().Role("allowed-role").ID.String())
|
||||||
|
if exists {
|
||||||
|
if e.SlashCommandInteractionData().Bool("global") {
|
||||||
|
err := e.CreateMessage(discord.NewMessageCreateBuilder().
|
||||||
|
SetContent("Polls are now globally unblocked.").SetEphemeral(true).
|
||||||
|
Build())
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
err := e.CreateMessage(discord.NewMessageCreateBuilder().
|
err := e.CreateMessage(discord.NewMessageCreateBuilder().
|
||||||
SetContent("Polls are now unblocked in " + discord.ChannelMention(e.Channel().ID())).SetEphemeral(true).
|
SetContent("Polls are now unblocked in " + discord.ChannelMention(e.Channel().ID())).SetEphemeral(true).
|
||||||
Build())
|
Build())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if isGlobal {
|
||||||
|
err := e.CreateMessage(discord.NewMessageCreateBuilder().
|
||||||
|
SetContent("Polls are now globally blocked.").SetEphemeral(true).
|
||||||
|
Build())
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
err := e.CreateMessage(discord.NewMessageCreateBuilder().
|
err := e.CreateMessage(discord.NewMessageCreateBuilder().
|
||||||
SetContent("Polls are now blocked in " + discord.ChannelMention(e.Channel().ID())).SetEphemeral(true).
|
SetContent("Polls are now blocked in " + discord.ChannelMention(e.Channel().ID())).SetEphemeral(true).
|
||||||
@@ -32,5 +78,10 @@ var cmd_blockpolls Command = Command{
|
|||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*case "list":
|
||||||
|
list := listBlockPolls(e.GuildID().String())*/
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -171,7 +171,13 @@ func messageCreate(e *events.MessageCreate) {
|
|||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
if channel != nil {
|
if channel != nil {
|
||||||
if isBlockPollsEnabled(e.GuildID.String(), e.Message.ChannelID.String()) && messageIsPoll(e.Message.ChannelID.String(), e.Message.ID.String(), e.Client()) {
|
isBlockPollsEnabledGlobal := isGlobalBlockPolls(e.GuildID.String())
|
||||||
|
isBlockPollsEnabled, allowedRole := getBlockPollsEnabled(e.GuildID.String(), e.Message.ChannelID.String())
|
||||||
|
var hasAllowedRole bool
|
||||||
|
if allowedRole != "" {
|
||||||
|
hasAllowedRole = slices.Contains(e.Message.Member.RoleIDs, snowflake.MustParse(allowedRole))
|
||||||
|
}
|
||||||
|
if (isBlockPollsEnabledGlobal || isBlockPollsEnabled) && !hasAllowedRole && messageIsPoll(e.Message.ChannelID.String(), e.Message.ID.String(), e.Client()) {
|
||||||
e.Client().Rest().DeleteMessage(e.Message.ChannelID, e.Message.ID)
|
e.Client().Rest().DeleteMessage(e.Message.ChannelID, e.Message.ID)
|
||||||
}
|
}
|
||||||
if channel.Type() == discord.ChannelTypeGuildNews {
|
if channel.Type() == discord.ChannelTypeGuildNews {
|
||||||
|
@@ -57,8 +57,10 @@ func initTables() {
|
|||||||
);
|
);
|
||||||
CREATE TABLE IF NOT EXISTS blockpolls (
|
CREATE TABLE IF NOT EXISTS blockpolls (
|
||||||
guild_id TEXT NOT NULL,
|
guild_id TEXT NOT NULL,
|
||||||
channel_id TEXT NOT NULL,
|
channel_id TEXT,
|
||||||
PRIMARY KEY (guild_id, channel_id)
|
global BOOLEAN,
|
||||||
|
allowed_role TEXT,
|
||||||
|
PRIMARY KEY (guild_id)
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
_, err := db.Exec(createTableQuery)
|
_, err := db.Exec(createTableQuery)
|
||||||
@@ -66,15 +68,11 @@ func initTables() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if slices.Contains(os.Args, "--form-db-update") {
|
if slices.Contains(os.Args, "--form-db-update") {
|
||||||
_, err := db.Exec("ALTER TABLE form_manage ADD moderator_id TEXT;")
|
_, err = db.Exec("ALTER TABLE blockpolls ADD global BOOLEAN;")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
_, err = db.Exec("ALTER TABLE form_manage ADD comment_category TEXT;")
|
_, err = db.Exec("ALTER TABLE blockpolls ADD allowed_role TEXT;")
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
_, err = db.Exec("ALTER TABLE form_manage DROP COLUMN mods_can_comment;")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -89,6 +87,12 @@ type FormResult struct {
|
|||||||
ModeratorID string
|
ModeratorID string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BlockPoll struct {
|
||||||
|
ChannelID string
|
||||||
|
Global bool
|
||||||
|
AllowedRole string
|
||||||
|
}
|
||||||
|
|
||||||
func addTag(guildID, tagName, tagContent string) bool {
|
func addTag(guildID, tagName, tagContent string) bool {
|
||||||
var exists bool = true
|
var exists bool = true
|
||||||
//TODO: add modify command
|
//TODO: add modify command
|
||||||
@@ -400,33 +404,84 @@ func isAutopublishEnabled(guildID string, newsChannelID string) bool {
|
|||||||
return enabled
|
return enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
func toggleBlockPolls(guildID string, channelID string) bool {
|
func isGlobalBlockPolls(guildID string) bool {
|
||||||
|
var globalexists bool
|
||||||
|
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM blockpolls WHERE guild_id = $1 AND global = true)", guildID).Scan(&globalexists)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
return globalexists
|
||||||
|
}
|
||||||
|
|
||||||
|
func toggleBlockPolls(guildID string, channelID string, global bool, allowedRole string) (e bool, isGlobal bool) {
|
||||||
|
globalexists := isGlobalBlockPolls(guildID)
|
||||||
var exists bool
|
var exists bool
|
||||||
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM blockpolls WHERE guild_id = $1 AND channel_id = $2)", guildID, channelID).Scan(&exists)
|
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM blockpolls WHERE guild_id = $1 AND channel_id = $2)", guildID, channelID).Scan(&exists)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
if exists {
|
if globalexists {
|
||||||
|
_, err := db.Exec("DELETE FROM blockpolls WHERE guild_id = $1 AND global = true", guildID)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
return true, true
|
||||||
|
} else if global {
|
||||||
|
_, err = db.Exec("DELETE FROM blockpolls WHERE guild_id = $1", guildID)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
_, err := db.Exec("INSERT INTO blockpolls (guild_id, global, channel_id, allowed_role) VALUES ($1, true, $2, $3)", guildID, channelID, allowedRole)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
return false, true
|
||||||
|
} else if exists && !globalexists {
|
||||||
_, err := db.Exec("DELETE FROM blockpolls WHERE guild_id = $1 AND channel_id = $2", guildID, channelID)
|
_, err := db.Exec("DELETE FROM blockpolls WHERE guild_id = $1 AND channel_id = $2", guildID, channelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
} else {
|
return true, false
|
||||||
_, err := db.Exec("INSERT INTO blockpolls (guild_id, channel_id) VALUES ($1, $2)", guildID, channelID)
|
} else if !globalexists {
|
||||||
|
_, err := db.Exec("INSERT INTO blockpolls (guild_id, channel_id, allowed_role) VALUES ($1, $2, $3)", guildID, channelID, allowedRole)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
|
return false, false
|
||||||
|
} else {
|
||||||
|
return false, false
|
||||||
}
|
}
|
||||||
return exists
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isBlockPollsEnabled(guildID string, channelID string) bool {
|
func listBlockPolls(guildID string) []BlockPoll {
|
||||||
|
var list []BlockPoll
|
||||||
|
rows, err := db.Query("SELECT channel_id, global, allowed_role FROM blockpolls WHERE guild_id = $1", guildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
for rows.Next() {
|
||||||
|
var bp BlockPoll
|
||||||
|
err := rows.Scan(&bp.ChannelID, &bp.Global, &bp.AllowedRole)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
list = append(list, bp)
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBlockPollsEnabled(guildID string, channelID string) (isEnabled bool, allowedRole string) {
|
||||||
var enabled bool
|
var enabled bool
|
||||||
|
var v_allowedRole string
|
||||||
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM blockpolls WHERE guild_id = $1 AND channel_id = $2)", guildID, channelID).Scan(&enabled)
|
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM blockpolls WHERE guild_id = $1 AND channel_id = $2)", guildID, channelID).Scan(&enabled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
return enabled
|
err = db.QueryRow("SELECT allowed_role FROM blockpolls WHERE guild_id = $1 AND channel_id = $2", guildID, channelID).Scan(&v_allowedRole)
|
||||||
|
if err != nil && err.Error() != "sql: no rows in result set" {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
return enabled, v_allowedRole
|
||||||
}
|
}
|
||||||
|
|
||||||
func tryDeleteUnusedMessage(messageID string) {
|
func tryDeleteUnusedMessage(messageID string) {
|
||||||
|
Reference in New Issue
Block a user