finished plugin system base
This commit is contained in:
34
plugin_src/autopublish/data.go
Normal file
34
plugin_src/autopublish/data.go
Normal 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
|
||||
}
|
23
plugin_src/autopublish/listener.go
Normal file
23
plugin_src/autopublish/listener.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
76
plugin_src/autopublish/main.go
Normal file
76
plugin_src/autopublish/main.go
Normal 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)
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
Reference in New Issue
Block a user