From 0fd339dad3ad338bf5ef82b44ff9ac5cd3f11102 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 5 Mar 2024 21:31:44 +0100 Subject: [PATCH] added more tool functions --- cmd_ask.go | 20 ++++++-------------- cmd_autojoinroles.go | 2 +- cmd_autopublish.go | 6 +++--- cmd_cat.go | 20 ++++++-------------- cmd_dadjoke.go | 7 +------ cmd_form.go | 8 ++++---- cmd_ping.go | 19 +++++-------------- cmd_sticky.go | 8 ++++---- cmd_tag.go | 11 +++-------- tool.go | 19 +++++++++++++++++-- 10 files changed, 50 insertions(+), 70 deletions(-) diff --git a/cmd_ask.go b/cmd_ask.go index 7d6e218..ff4f4f0 100644 --- a/cmd_ask.go +++ b/cmd_ask.go @@ -16,19 +16,11 @@ var cmd_ask Command = Command{ }, }, Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{ - { - Type: discordgo.EmbedTypeImage, - Color: hexToDecimal(color["primary"]), - Image: &discordgo.MessageEmbedImage{ - URL: simpleGetFromAPI("image", "https://yesno.wtf/api").(string), - }, - }, - }, - }, - }) + respondEmbed(s, i.Interaction, discordgo.MessageEmbed{ + Type: discordgo.EmbedTypeImage, + Color: hexToDecimal(color["primary"]), + Image: &discordgo.MessageEmbedImage{ + URL: simpleGetFromAPI("image", "https://yesno.wtf/api").(string), + }}, false) }, } diff --git a/cmd_autojoinroles.go b/cmd_autojoinroles.go index 191ea79..453c87d 100644 --- a/cmd_autojoinroles.go +++ b/cmd_autojoinroles.go @@ -53,7 +53,7 @@ var cmd_autojoinroles Command = Command{ } else if setAutoJoinRole(i.GuildID, option, role) { content = "Deleted auto join role for " + option + "s" } - respondEphemeral(s, i.Interaction, content) + respond(s, i.Interaction, content, true) purgeUnusedAutoJoinRoles(i.GuildID) }, } diff --git a/cmd_autopublish.go b/cmd_autopublish.go index c295610..653e846 100644 --- a/cmd_autopublish.go +++ b/cmd_autopublish.go @@ -11,12 +11,12 @@ var cmd_autopublish Command = Command{ channel, _ := s.State.Channel(i.ChannelID) if channel.Type == discordgo.ChannelTypeGuildNews { if toggleAutoPublish(i.GuildID, i.ChannelID) { - respondEphemeral(s, i.Interaction, "Autopublishing is now disabled on <#"+i.ChannelID+">") + respond(s, i.Interaction, "Autopublishing is now disabled on <#"+i.ChannelID+">", true) } else { - respondEphemeral(s, i.Interaction, "Autopublishing is now enabled on <#"+i.ChannelID+">") + respond(s, i.Interaction, "Autopublishing is now enabled on <#"+i.ChannelID+">", true) } } else { - respondEphemeral(s, i.Interaction, "This is not an announcement channel!") + respond(s, i.Interaction, "This is not an announcement channel!", true) } }, } diff --git a/cmd_cat.go b/cmd_cat.go index 2d26996..d0294be 100644 --- a/cmd_cat.go +++ b/cmd_cat.go @@ -15,20 +15,12 @@ var cmd_cat Command = Command{ Description: "Random cat pictures", }, Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{ - { - Type: discordgo.EmbedTypeImage, - Color: hexToDecimal(color["primary"]), - Image: &discordgo.MessageEmbedImage{ - URL: GetCatImageURL(), - }, - }, - }, - }, - }) + respondEmbed(s, i.Interaction, discordgo.MessageEmbed{ + Type: discordgo.EmbedTypeImage, + Color: hexToDecimal(color["primary"]), + Image: &discordgo.MessageEmbedImage{ + URL: GetCatImageURL(), + }}, false) }, } diff --git a/cmd_dadjoke.go b/cmd_dadjoke.go index b3f15e3..075e126 100644 --- a/cmd_dadjoke.go +++ b/cmd_dadjoke.go @@ -10,11 +10,6 @@ var cmd_dadjoke Command = Command{ Description: "Gives you a random joke that is as bad as your dad would tell them", }, Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: simpleGetFromAPI("joke", "https://icanhazdadjoke.com/").(string), - }, - }) + respond(s, i.Interaction, simpleGetFromAPI("joke", "https://icanhazdadjoke.com/").(string), false) }, } diff --git a/cmd_form.go b/cmd_form.go index 6912929..72a590f 100644 --- a/cmd_form.go +++ b/cmd_form.go @@ -118,7 +118,7 @@ var cmd_form Command = Command{ }, }) case "custom": - respondEphemeral(s, i.Interaction, "Feature not available yet use `/form add` instead") + respond(s, i.Interaction, "Feature not available yet use `/form add` instead", true) case "add": var title, formID, overwriteTitle, acceptChannelID string var modsCanComment bool @@ -180,12 +180,12 @@ var cmd_form Command = Command{ }, }) addFormButton(i.GuildID, i.ChannelID, message.ID, formManageID.String(), formID, options.Options[0].ChannelValue(s).ID, overwriteTitle, acceptChannelID, modsCanComment) - respondEphemeral(s, i.Interaction, "Successfully added form button!") + respond(s, i.Interaction, "Successfully added form button!", true) } }, ComponentInteract: func(s *discordgo.Session, i *discordgo.InteractionCreate) { if strings.HasPrefix(i.Interaction.MessageComponentData().CustomID, "form:") { - respondEphemeral(s, i.Interaction, getFormType(strings.TrimPrefix(i.Interaction.MessageComponentData().CustomID, "form:"))) + respond(s, i.Interaction, getFormType(strings.TrimPrefix(i.Interaction.MessageComponentData().CustomID, "form:")), true) } if i.Interaction.MessageComponentData().CustomID == "form_demo" { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ @@ -229,7 +229,7 @@ var cmd_form Command = Command{ }, ModalIDs: getFormTypes(), ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - respondEphemeral(s, i.Interaction, "The form data would be send to a specified channel. 🤲") + respond(s, i.Interaction, "The form data would be send to a specified channel. 🤲", true) }, Autocomplete: func(s *discordgo.Session, i *discordgo.InteractionCreate) { choices := []*discordgo.ApplicationCommandOptionChoice{ diff --git a/cmd_ping.go b/cmd_ping.go index 4c9d142..69dedb5 100644 --- a/cmd_ping.go +++ b/cmd_ping.go @@ -36,19 +36,10 @@ var cmd_ping Command = Command{ } else { ping_color = "red" } - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Flags: discordgo.MessageFlagsEphemeral, - Embeds: []*discordgo.MessageEmbed{ - { - Title: s.State.User.Username + " ping", - Description: fmt.Sprintf("# %.2fms", ping.Seconds()*1000), - Type: discordgo.EmbedTypeArticle, - Color: hexToDecimal(color[ping_color]), - }, - }, - }, - }) + respondEmbed(s, i.Interaction, discordgo.MessageEmbed{ + Title: s.State.User.Username + " ping", + Description: fmt.Sprintf("# %.2fms", ping.Seconds()*1000), + Type: discordgo.EmbedTypeArticle, + Color: hexToDecimal(color[ping_color])}, true) }, } diff --git a/cmd_sticky.go b/cmd_sticky.go index ac60c90..1fbf932 100644 --- a/cmd_sticky.go +++ b/cmd_sticky.go @@ -53,9 +53,9 @@ var cmd_sticky Command = Command{ if hasSticky(i.GuildID, i.ChannelID) { s.ChannelMessageDelete(i.ChannelID, getStickyMessageID(i.GuildID, i.ChannelID)) removeSticky(i.GuildID, i.ChannelID) - respondEphemeral(s, i.Interaction, "The sticky message was removed from this channel!") + respond(s, i.Interaction, "The sticky message was removed from this channel!", true) } else { - respondEphemeral(s, i.Interaction, "This channel has no sticky message!") + respond(s, i.Interaction, "This channel has no sticky message!", true) } } }, @@ -74,9 +74,9 @@ var cmd_sticky Command = Command{ log.Println(err) } if addSticky(i.GuildID, i.ChannelID, text, message.ID) { - respondEphemeral(s, i.Interaction, "Sticky message in this channel was updated!") + respond(s, i.Interaction, "Sticky message in this channel was updated!", true) } else { - respondEphemeral(s, i.Interaction, "Message sticked to the channel!") + respond(s, i.Interaction, "Message sticked to the channel!", true) } }, } diff --git a/cmd_tag.go b/cmd_tag.go index b126b22..7d0b078 100644 --- a/cmd_tag.go +++ b/cmd_tag.go @@ -88,7 +88,7 @@ var cmd_tag Command = Command{ }) case "remove": removeTag(i.GuildID, i.ApplicationCommandData().Options[0].Options[0].StringValue()) - respondEphemeral(s, i.Interaction, "Tag removed!") + respond(s, i.Interaction, "Tag removed!", true) } }, ModalIDs: []string{"tag_add_modal"}, @@ -96,7 +96,7 @@ var cmd_tag Command = Command{ tagName := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value tagContent := i.ModalSubmitData().Components[1].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value addTag(i.GuildID, tagName, tagContent) - respondEphemeral(s, i.Interaction, "Tag \""+tagName+"\" added!") + respond(s, i.Interaction, "Tag \""+tagName+"\" added!", true) }, Autocomplete: func(s *discordgo.Session, i *discordgo.InteractionCreate) { AutocompleteTag(s, i) @@ -126,12 +126,7 @@ var cmd_tag_short Command = Command{ } func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: getTagContent(i.GuildID, option.Value.(string)), - }, - }) + respond(s, i.Interaction, getTagContent(i.GuildID, option.Value.(string)), false) } func AutocompleteTag(s *discordgo.Session, i *discordgo.InteractionCreate) { diff --git a/tool.go b/tool.go index 20ea301..7452fca 100644 --- a/tool.go +++ b/tool.go @@ -66,12 +66,27 @@ func hexToDecimal(hexColor string) int { return int(decimal) } -func respondEphemeral(s *discordgo.Session, interaction *discordgo.Interaction, content string) { +func respond(s *discordgo.Session, interaction *discordgo.Interaction, content string, ephemeral bool) { + var flag discordgo.MessageFlags + if ephemeral { + flag = discordgo.MessageFlagsEphemeral + } s.InteractionRespond(interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: content, - Flags: discordgo.MessageFlagsEphemeral, + Flags: flag, + }, + }) +} + +func respondEmbed(s *discordgo.Session, interaction *discordgo.Interaction, embed discordgo.MessageEmbed, ephemeral bool) { + s.InteractionRespond(interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + &embed, + }, }, }) }