finished plugin system base

This commit is contained in:
2024-04-15 18:17:12 +02:00
parent 31e96c80f4
commit 077054109f
51 changed files with 2218 additions and 2160 deletions

View File

@@ -0,0 +1,34 @@
package main
import (
"github.com/sirupsen/logrus"
)
func toggleAutoPublish(guildID string, newsChannelID string) bool {
var exists bool
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM autopublish WHERE guild_id = $1 AND news_channel_id = $2)", guildID, newsChannelID).Scan(&exists)
if err != nil {
logrus.Error(err)
}
if exists {
_, err := db.Exec("DELETE FROM autopublish WHERE guild_id = $1 AND news_channel_id = $2", guildID, newsChannelID)
if err != nil {
logrus.Error(err)
}
} else {
_, err := db.Exec("INSERT INTO autopublish (guild_id, news_channel_id) VALUES ($1, $2)", guildID, newsChannelID)
if err != nil {
logrus.Error(err)
}
}
return exists
}
func isAutopublishEnabled(guildID string, newsChannelID string) bool {
var enabled bool
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM autopublish WHERE guild_id = $1 AND news_channel_id = $2)", guildID, newsChannelID).Scan(&enabled)
if err != nil {
logrus.Error(err)
}
return enabled
}

View File

@@ -0,0 +1,23 @@
package main
import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"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.Type() == discord.ChannelTypeGuildNews {
if isAutopublishEnabled(e.GuildID.String(), e.ChannelID.String()) {
_, err := e.Client().Rest().CrosspostMessage(e.ChannelID, e.MessageID)
if err != nil {
logrus.Error(err)
return
}
}
}
}

View File

@@ -0,0 +1,76 @@
package main
import (
"database/sql"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"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 autopublish (
guild_id TEXT NOT NULL,
news_channel_id TEXT NOT NULL,
PRIMARY KEY (guild_id, news_channel_id)
);
`
var Plugin = &shared.Plugin{
Name: "Auto Publish",
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: "autopublish",
Description: "Toggle automatically publishing every post in a announcement channel",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels),
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
channel := e.Channel()
if channel.Type() == discord.ChannelTypeGuildNews {
if toggleAutoPublish(e.GuildID().String(), e.Channel().ID().String()) {
err := e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("Autopublishing is now disabled on " + discord.ChannelMention(e.Channel().ID())).SetEphemeral(true).
Build())
if err != nil {
logrus.Error(err)
}
} else {
err := e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("Autopublishing is now enabled on " + discord.ChannelMention(e.Channel().ID())).SetEphemeral(true).
Build())
if err != nil {
logrus.Error(err)
}
}
} else {
err := e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("This is not an announcement channel!").SetEphemeral(true).
Build())
if err != nil {
logrus.Error(err)
}
}
},
},
},
}