1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-12 08:25:10 +00:00
arikawa/bot/extras/arguments/mention.go

92 lines
1.7 KiB
Go
Raw Normal View History

2020-01-19 06:06:00 +00:00
package arguments
import (
"errors"
"regexp"
2020-01-22 07:24:15 +00:00
2021-06-02 02:53:19 +00:00
"github.com/diamondburned/arikawa/v3/discord"
2020-01-19 06:06:00 +00:00
)
var (
ChannelRegex = regexp.MustCompile(`<#(\d+)>`)
UserRegex = regexp.MustCompile(`<@!?(\d+)>`)
RoleRegex = regexp.MustCompile(`<@&(\d+)>`)
)
2020-01-24 04:54:46 +00:00
//
type ChannelMention discord.ChannelID
2020-01-19 06:06:00 +00:00
func (m *ChannelMention) Parse(arg string) error {
2020-04-09 04:25:50 +00:00
return grabFirst(ChannelRegex, "channel mention", arg, (*discord.Snowflake)(m))
2020-01-19 06:06:00 +00:00
}
func (m *ChannelMention) Usage() string {
return "#channel"
}
func (m *ChannelMention) ID() discord.ChannelID {
return discord.ChannelID(*m)
2020-01-22 07:26:46 +00:00
}
2020-01-24 04:54:46 +00:00
func (m *ChannelMention) Mention() string {
return "<#" + m.ID().String() + ">"
}
//
type UserMention discord.UserID
2020-01-19 06:06:00 +00:00
func (m *UserMention) Parse(arg string) error {
2020-04-09 04:25:50 +00:00
return grabFirst(UserRegex, "user mention", arg, (*discord.Snowflake)(m))
2020-01-19 06:06:00 +00:00
}
func (m *UserMention) Usage() string {
return "@user"
}
func (m *UserMention) ID() discord.UserID {
return discord.UserID(*m)
2020-01-22 07:26:46 +00:00
}
2020-01-24 04:54:46 +00:00
func (m *UserMention) Mention() string {
return "<@" + m.ID().String() + ">"
}
//
type RoleMention discord.RoleID
2020-01-19 06:06:00 +00:00
func (m *RoleMention) Parse(arg string) error {
2020-04-09 04:25:50 +00:00
return grabFirst(RoleRegex, "role mention", arg, (*discord.Snowflake)(m))
2020-01-19 06:06:00 +00:00
}
func (m *RoleMention) Usage() string {
return "@role"
}
func (m *RoleMention) ID() discord.RoleID {
return discord.RoleID(*m)
2020-01-22 07:26:46 +00:00
}
2020-01-24 04:54:46 +00:00
func (m *RoleMention) Mention() string {
2020-02-04 16:13:11 +00:00
return "<@&" + m.ID().String() + ">"
2020-01-24 04:54:46 +00:00
}
//
2020-04-09 04:25:50 +00:00
func grabFirst(reg *regexp.Regexp, item, input string, output *discord.Snowflake) error {
2020-01-19 06:06:00 +00:00
matches := reg.FindStringSubmatch(input)
if len(matches) < 2 {
2020-05-16 21:14:49 +00:00
return errors.New("invalid " + item)
2020-01-19 06:06:00 +00:00
}
2020-01-22 07:24:15 +00:00
id, err := discord.ParseSnowflake(matches[1])
if err != nil {
2020-05-16 21:14:49 +00:00
return errors.New("invalid " + item)
2020-01-22 07:24:15 +00:00
}
*output = id
2020-01-19 06:06:00 +00:00
return nil
}