made getting tags working

This commit is contained in:
2024-04-04 20:14:47 +02:00
parent b85d9d5294
commit 68a99fbb10
4 changed files with 75 additions and 54 deletions

View File

@@ -98,7 +98,7 @@ var cmd_tag_short Command = Command{
} }
var context_tag Command = Command{ var context_tag Command = Command{
Definition: discord.SlashCommandCreate{ Definition: discord.MessageCommandCreate{
Name: "Save as tag", Name: "Save as tag",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild), DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild),
}, },
@@ -130,6 +130,14 @@ func AddTagCommand(e *events.ApplicationCommandInteractionCreate, prevalue strin
MaxLength: 20, MaxLength: 20,
Value: "", Value: "",
}, },
discord.TextInputComponent{
CustomID: "tag_add_modal_content",
Label: "Content",
Style: discord.TextInputStyleParagraph,
Required: true,
MaxLength: 2000,
Value: prevalue,
},
}, },
}, },
}) })

View File

@@ -1,9 +1,12 @@
package main package main
import ( import (
"fmt"
"os"
"slices" "slices"
"strings" "strings"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events" "github.com/disgoorg/disgo/events"
"github.com/disgoorg/snowflake/v2" "github.com/disgoorg/snowflake/v2"
@@ -11,7 +14,7 @@ import (
) )
type Command struct { type Command struct {
Definition discord.SlashCommandCreate Definition discord.ApplicationCommandCreate
Interact func(e *events.ApplicationCommandInteractionCreate) Interact func(e *events.ApplicationCommandInteractionCreate)
Autocomplete func(e *events.AutocompleteInteractionCreate) Autocomplete func(e *events.AutocompleteInteractionCreate)
ComponentInteract func(e *events.ComponentInteractionCreate) ComponentInteract func(e *events.ComponentInteractionCreate)
@@ -27,49 +30,57 @@ var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag /*, cmd_f
func ready(e *events.Ready) { func ready(e *events.Ready) {
logrus.Info("Starting up...") logrus.Info("Starting up...")
//findAndDeleteUnusedMessages() findAndDeleteUnusedMessages(e.Client())
removeOldCommandFromAllGuilds() removeOldCommandFromAllGuilds(e.Client())
/* var existingCommandNames []string
var existingCommandNames []string existingCommands, err := e.Client().Rest().GetGlobalCommands(e.Client().ApplicationID(), false)
existingCommands, err := client.Rest().GetGlobalCommands(client.ApplicationID(), false) if err != nil {
if err != nil { logrus.Errorf("error fetching existing global commands: %v", err)
logrus.Errorf("error fetching existing global commands: %v", err) } else {
} else { for _, existingCommand := range existingCommands {
for _, existingCommand := range existingCommands { existingCommandNames = append(existingCommandNames, existingCommand.Name())
existingCommandNames = append(existingCommandNames, existingCommand.Name())
}
} }
for _, command := range commands { }
if !slices.Contains(existingCommandNames, command.Definition.Name) || slices.Contains(os.Args, "--update="+command.Definition.Name) || slices.Contains(os.Args, "--update=all") || slices.Contains(os.Args, "--clean") { globalCommands := []discord.ApplicationCommandCreate{}
cmd, err := client.Rest().CreateGlobalCommand(client.ApplicationID(), command.Definition) for _, command := range commands {
if err != nil { if !slices.Contains(existingCommandNames, command.Definition.CommandName()) || slices.Contains(os.Args, "--update="+command.Definition.CommandName()) || slices.Contains(os.Args, "--update=all") || slices.Contains(os.Args, "--clean") {
logrus.Errorf("error creating global command '%s': %v", cmd.Name(), err) globalCommands = append(globalCommands, command.Definition)
} else { logrus.Infof("Appending command \"%s\"", command.Definition.CommandName())
logrus.Infof("Added global command '%s'", cmd.Name())
}
}
} }
logrus.Info("Successfully started the Bot!") }
*/ 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) { func applicationCommandInteractionCreate(e *events.ApplicationCommandInteractionCreate) {
app, err := e.Client().Rest().GetCurrentApplication()
if err != nil {
logrus.Error(err)
}
for _, command := range commands { for _, command := range commands {
if command.Interact != nil && e.SlashCommandInteractionData().CommandName() == command.Definition.Name { if command.Interact != nil && e.SlashCommandInteractionData().CommandName() == command.Definition.CommandName() {
if !command.AllowDM && e.SlashCommandInteractionData().GuildID().String() == "" { if !command.AllowDM && app.GuildID.String() == "" {
e.CreateMessage(discord.NewMessageCreateBuilder(). e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("This command is not available in DMs.").SetEphemeral(true). SetContent("This command is not available in DMs.").SetEphemeral(true).
Build()) Build())
} else { } else {
command.Interact(e) command.Interact(e)
} }
} }
} }
} }
func autocompleteInteractionCreate(e *events.AutocompleteInteractionCreate) { func autocompleteInteractionCreate(e *events.AutocompleteInteractionCreate) {
for _, command := range commands { for _, command := range commands {
if command.Autocomplete != nil && e.Data.CommandName == command.Definition.Name { if command.Autocomplete != nil && e.Data.CommandName == command.Definition.CommandName() {
if !command.AllowDM && e.GuildID().String() == "" { if !command.AllowDM && e.GuildID().String() == "" {
err := e.AutocompleteResult(nil) err := e.AutocompleteResult(nil)
if err != nil { if err != nil {
@@ -137,22 +148,24 @@ func modalSubmitInteractionCreate(e *events.ModalSubmitInteractionCreate) {
} }
} }
func removeOldCommandFromAllGuilds() { func removeOldCommandFromAllGuilds(c bot.Client) {
logrus.Debug(app.Bot.ID.String()) app, err := c.Rest().GetCurrentApplication()
globalCommands, err := client.Rest().GetGlobalCommands(app.Bot.ID, false) if err != nil {
logrus.Debug("HERE") //doesnt get called logrus.Error(err)
}
globalCommands, err := c.Rest().GetGlobalCommands(app.Bot.ID, false)
if err != nil { if err != nil {
logrus.Errorf("error fetching existing global commands: %v", err) logrus.Errorf("error fetching existing global commands: %v", err)
return return
} }
var commandNames []string var commandNames []string
for _, command := range commands { for _, command := range commands {
commandNames = append(commandNames, command.Definition.Name) commandNames = append(commandNames, command.Definition.CommandName())
} }
for _, existingCommand := range globalCommands { for _, existingCommand := range globalCommands {
if slices.Contains(commandNames, existingCommand.Name()) { if !slices.Contains(commandNames, existingCommand.Name()) {
logrus.Infof("Deleting command '%s'", existingCommand.Name()) logrus.Infof("Deleting command '%s'", existingCommand.Name())
err := client.Rest().DeleteGlobalCommand(client.ApplicationID(), existingCommand.ID()) err := c.Rest().DeleteGlobalCommand(c.ApplicationID(), existingCommand.ID())
if err != nil { if err != nil {
logrus.Errorf("error deleting command %s: %v", existingCommand.Name(), err) logrus.Errorf("error deleting command %s: %v", existingCommand.Name(), err)
} }

23
main.go
View File

@@ -15,7 +15,6 @@ import (
"github.com/disgoorg/disgo" "github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot" "github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/gateway" "github.com/disgoorg/disgo/gateway"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -23,9 +22,7 @@ import (
) )
var ( var (
client bot.Client db *sql.DB
app *discord.Application
db *sql.DB
) )
func main() { func main() {
@@ -47,13 +44,15 @@ func main() {
), ),
), ),
bot.WithEventListenerFunc(ready), bot.WithEventListenerFunc(ready),
//bot.WithEventListenerFunc(applicationCommandInteractionCreate), bot.WithEventListenerFunc(applicationCommandInteractionCreate),
//bot.WithEventListenerFunc(autocompleteInteractionCreate), bot.WithEventListenerFunc(autocompleteInteractionCreate),
//bot.WithEventListenerFunc(componentInteractionCreate), bot.WithEventListenerFunc(componentInteractionCreate),
//bot.WithEventListenerFunc(modalSubmitInteractionCreate), bot.WithEventListenerFunc(modalSubmitInteractionCreate),
//bot.WithEventListenerFunc(messageCreate), /*
//bot.WithEventListenerFunc(messageDelete), bot.WithEventListenerFunc(messageCreate),
//bot.WithEventListenerFunc(guildMemberJoin), bot.WithEventListenerFunc(messageDelete),
bot.WithEventListenerFunc(guildMemberJoin),
*/
) )
if err != nil { if err != nil {
logrus.Fatal("error creating Discord session,", err) logrus.Fatal("error creating Discord session,", err)
@@ -66,7 +65,7 @@ func main() {
logrus.Error("error opening connection,", err) logrus.Error("error opening connection,", err)
return return
} }
app, err = client.Rest().GetCurrentApplication() app, err := client.Rest().GetCurrentApplication()
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} }

17
tool.go
View File

@@ -9,6 +9,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/discord"
"github.com/disgoorg/snowflake/v2" "github.com/disgoorg/snowflake/v2"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -101,12 +102,12 @@ func getModalByFormID(formID string) ModalJson {
return modal return modal
} }
func getHighestRole(guildID string) (*discord.Role, error) { func getHighestRole(guildID string, c bot.Client) (*discord.Role, error) {
botmember, err := client.Rest().GetMember(snowflake.MustParse(guildID), client.ApplicationID()) botmember, err := c.Rest().GetMember(snowflake.MustParse(guildID), c.ApplicationID())
if err != nil { if err != nil {
return nil, err return nil, err
} }
roles, err := client.Rest().GetRoles(snowflake.MustParse(guildID)) roles, err := c.Rest().GetRoles(snowflake.MustParse(guildID))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -165,21 +166,21 @@ func simpleGetFromAPI(key string, url string) interface{} {
return result[key] return result[key]
} }
func findAndDeleteUnusedMessages() { func findAndDeleteUnusedMessages(c bot.Client) {
for _, message := range getAllSavedMessages() { for _, message := range getAllSavedMessages() {
_, err := client.Rest().GetMessage(snowflake.MustParse(message.ChannelID), snowflake.MustParse(message.ID)) _, err := c.Rest().GetMessage(snowflake.MustParse(message.ChannelID), snowflake.MustParse(message.ID))
if err != nil { if err != nil {
tryDeleteUnusedMessage(message.ID) tryDeleteUnusedMessage(message.ID)
} }
} }
} }
func isIDRole(guildID snowflake.ID, id snowflake.ID) bool { func isIDRole(c bot.Client, guildID snowflake.ID, id snowflake.ID) bool {
_, err1 := client.Rest().GetMember(guildID, id) _, err1 := c.Rest().GetMember(guildID, id)
if err1 == nil { if err1 == nil {
return false return false
} }
roles, err2 := client.Rest().GetRoles(guildID) roles, err2 := c.Rest().GetRoles(guildID)
if err2 == nil { if err2 == nil {
for _, role := range roles { for _, role := range roles {
if role.ID == id { if role.ID == id {