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
/*
import (
"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
@@ -31,3 +32,4 @@ var cmd_ask Command = Command{
},
AllowDM: true,
}
*/

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -24,7 +24,7 @@ type Command struct {
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) {
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") {
cmd, err := client.Rest().CreateGlobalCommand(app.Bot.ID, command.Definition)
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 {
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() {
globalCommands, err := client.Rest().GetGlobalCommands(app.Bot.ID, false)
if err != nil {
logrus.Error("error fetching existing global commands: %v", err)
logrus.Errorf("error fetching existing global commands: %v", err)
return
}
var commandNames []string
@@ -149,10 +149,10 @@ func removeOldCommandFromAllGuilds() {
for _, existingCommand := range globalCommands {
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())
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"
"time"
_ "github.com/lib/pq"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
@@ -36,7 +38,7 @@ func main() {
logrus.Fatal(err)
}
initTables()
client, err := disgo.New("Bot "+os.Getenv("BOT_TOKEN"),
client, err := disgo.New(os.Getenv("BOT_TOKEN"),
bot.WithGatewayConfigOpts(
gateway.WithIntents(
gateway.IntentGuilds,
@@ -47,6 +49,8 @@ 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),