added partial userinstall support

This commit is contained in:
2024-04-10 18:15:08 +02:00
parent 99617e5e1f
commit bb9c616844
11 changed files with 120 additions and 70 deletions

View File

@@ -10,6 +10,15 @@ var cmd_ask = Command{
Definition: discord.SlashCommandCreate{
Name: "ask",
Description: "Ask anything and get a gif as response!",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel,
discord.InteractionContextTypeBotDM,
},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall,
discord.ApplicationIntegrationTypeUserInstall,
},
Options: []discord.ApplicationCommandOption{
&discord.ApplicationCommandOptionString{
Name: "question",
@@ -26,5 +35,4 @@ var cmd_ask = Command{
logrus.Error(err)
}
},
AllowDM: true,
}

View File

@@ -10,6 +10,11 @@ var cmd_autojoinroles Command = Command{
Definition: discord.SlashCommandCreate{
Name: "autojoinroles",
Description: "Give users a role when they join",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall},
Options: []discord.ApplicationCommandOption{
&discord.ApplicationCommandOptionSubCommand{
Name: "bot",

View File

@@ -10,6 +10,11 @@ var cmd_autopublish Command = Command{
Definition: discord.SlashCommandCreate{
Name: "autopublish",
Description: "Toggle automatically publishing every post in a announcement channel",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
channel := e.Channel()

View File

@@ -14,6 +14,15 @@ var cmd_cat = Command{
Definition: discord.SlashCommandCreate{
Name: "cat",
Description: "Random cat pictures",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel,
discord.InteractionContextTypeBotDM,
},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall,
discord.ApplicationIntegrationTypeUserInstall,
},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
cat, err := GetCatImageURL()
@@ -26,7 +35,6 @@ var cmd_cat = Command{
}
}
},
AllowDM: true,
}
type CatImage struct {

View File

@@ -10,6 +10,15 @@ var cmd_dadjoke = Command{
Definition: discord.SlashCommandCreate{
Name: "dadjoke",
Description: "Gives you a random joke that is as bad as your dad would tell them",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel,
discord.InteractionContextTypeBotDM,
},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall,
discord.ApplicationIntegrationTypeUserInstall,
},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
joke := simpleGetFromAPI("joke", "https://icanhazdadjoke.com/").(string)
@@ -20,5 +29,4 @@ var cmd_dadjoke = Command{
logrus.Error(err)
}
},
AllowDM: true,
}

View File

@@ -19,6 +19,11 @@ var cmd_form Command = Command{
Name: "form",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels),
Description: "Create custom forms right inside Discord",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall},
Options: []discord.ApplicationCommandOption{
&discord.ApplicationCommandOptionSubCommand{
Name: "help",
@@ -250,7 +255,6 @@ var cmd_form Command = Command{
Build())
}
} else {
logrus.Debug(result.AcceptChannelID)
if result.AcceptChannelID == "" {
_, err := e.Client().Rest().CreateMessage(snowflake.MustParse(result.ResultChannelID), discord.NewMessageCreateBuilder().
SetEmbeds(discord.NewEmbedBuilder().
@@ -270,7 +274,6 @@ var cmd_form Command = Command{
}
}
} else {
logrus.Debug("HEERE")
var buttons []discord.InteractiveComponent
if result.CommentCategoryID != "" {
buttons = []discord.InteractiveComponent{discord.
@@ -335,6 +338,9 @@ var cmd_ticket_form Command = Command{
Name: "ticket",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels),
Description: "A quick command to create Ticketpanels. (/form for more)",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
Options: []discord.ApplicationCommandOption{
&discord.ApplicationCommandOptionString{
Name: "title",

View File

@@ -42,6 +42,15 @@ var cmd_userinfo Command = Command{
Definition: discord.SlashCommandCreate{
Name: "info",
Description: "Gives you information about a user or this bot.",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel,
discord.InteractionContextTypeBotDM,
},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall,
discord.ApplicationIntegrationTypeUserInstall,
},
Options: []discord.ApplicationCommandOption{
&discord.ApplicationCommandOptionSubCommand{
Name: "user",
@@ -92,7 +101,7 @@ var cmd_userinfo Command = Command{
embedBuilder.AddField("Discriminator", user.Discriminator, false)
}
if user.AccentColor != nil {
embedBuilder.AddField("Accent color", strconv.Itoa(*user.AccentColor), true)
embedBuilder.AddField("Accent color", "#"+strconv.FormatInt(int64(*user.AccentColor), 16), true)
}
if user.AvatarDecorationURL() != nil {
value := fmt.Sprintf("[PNG (animated)](%s)\n[PNG](%s)", *user.AvatarDecorationURL(), *user.AvatarDecorationURL()+"?passthrough=false")
@@ -119,7 +128,6 @@ var cmd_userinfo Command = Command{
}
},
AllowDM: true,
}
func checkDefaultPb(user discord.User) string {

View File

@@ -14,6 +14,15 @@ var cmd_ping Command = Command{
Definition: discord.SlashCommandCreate{
Name: "ping",
Description: "Returns the ping of the bot",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel,
discord.InteractionContextTypeBotDM,
},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall,
discord.ApplicationIntegrationTypeUserInstall,
},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
start := time.Now()
@@ -51,5 +60,4 @@ var cmd_ping Command = Command{
logrus.Error(err)
}
},
AllowDM: true,
}

View File

@@ -13,6 +13,11 @@ var cmd_sticky Command = Command{
Name: "sticky",
Description: "Stick or unstick messages to the bottom of the current channel",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageMessages),
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall},
Options: []discord.ApplicationCommandOption{
&discord.ApplicationCommandOptionString{
Name: "message",
@@ -53,6 +58,11 @@ var context_sticky Command = Command{
Definition: discord.MessageCommandCreate{
Name: "Stick to channel",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageMessages),
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
inputStickyMessage(e)

View File

@@ -7,11 +7,17 @@ import (
"github.com/sirupsen/logrus"
)
// TODO: make user installable tag command using userIDs instead of guildIDs
var cmd_tag Command = Command{
Definition: discord.SlashCommandCreate{
Name: "tag",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild),
Description: "A command to show and edit saved presaved messages.",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall},
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionSubCommand{
Name: "get",
@@ -80,6 +86,11 @@ var cmd_tag_short Command = Command{
Definition: discord.SlashCommandCreate{
Name: "g",
Description: "A short command to get presaved messages.",
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall},
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionString{
Name: "tag",
@@ -101,6 +112,11 @@ var context_tag Command = Command{
Definition: discord.MessageCommandCreate{
Name: "Save as tag",
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild),
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypePrivateChannel},
IntegrationTypes: []discord.ApplicationIntegrationType{
discord.ApplicationIntegrationTypeGuildInstall},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
AddTagCommand(e)

View File

@@ -23,7 +23,6 @@ type Command struct {
ModalIDs []string
DynamicModalIDs func() []string
DynamicComponentIDs func() []string
AllowDM bool
}
var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag, cmd_sticky, context_sticky, cmd_ping, cmd_userinfo, cmd_form, cmd_ask, cmd_cat, cmd_dadjoke, cmd_ticket_form, cmd_autopublish, cmd_autojoinroles}
@@ -63,56 +62,33 @@ func ready(e *events.Ready) {
func applicationCommandInteractionCreate(e *events.ApplicationCommandInteractionCreate) {
for _, command := range commands {
if command.Interact != nil && e.Data.CommandName() == command.Definition.CommandName() {
if !command.AllowDM && e.ApplicationCommandInteraction.GuildID().String() == "" {
err := e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("This command is not available in DMs.").SetEphemeral(true).
Build())
if err != nil {
logrus.Error(err)
}
} else {
command.Interact(e)
}
command.Interact(e)
}
}
}
func autocompleteInteractionCreate(e *events.AutocompleteInteractionCreate) {
for _, command := range commands {
if command.Autocomplete != nil && e.Data.CommandName == command.Definition.CommandName() {
if !command.AllowDM && e.AutocompleteInteraction.GuildID().String() == "" {
err := e.AutocompleteResult(nil)
if err != nil {
logrus.Error(err)
}
} else {
command.Autocomplete(e)
}
command.Autocomplete(e)
}
}
}
func componentInteractionCreate(e *events.ComponentInteractionCreate) {
for _, command := range commands {
if !command.AllowDM && e.ComponentInteraction.GuildID().String() == "" {
e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("This component is not available in DMs.").SetEphemeral(true).
Build())
} else {
if command.ComponentInteract != nil {
if slices.Contains(command.ComponentIDs, e.Data.CustomID()) || slices.ContainsFunc(command.DynamicComponentIDs(), func(id string) bool {
var customID string
if strings.ContainsAny(e.Data.CustomID(), ";") {
customID = strings.TrimSuffix(e.Data.CustomID(), ";"+strings.Split(e.Data.CustomID(), ";")[1])
} else {
customID = e.Data.CustomID()
}
return id == customID
}) {
command.ComponentInteract(e)
if command.ComponentInteract != nil {
if slices.Contains(command.ComponentIDs, e.Data.CustomID()) || slices.ContainsFunc(command.DynamicComponentIDs(), func(id string) bool {
var customID string
if strings.ContainsAny(e.Data.CustomID(), ";") {
customID = strings.TrimSuffix(e.Data.CustomID(), ";"+strings.Split(e.Data.CustomID(), ";")[1])
} else {
customID = e.Data.CustomID()
}
return id == customID
}) {
command.ComponentInteract(e)
}
}
}
@@ -120,31 +96,25 @@ func componentInteractionCreate(e *events.ComponentInteractionCreate) {
func modalSubmitInteractionCreate(e *events.ModalSubmitInteractionCreate) {
for _, command := range commands {
if !command.AllowDM && e.ModalSubmitInteraction.GuildID().String() == "" {
e.CreateMessage(discord.NewMessageCreateBuilder().
SetContent("This modal is not available in DMs.").SetEphemeral(true).
Build())
} else {
if command.ModalSubmit != nil {
var hasID bool = false
var modalIDs []string
if command.ModalIDs != nil {
modalIDs = command.ModalIDs
}
if command.DynamicModalIDs != nil {
modalIDs = append(command.ModalIDs, command.DynamicModalIDs()...)
}
for _, modalID := range modalIDs {
if strings.HasPrefix(e.Data.CustomID, modalID) {
hasID = true
break
}
}
if hasID {
command.ModalSubmit(e)
return // I have no idea why it crashes without that return
if command.ModalSubmit != nil {
var hasID bool = false
var modalIDs []string
if command.ModalIDs != nil {
modalIDs = command.ModalIDs
}
if command.DynamicModalIDs != nil {
modalIDs = append(command.ModalIDs, command.DynamicModalIDs()...)
}
for _, modalID := range modalIDs {
if strings.HasPrefix(e.Data.CustomID, modalID) {
hasID = true
break
}
}
if hasID {
command.ModalSubmit(e)
return // I have no idea why it crashes without that return
}
}
}
}
@@ -201,7 +171,6 @@ func messageCreate(e *events.MessageCreate) {
logrus.Error(err)
}
if channel != nil && channel.Type() == discord.ChannelTypeGuildNews {
logrus.Debug("HERE")
if isAutopublishEnabled(e.GuildID.String(), e.ChannelID.String()) {
_, err := e.Client().Rest().CrosspostMessage(e.ChannelID, e.MessageID)
if err != nil {
@@ -217,7 +186,6 @@ func messageDelete(e *events.MessageDelete) { //TODO: also clear on bot start wh
}
func guildMemberJoin(e *events.GuildMemberJoin) {
logrus.Debug("TESSST")
role := getAutoJoinRole(e.GuildID.String(), e.Member.User.Bot)
if role != "" {
err := e.Client().Rest().AddMemberRole(e.GuildID, e.Member.User.ID, snowflake.MustParse(role))