added command for blocking polls
This commit is contained in:
36
cmd_blockpolls.go
Normal file
36
cmd_blockpolls.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/disgoorg/disgo/discord"
|
||||||
|
"github.com/disgoorg/disgo/events"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cmd_blockpolls Command = Command{
|
||||||
|
Definition: discord.SlashCommandCreate{
|
||||||
|
Name: "block-polls",
|
||||||
|
Description: "Toggle blocking polls from beeing posted in this channel.",
|
||||||
|
Contexts: []discord.InteractionContextType{
|
||||||
|
discord.InteractionContextTypeGuild,
|
||||||
|
discord.InteractionContextTypePrivateChannel},
|
||||||
|
IntegrationTypes: []discord.ApplicationIntegrationType{
|
||||||
|
discord.ApplicationIntegrationTypeGuildInstall},
|
||||||
|
},
|
||||||
|
Interact: func(e *events.ApplicationCommandInteractionCreate) {
|
||||||
|
if toggleBlockPolls(e.GuildID().String(), e.Channel().ID().String()) {
|
||||||
|
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 {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
@@ -25,7 +25,7 @@ type Command struct {
|
|||||||
DynamicComponentIDs func() []string
|
DynamicComponentIDs func() []string
|
||||||
}
|
}
|
||||||
|
|
||||||
var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag, cmd_sticky, context_sticky, cmd_ping, cmd_userinfo, cmd_addemoji, cmd_form, cmd_ask, cmd_cat, cmd_dadjoke, cmd_ticket_form, cmd_autopublish, cmd_autojoinroles}
|
var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag, cmd_sticky, context_sticky, cmd_ping, cmd_userinfo, cmd_addemoji, cmd_form, cmd_ask, cmd_cat, cmd_dadjoke, cmd_ticket_form, cmd_blockpolls, cmd_autopublish, cmd_autojoinroles}
|
||||||
|
|
||||||
func ready(e *events.Ready) {
|
func ready(e *events.Ready) {
|
||||||
logrus.Info("Starting up...")
|
logrus.Info("Starting up...")
|
||||||
@@ -170,7 +170,11 @@ func messageCreate(e *events.MessageCreate) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
if channel != nil && channel.Type() == discord.ChannelTypeGuildNews {
|
if channel != nil {
|
||||||
|
if isBlockPollsEnabled(e.GuildID.String(), e.Message.ChannelID.String()) && messageIsPoll(e.Message.ChannelID.String(), e.Message.ID.String(), e.Client()) {
|
||||||
|
e.Client().Rest().DeleteMessage(e.Message.ChannelID, e.Message.ID)
|
||||||
|
}
|
||||||
|
if channel.Type() == discord.ChannelTypeGuildNews {
|
||||||
if isAutopublishEnabled(e.GuildID.String(), e.ChannelID.String()) {
|
if isAutopublishEnabled(e.GuildID.String(), e.ChannelID.String()) {
|
||||||
_, err := e.Client().Rest().CrosspostMessage(e.ChannelID, e.MessageID)
|
_, err := e.Client().Rest().CrosspostMessage(e.ChannelID, e.MessageID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -180,6 +184,7 @@ func messageCreate(e *events.MessageCreate) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func messageDelete(e *events.MessageDelete) { //TODO: also clear on bot start when message doesn't exist
|
func messageDelete(e *events.MessageDelete) { //TODO: also clear on bot start when message doesn't exist
|
||||||
tryDeleteUnusedMessage(e.MessageID.String())
|
tryDeleteUnusedMessage(e.MessageID.String())
|
||||||
|
@@ -54,7 +54,13 @@ func initTables() {
|
|||||||
guild_id TEXT NOT NULL,
|
guild_id TEXT NOT NULL,
|
||||||
news_channel_id TEXT NOT NULL,
|
news_channel_id TEXT NOT NULL,
|
||||||
PRIMARY KEY (guild_id, news_channel_id)
|
PRIMARY KEY (guild_id, news_channel_id)
|
||||||
)`
|
);
|
||||||
|
CREATE TABLE IF NOT EXISTS blockpolls (
|
||||||
|
guild_id TEXT NOT NULL,
|
||||||
|
channel_id TEXT NOT NULL,
|
||||||
|
PRIMARY KEY (guild_id, channel_id)
|
||||||
|
)
|
||||||
|
`
|
||||||
_, err := db.Exec(createTableQuery)
|
_, err := db.Exec(createTableQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -394,6 +400,35 @@ func isAutopublishEnabled(guildID string, newsChannelID string) bool {
|
|||||||
return enabled
|
return enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toggleBlockPolls(guildID string, channelID string) 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)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
_, err := db.Exec("DELETE FROM blockpolls WHERE guild_id = $1 AND channel_id = $2", guildID, channelID)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_, err := db.Exec("INSERT INTO blockpolls (guild_id, channel_id) VALUES ($1, $2)", guildID, channelID)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return exists
|
||||||
|
}
|
||||||
|
|
||||||
|
func isBlockPollsEnabled(guildID string, channelID string) bool {
|
||||||
|
var enabled bool
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
return enabled
|
||||||
|
}
|
||||||
|
|
||||||
func tryDeleteUnusedMessage(messageID string) {
|
func tryDeleteUnusedMessage(messageID string) {
|
||||||
_, err := db.Exec("DELETE FROM form_manage WHERE message_id = $1", messageID)
|
_, err := db.Exec("DELETE FROM form_manage WHERE message_id = $1", messageID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
52
tool.go
52
tool.go
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/disgoorg/disgo/bot"
|
"github.com/disgoorg/disgo/bot"
|
||||||
"github.com/disgoorg/disgo/discord"
|
"github.com/disgoorg/disgo/discord"
|
||||||
|
"github.com/disgoorg/disgo/rest"
|
||||||
"github.com/disgoorg/snowflake/v2"
|
"github.com/disgoorg/snowflake/v2"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@@ -36,21 +37,6 @@ type MessageIDs struct {
|
|||||||
ChannelID string
|
ChannelID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func noNullString(in interface{}) string {
|
|
||||||
var s string = ""
|
|
||||||
var is_str bool
|
|
||||||
switch in.(type) {
|
|
||||||
case string:
|
|
||||||
is_str = true
|
|
||||||
case *string:
|
|
||||||
is_str = true
|
|
||||||
}
|
|
||||||
if in != nil && is_str {
|
|
||||||
s = fmt.Sprint(in)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func jsonStringBuildModal(userID string, manageID string, formID string, overwrite ...string) discord.ModalCreate {
|
func jsonStringBuildModal(userID string, manageID string, formID string, overwrite ...string) discord.ModalCreate {
|
||||||
var modal ModalJson = getModalByFormID(formID)
|
var modal ModalJson = getModalByFormID(formID)
|
||||||
var components []discord.ContainerComponent
|
var components []discord.ContainerComponent
|
||||||
@@ -216,3 +202,39 @@ func isGIFImage(imageData []byte) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user