replaced by discords native support

This commit is contained in:
2024-04-18 14:29:46 +02:00
parent 077054109f
commit 2fef5fb6de
3 changed files with 0 additions and 276 deletions

View File

@@ -1,93 +0,0 @@
package main
import (
"log"
"github.com/sirupsen/logrus"
)
type BlockPoll struct {
ChannelID string
Global bool
AllowedRole string
}
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
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM blockpolls WHERE guild_id = $1 AND channel_id = $2)", guildID, channelID).Scan(&exists)
if err != nil {
logrus.Error(err)
}
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)
if err != nil {
logrus.Error(err)
}
return true, false
} 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 {
logrus.Error(err)
}
return false, false
} else {
return false, false
}
}
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 v_allowedRole string
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM blockpolls WHERE guild_id = $1 AND channel_id = $2)", guildID, channelID).Scan(&enabled)
if err != nil {
logrus.Error(err)
}
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
}

View File

@@ -1,27 +0,0 @@
package main
import (
"slices"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/snowflake/v2"
"github.com/sirupsen/logrus"
)
func messageCreate(e *events.MessageCreate) {
channel, err := e.Client().Rest().GetChannel(e.Message.ChannelID)
if err != nil {
logrus.Error(err)
}
if channel != nil {
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)
}
}
}

View File

@@ -1,156 +0,0 @@
package main
import (
"database/sql"
"io"
"net/http"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/json"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/shared"
)
var db *sql.DB
var dbCreateQuery string = `
CREATE TABLE IF NOT EXISTS blockpolls (
guild_id TEXT NOT NULL,
channel_id TEXT,
global BOOLEAN,
allowed_role TEXT,
PRIMARY KEY (guild_id)
)
`
var Plugin = &shared.Plugin{
Name: "Block Polls",
Init: func(d *sql.DB) error {
db = d
_, err := d.Exec(dbCreateQuery)
if err != nil {
return err
}
shared.BotConfigs = append(shared.BotConfigs, bot.WithEventListenerFunc(messageCreate))
return nil
},
Commands: []shared.Command{
{
Definition: discord.SlashCommandCreate{
Name: "block-polls",
Description: "Block polls from beeing posted in this channel.",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels),
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
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) {
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().
SetContent("Polls are now unblocked in " + discord.ChannelMention(e.Channel().ID())).SetEphemeral(true).
Build())
if err != nil {
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 {
err := e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("Polls are now blocked in " + discord.ChannelMention(e.Channel().ID())).SetEphemeral(true).
Build())
if err != nil {
logrus.Error(err)
}
}
}
}
/*case "list":
list := listBlockPolls(e.GuildID().String())*/
}
},
},
},
}
func messageIsPoll(channelID string, messageID string, client bot.Client) bool {
url := rest.DefaultConfig().URL + "/channels/" + channelID + "/messages/" + messageID
req, err := http.NewRequest("GET", url, nil)
if err != nil {
logrus.Error(err)
return false
}
auth := "Bot " + client.Token()
req.Header.Set("Authorization", auth)
resp, err := client.Rest().HTTPClient().Do(req)
if err != nil {
logrus.Error(err)
return false
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
logrus.Error(err)
return false
}
var data map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
logrus.Error(err)
return false
}
_, ok := data["poll"]
return ok
}