2020-01-19 06:06:00 +00:00
|
|
|
package arguments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
2020-01-22 07:24:15 +00:00
|
|
|
|
|
|
|
"github.com/diamondburned/arikawa/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
|
|
|
//
|
|
|
|
|
2020-07-21 20:27:59 +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"
|
|
|
|
}
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
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() + ">"
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
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() + ">"
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2020-07-29 20:58:39 +00:00
|
|
|
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
|
|
|
|
}
|