Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for slash command mentions #194

Merged
merged 4 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions discord/application_command.go
Expand Up @@ -180,6 +180,10 @@ func (c SlashCommand) CreatedAt() time.Time {
return c.id.Time()
}

func (c SlashCommand) Mention() string {
return SlashCommandMention(c.id, c.name)
}

func (SlashCommand) applicationCommand() {}

var _ ApplicationCommand = (*UserCommand)(nil)
Expand Down
23 changes: 16 additions & 7 deletions discord/mentionable.go
Expand Up @@ -12,13 +12,14 @@ type MentionType struct {
}

var (
MentionTypeUser = MentionType{regexp.MustCompile(`<@!?(\d+)>`)}
MentionTypeRole = MentionType{regexp.MustCompile(`<@&(\d+)>`)}
MentionTypeChannel = MentionType{regexp.MustCompile(`<#(\d+)>`)}
MentionTypeEmoji = MentionType{regexp.MustCompile(`<a?:(\w+):(\d+)>`)}
MentionTypeTimestamp = MentionType{regexp.MustCompile(`<t:(?P<time>-?\d{1,17})(?::(?P<format>[tTdDfFR]))?>`)}
MentionTypeHere = MentionType{regexp.MustCompile(`@here`)}
MentionTypeEveryone = MentionType{regexp.MustCompile(`@everyone`)}
MentionTypeUser = MentionType{regexp.MustCompile(`<@!?(\d+)>`)}
MentionTypeRole = MentionType{regexp.MustCompile(`<@&(\d+)>`)}
MentionTypeChannel = MentionType{regexp.MustCompile(`<#(\d+)>`)}
MentionTypeEmoji = MentionType{regexp.MustCompile(`<a?:(\w+):(\d+)>`)}
MentionTypeTimestamp = MentionType{regexp.MustCompile(`<t:(?P<time>-?\d{1,17})(?::(?P<format>[tTdDfFR]))?>`)}
MentionTypeSlashCommand = MentionType{regexp.MustCompile(`</(\w+) ?((\w+)|(\w+ \w+)):(\d+)>`)}
MentionTypeHere = MentionType{regexp.MustCompile(`@here`)}
MentionTypeEveryone = MentionType{regexp.MustCompile(`@everyone`)}
)

type Mentionable interface {
Expand Down Expand Up @@ -57,3 +58,11 @@ func TimestampMention(timestamp int64) string {
func FormattedTimestampMention(timestamp int64, style TimestampStyle) string {
return fmt.Sprintf("<t:%d:%s>", timestamp, style)
}

// SlashCommandMention creates a slash command mention.
// You can also pass a subcommand (and/or a subcommand group respectively) to the path.
//
// mention := SlashCommandMention(id, "command group subcommand")
func SlashCommandMention(id snowflake.ID, path string) string {
return fmt.Sprintf("</%s:%d>", path, id)
}