continued porting to disgo

This commit is contained in:
2024-04-04 02:18:33 +02:00
parent 8579328ee4
commit 9738ab3901
12 changed files with 101 additions and 94 deletions

View File

@@ -1,5 +1,6 @@
package main package main
/*
import ( import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -31,3 +32,4 @@ var cmd_ask Command = Command{
}, },
AllowDM: true, AllowDM: true,
} }
*/

View File

@@ -1,10 +1,6 @@
package main package main
import ( /*
"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
)
var cmd_autojoinroles Command = Command{ var cmd_autojoinroles Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "autojoinroles", Name: "autojoinroles",
@@ -66,3 +62,4 @@ var cmd_autojoinroles Command = Command{
purgeUnusedAutoJoinRoles(i.GuildID) purgeUnusedAutoJoinRoles(i.GuildID)
}, },
} }
*/

View File

@@ -1,5 +1,6 @@
package main package main
/*
import ( import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -32,3 +33,4 @@ var cmd_autopublish Command = Command{
} }
}, },
} }
*/

View File

@@ -1,14 +1,6 @@
package main package main
import ( /*
"encoding/json"
"io"
"net/http"
"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
)
var cmd_cat Command = Command{ var cmd_cat Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "cat", Name: "cat",
@@ -52,3 +44,4 @@ func GetCatImageURL() (string, error) {
return "https://cataas.com/cat/" + images.ID, err return "https://cataas.com/cat/" + images.ID, err
} }
*/

View File

@@ -1,5 +1,6 @@
package main package main
/*
import ( import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -18,3 +19,4 @@ var cmd_dadjoke Command = Command{
}, },
AllowDM: true, AllowDM: true,
} }
*/

View File

@@ -1,15 +1,6 @@
package main package main
import ( /*
"bytes"
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
var cmd_form Command = Command{ var cmd_form Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "form", Name: "form",
@@ -572,3 +563,4 @@ func getFormButtonIDs() []string {
} }
return IDs return IDs
} }
*/

View File

@@ -1,5 +1,6 @@
package main package main
/*
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
@@ -232,3 +233,4 @@ func parseRetryAfterHeader(headers http.Header) time.Duration {
return time.Duration(retryAfter) * time.Millisecond return time.Duration(retryAfter) * time.Millisecond
} }
*/

View File

@@ -1,5 +1,6 @@
package main package main
/*
import ( import (
"fmt" "fmt"
"net/http" "net/http"
@@ -48,3 +49,4 @@ var cmd_ping Command = Command{
}, },
AllowDM: true, AllowDM: true,
} }
*/

View File

@@ -1,5 +1,6 @@
package main package main
/*
import ( import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -99,3 +100,4 @@ func inputStickyMessage(i *discordgo.InteractionCreate) {
} }
} }
} }
*/

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"github.com/bwmarrin/discordgo"
"github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events" "github.com/disgoorg/disgo/events"
"github.com/disgoorg/json" "github.com/disgoorg/json"
@@ -49,10 +48,12 @@ var cmd_tag Command = Command{
case "get": case "get":
GetTagCommand(e) GetTagCommand(e)
case "add": case "add":
AddTagCommand(i, "") AddTagCommand(e, "")
case "remove": case "remove":
removeTag(i.GuildID, i.ApplicationCommandData().Options[0].Options[0].StringValue()) removeTag(e.GuildID().String(), e.SlashCommandInteractionData().String("tag"))
err := respond(i.Interaction, "Tag removed!", true) err := e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("Tag removed!").SetEphemeral(true).
Build())
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} }
@@ -76,12 +77,11 @@ var cmd_tag Command = Command{
} }
var cmd_tag_short Command = Command{ var cmd_tag_short Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discord.SlashCommandCreate{
Name: "g", Name: "g",
Description: "A short command to get presaved messages.", Description: "A short command to get presaved messages.",
Options: []*discordgo.ApplicationCommandOption{ Options: []discord.ApplicationCommandOption{
{ discord.ApplicationCommandOptionString{
Type: discordgo.ApplicationCommandOptionString,
Name: "tag", Name: "tag",
Description: "Your predefined tag for the saved message", Description: "Your predefined tag for the saved message",
Required: true, Required: true,
@@ -89,86 +89,95 @@ var cmd_tag_short Command = Command{
}, },
}, },
}, },
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Interact: func(e *events.ApplicationCommandInteractionCreate) {
GetTagCommand(i, i.ApplicationCommandData().Options[0]) GetTagCommand(e)
}, },
Autocomplete: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Autocomplete: func(e *events.AutocompleteInteractionCreate) {
AutocompleteTag(i) AutocompleteTag(e)
}, },
} }
var context_tag Command = Command{ var context_tag Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discord.SlashCommandCreate{
Type: discordgo.MessageApplicationCommand,
Name: "Save as tag", Name: "Save as tag",
DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageServer), DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild),
}, },
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { Interact: func(e *events.ApplicationCommandInteractionCreate) {
AddTagCommand(i, i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content) AddTagCommand(e, e.SlashCommandInteractionData().String(""))
}, },
} }
func GetTagCommand(e *events.ApplicationCommandInteractionCreate) { func GetTagCommand(e *events.ApplicationCommandInteractionCreate) {
err := e.CreateMessage(discord.NewMessageCreateBuilder().SetContent(getTagContent(guildID, option)).SetEphemeral(true).Build()) err := e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent(getTagContent(e.GuildID().String(), e.SlashCommandInteractionData().String("tag"))).
Build())
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} }
} }
func AddTagCommand(i *discordgo.InteractionCreate, prevalue string) { func AddTagCommand(e *events.ApplicationCommandInteractionCreate, prevalue string) {
err := bot.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err := e.Modal(discord.ModalCreate{
Type: discordgo.InteractionResponseModal, CustomID: "tag_add_modal" + e.User().ID.String(),
Data: &discordgo.InteractionResponseData{ Title: "Add a custom tag command",
CustomID: "tag_add_modal" + i.Interaction.Member.User.ID, Components: []discord.ContainerComponent{
Title: "Add a custom tag command", discord.ActionRowComponent{
Components: []discordgo.MessageComponent{ discord.TextInputComponent{
discordgo.ActionsRow{ CustomID: "tag_add_modal_name",
Components: []discordgo.MessageComponent{ Label: "Name",
discordgo.TextInput{ Style: discord.TextInputStyleShort,
CustomID: "tag_add_modal_name", Required: true,
Label: "Name", MaxLength: 20,
Style: discordgo.TextInputShort, Value: "",
Required: true,
MaxLength: 20,
Value: "",
},
},
},
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: "tag_add_modal_content",
Label: "Content",
Placeholder: "Content that gets returned when the tag will be run",
Style: discordgo.TextInputParagraph,
Required: true,
MaxLength: 2000,
Value: prevalue,
},
},
}, },
}, },
}, },
}) })
if err != nil { /*Data: &discordgo.InteractionResponseData{
logrus.Error(err) CustomID: "tag_add_modal" + i.Interaction.Member.User.ID,
} Title: "Add a custom tag command",
} Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
func AutocompleteTag(i *discordgo.InteractionCreate) { Components: []discordgo.MessageComponent{
err := bot.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ discordgo.TextInput{
Type: discordgo.InteractionApplicationCommandAutocompleteResult, CustomID: "tag_add_modal_name",
Data: &discordgo.InteractionResponseData{ Label: "Name",
Choices: generateTagChoices(i.GuildID), Style: discordgo.TextInputShort,
Required: true,
MaxLength: 20,
Value: "",
},
},
},
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: "tag_add_modal_content",
Label: "Content",
Placeholder: "Content that gets returned when the tag will be run",
Style: discordgo.TextInputParagraph,
Required: true,
MaxLength: 2000,
Value: prevalue,
},
},
},
}, },
}) },*/
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} }
} }
func generateTagChoices(guildID string) []*discordgo.ApplicationCommandOptionChoice { func AutocompleteTag(e *events.AutocompleteInteractionCreate) {
choices := []*discordgo.ApplicationCommandOptionChoice{} err := e.AutocompleteResult(generateTagChoices(e.GuildID().String()))
if err != nil {
logrus.Error(err)
}
}
func generateTagChoices(guildID string) []discord.AutocompleteChoice {
choices := []discord.AutocompleteChoice{}
IDs, err := getTagIDs(guildID) IDs, err := getTagIDs(guildID)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
@@ -176,7 +185,7 @@ func generateTagChoices(guildID string) []*discordgo.ApplicationCommandOptionCho
} }
for _, id := range IDs { for _, id := range IDs {
id_name := getTagName(guildID, id) id_name := getTagName(guildID, id)
choices = append(choices, &discordgo.ApplicationCommandOptionChoice{ choices = append(choices, &discord.AutocompleteChoiceString{
Name: id_name, Name: id_name,
Value: id, Value: id,
}) })

View File

@@ -24,7 +24,7 @@ type Command struct {
AllowDM bool AllowDM bool
} }
var commands []Command = []Command{cmd_form, cmd_ticket_form, cmd_tag, cmd_tag_short, cmd_dadjoke, cmd_ping, cmd_ask, cmd_sticky, cmd_cat, cmd_autojoinroles, cmd_autopublish, context_sticky, context_tag, cmd_userinfo} var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag /*, cmd_form, cmd_ticket_form, cmd_dadjoke, cmd_ping, cmd_ask, cmd_sticky, cmd_cat, cmd_autojoinroles, cmd_autopublish, context_sticky, cmd_userinfo*/}
func ready(e *events.Ready) { func ready(e *events.Ready) {
logrus.Info("Starting up...") logrus.Info("Starting up...")
@@ -43,9 +43,9 @@ func ready(e *events.Ready) {
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") { 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(app.Bot.ID, command.Definition) cmd, err := client.Rest().CreateGlobalCommand(app.Bot.ID, command.Definition)
if err != nil { if err != nil {
logrus.Errorf("error creating global command '%s': %v", cmd.Name, err) logrus.Errorf("error creating global command '%s': %v", cmd.Name(), err)
} else { } else {
logrus.Infof("Added global command '%s'", cmd.Name) logrus.Infof("Added global command '%s'", cmd.Name())
} }
} }
} }
@@ -139,7 +139,7 @@ func modalSubmitInteractionCreate(e *events.ModalSubmitInteractionCreate) {
func removeOldCommandFromAllGuilds() { func removeOldCommandFromAllGuilds() {
globalCommands, err := client.Rest().GetGlobalCommands(app.Bot.ID, false) globalCommands, err := client.Rest().GetGlobalCommands(app.Bot.ID, false)
if err != nil { if err != nil {
logrus.Error("error fetching existing global commands: %v", err) logrus.Errorf("error fetching existing global commands: %v", err)
return return
} }
var commandNames []string var commandNames []string
@@ -149,10 +149,10 @@ func removeOldCommandFromAllGuilds() {
for _, existingCommand := range globalCommands { for _, existingCommand := range globalCommands {
if slices.Contains(commandNames, existingCommand.Name()) { if slices.Contains(commandNames, existingCommand.Name()) {
logrus.Info("Deleting command '%s'", existingCommand.Name) logrus.Infof("Deleting command '%s'", existingCommand.Name())
err := client.Rest().DeleteGlobalCommand(app.Bot.ID, existingCommand.ID()) err := client.Rest().DeleteGlobalCommand(app.Bot.ID, existingCommand.ID())
if err != nil { if err != nil {
logrus.Error("error deleting command %s: %v", existingCommand.Name, err) logrus.Errorf("error deleting command %s: %v", existingCommand.Name(), err)
} }
} }
} }

View File

@@ -11,6 +11,8 @@ import (
"syscall" "syscall"
"time" "time"
_ "github.com/lib/pq"
"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/discord"
@@ -36,7 +38,7 @@ func main() {
logrus.Fatal(err) logrus.Fatal(err)
} }
initTables() initTables()
client, err := disgo.New("Bot "+os.Getenv("BOT_TOKEN"), client, err := disgo.New(os.Getenv("BOT_TOKEN"),
bot.WithGatewayConfigOpts( bot.WithGatewayConfigOpts(
gateway.WithIntents( gateway.WithIntents(
gateway.IntentGuilds, gateway.IntentGuilds,
@@ -47,6 +49,8 @@ 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(modalSubmitInteractionCreate),
bot.WithEventListenerFunc(messageCreate), bot.WithEventListenerFunc(messageCreate),
bot.WithEventListenerFunc(messageDelete), bot.WithEventListenerFunc(messageDelete),
bot.WithEventListenerFunc(guildMemberJoin), bot.WithEventListenerFunc(guildMemberJoin),