fixed some rare boot looping

This commit is contained in:
2024-06-08 23:54:28 +02:00
parent a5fdf699d6
commit 6e6a613893
7 changed files with 116 additions and 66 deletions

View File

@@ -12,7 +12,7 @@
- `/cat` - Fetches a random cat picture.
- `/dadjoke` - Retrieves a random dad joke.
- `/ask` - Receive a gif response to your query, akin to an 8-ball.
- `/ping` - Displays the bot's current ping.
- `/ping` - Displays the app's current ping.
![Forms Screenshot](https://raw.githubusercontent.com/vaporvee/acecore/main/custom/images/screenshots/forms.png)

46
cmd_pluginmanage.go Normal file
View File

@@ -0,0 +1,46 @@
package main
import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/json"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/custom"
"github.com/vaporvee/acecore/shared"
)
var cmd_pluginmanage shared.Command = shared.Command{
Definition: discord.SlashCommandCreate{
Name: "plugin",
Description: "Manage the plugins for this bot.",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionAdministrator),
Options: []discord.ApplicationCommandOption{
&discord.ApplicationCommandOptionSubCommand{
Name: "list",
Description: "List all installed plugins for this bot.",
},
},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
app, err := e.Client().Rest().GetCurrentApplication()
if err != nil {
logrus.Error(err)
return
}
if app.Owner.ID == e.User().ID {
switch *e.SlashCommandInteractionData().SubCommandName {
case "list":
var fields []discord.EmbedField
for _, name := range pluginNames {
fields = append(fields, discord.EmbedField{Name: name})
}
e.CreateMessage(discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
SetTitle("Plugins").SetDescription("These are the currently installed plugins for this bot.").SetFields(fields...).SetColor(custom.GetColor("primary")).
Build()).SetEphemeral(true).Build())
}
} else {
e.CreateMessage(discord.NewMessageCreateBuilder().SetContent("You are not the owner of this bot.").SetEphemeral(true).Build())
}
},
}

View File

@@ -1,4 +1,6 @@
BOT_TOKEN="<Your bot token>"
OWNER_GUILD="<Your bot manage guild id>"
OWNER_USER="<Your user id>"
DB_USER="postgres"
DB_PASSWORD="<Your db password>"
DB_SERVER="localhost"

4
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/vaporvee/acecore
go 1.22.1
require (
github.com/disgoorg/disgo v0.18.2
github.com/disgoorg/disgo v0.18.7
github.com/disgoorg/json v1.1.0
github.com/disgoorg/snowflake/v2 v2.0.1
github.com/google/uuid v1.6.0
@@ -21,4 +21,4 @@ require (
golang.org/x/sys v0.17.0 // indirect
)
replace github.com/vaporvee/acecore/shared => ./shared
replace github.com/vaporvee/acecore/shared => ./shared

2
go.sum
View File

@@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disgoorg/disgo v0.18.2 h1:pZCvaFamfHcnXrj0XA73qtVofP0R8dYEyQfPNgv8dLE=
github.com/disgoorg/disgo v0.18.2/go.mod h1:gkl6DBdbKUvmOOJayWPSvS52KPN/8uJGJ2f13gCEB1o=
github.com/disgoorg/disgo v0.18.7 h1:Xg5eiOdSo+wR3CDMIPh9Vmykdkwk/rdcs00vhr2U6m0=
github.com/disgoorg/disgo v0.18.7/go.mod h1:gkl6DBdbKUvmOOJayWPSvS52KPN/8uJGJ2f13gCEB1o=
github.com/disgoorg/json v1.1.0 h1:7xigHvomlVA9PQw9bMGO02PHGJJPqvX5AnwlYg/Tnys=
github.com/disgoorg/json v1.1.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
github.com/disgoorg/snowflake/v2 v2.0.1 h1:CuUxGLwggUxEswZOmZ+mZ5i0xSumQdXW9tXW7uGqe+0=

View File

@@ -1,52 +1,16 @@
package main
import (
"fmt"
"os"
"slices"
"strings"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/shared"
)
var commands []shared.Command
func ready(e *events.Ready) {
logrus.Info("Starting up...")
removeOldCommandFromAllGuilds(e.Client())
var existingCommandNames []string
existingCommands, err := e.Client().Rest().GetGlobalCommands(e.Client().ApplicationID(), false)
if err != nil {
logrus.Errorf("error fetching existing global commands: %v", err)
} else {
for _, existingCommand := range existingCommands {
existingCommandNames = append(existingCommandNames, existingCommand.Name())
}
}
globalCommands := []discord.ApplicationCommandCreate{}
for _, command := range commands {
if !slices.Contains(existingCommandNames, command.Definition.CommandName()) || slices.Contains(os.Args, "--update-all") || slices.Contains(os.Args, "--clean") {
globalCommands = append(globalCommands, command.Definition)
logrus.Infof("Appending command \"%s\"", command.Definition.CommandName())
}
}
if len(globalCommands) > 0 {
logrus.Infof("Attempting to add global commands %s", fmt.Sprint(globalCommands))
_, err = e.Client().Rest().SetGlobalCommands(e.Client().ApplicationID(), globalCommands)
if err != nil {
logrus.Errorf("error creating global commands '%s'", err)
} else {
logrus.Infof("Added global commands sucessfully!")
}
}
logrus.Info("Successfully started the Bot!")
}
func applicationCommandInteractionCreate(e *events.ApplicationCommandInteractionCreate) {
for _, command := range commands {
if command.Interact != nil && e.Data.CommandName() == command.Definition.CommandName() {
@@ -106,28 +70,3 @@ func modalSubmitInteractionCreate(e *events.ModalSubmitInteractionCreate) {
}
}
}
func removeOldCommandFromAllGuilds(c bot.Client) {
app, err := c.Rest().GetCurrentApplication()
if err != nil {
logrus.Error(err)
}
globalCommands, err := c.Rest().GetGlobalCommands(app.Bot.ID, false)
if err != nil {
logrus.Errorf("error fetching existing global commands: %v", err)
return
}
var commandNames []string
for _, command := range commands {
commandNames = append(commandNames, command.Definition.CommandName())
}
for _, existingCommand := range globalCommands {
if !slices.Contains(commandNames, existingCommand.Name()) {
logrus.Infof("Deleting command '%s'", existingCommand.Name())
err := c.Rest().DeleteGlobalCommand(c.ApplicationID(), existingCommand.ID())
if err != nil {
logrus.Errorf("error deleting command %s: %v", existingCommand.Name(), err)
}
}
}
}

65
main.go
View File

@@ -11,6 +11,7 @@ import (
"path/filepath"
"plugin"
"runtime"
"slices"
"strconv"
"syscall"
"time"
@@ -19,6 +20,7 @@ import (
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/gateway"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
@@ -31,7 +33,7 @@ var (
db *sql.DB
)
var listeners []func()
var pluginNames []string
func main() {
logrusInitFile()
@@ -47,7 +49,6 @@ func main() {
logrus.Warn(err)
}
shared.BotConfigs = append(shared.BotConfigs,
bot.WithEventListenerFunc(ready),
bot.WithEventListenerFunc(applicationCommandInteractionCreate),
bot.WithEventListenerFunc(autocompleteInteractionCreate),
bot.WithEventListenerFunc(componentInteractionCreate),
@@ -80,6 +81,7 @@ func main() {
logrus.Error(err)
}
logrus.Infof("Bot is now running as '%s'!", app.Bot.Username)
registerCommands(client)
go web.HostRoutes(app.Bot.ID.String())
sc := make(chan os.Signal, 1)
@@ -88,6 +90,62 @@ func main() {
logrus.Info("Shutting down...")
}
func registerCommands(c bot.Client) {
logrus.Info("Starting up...")
removeOldCommandFromAllGuilds(c)
var existingCommandNames []string
existingCommands, err := c.Rest().GetGlobalCommands(c.ApplicationID(), false)
if err != nil {
logrus.Errorf("error fetching existing global commands: %v", err)
} else {
for _, existingCommand := range existingCommands {
existingCommandNames = append(existingCommandNames, existingCommand.Name())
}
}
globalCommands := []discord.ApplicationCommandCreate{}
for _, command := range commands {
if !slices.Contains(existingCommandNames, command.Definition.CommandName()) || slices.Contains(os.Args, "--update-all") || slices.Contains(os.Args, "--clean") {
globalCommands = append(globalCommands, command.Definition)
logrus.Infof("Appending command \"%s\"", command.Definition.CommandName())
}
}
if len(globalCommands) > 0 {
logrus.Infof("Attempting to add global commands %s", fmt.Sprint(globalCommands))
_, err = c.Rest().SetGlobalCommands(c.ApplicationID(), globalCommands)
if err != nil {
logrus.Errorf("error creating global commands '%s'", err)
} else {
logrus.Infof("Added global commands sucessfully!")
}
}
logrus.Info("Successfully started the Bot!")
}
func removeOldCommandFromAllGuilds(c bot.Client) {
app, err := c.Rest().GetCurrentApplication()
if err != nil {
logrus.Error(err)
}
globalCommands, err := c.Rest().GetGlobalCommands(app.Bot.ID, false)
if err != nil {
logrus.Errorf("error fetching existing global commands: %v", err)
return
}
var commandNames []string
for _, command := range commands {
commandNames = append(commandNames, command.Definition.CommandName())
}
for _, existingCommand := range globalCommands {
if !slices.Contains(commandNames, existingCommand.Name()) {
logrus.Infof("Deleting command '%s'", existingCommand.Name())
err := c.Rest().DeleteGlobalCommand(c.ApplicationID(), existingCommand.ID())
if err != nil {
logrus.Errorf("error deleting command %s: %v", existingCommand.Name(), err)
}
}
}
}
func logrusInitFile() {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logrus.JSONFormatter{})
@@ -153,6 +211,9 @@ func loadPlugins(directory string) error {
plugin := *pluginPtr
if plugin.Name == "" {
logrus.Warn("Plugin is unnamed")
pluginNames = append(pluginNames, "UNNAMED PLUGIN")
} else {
pluginNames = append(pluginNames, plugin.Name)
}
if plugin.Commands != nil {
commands = append(commands, plugin.Commands...)