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

21
main.go
View File

@@ -15,7 +15,6 @@ 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"
@@ -23,8 +22,6 @@ import (
)
var (
client bot.Client
app *discord.Application
db *sql.DB
)
@@ -47,13 +44,15 @@ func main() {
),
),
bot.WithEventListenerFunc(ready),
//bot.WithEventListenerFunc(applicationCommandInteractionCreate),
//bot.WithEventListenerFunc(autocompleteInteractionCreate),
//bot.WithEventListenerFunc(componentInteractionCreate),
//bot.WithEventListenerFunc(modalSubmitInteractionCreate),
//bot.WithEventListenerFunc(messageCreate),
//bot.WithEventListenerFunc(messageDelete),
//bot.WithEventListenerFunc(guildMemberJoin),
bot.WithEventListenerFunc(applicationCommandInteractionCreate),
bot.WithEventListenerFunc(autocompleteInteractionCreate),
bot.WithEventListenerFunc(componentInteractionCreate),
bot.WithEventListenerFunc(modalSubmitInteractionCreate),
/*
bot.WithEventListenerFunc(messageCreate),
bot.WithEventListenerFunc(messageDelete),
bot.WithEventListenerFunc(guildMemberJoin),
*/
)
if err != nil {
logrus.Fatal("error creating Discord session,", err)
@@ -66,7 +65,7 @@ func main() {
logrus.Error("error opening connection,", err)
return
}
app, err = client.Rest().GetCurrentApplication()
app, err := client.Rest().GetCurrentApplication()
if err != nil {
logrus.Error(err)
}

17
tool.go
View File

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