1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-18 04:42:54 +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
"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-01-22 07:24:15 +00:00
type ChannelMention discord.Snowflake
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-01-22 07:26:46 +00:00
func (m *ChannelMention) ID() discord.Snowflake {
return discord.Snowflake(*m)
}
2020-01-24 04:54:46 +00:00
func (m *ChannelMention) Mention() string {
return "<#" + m.ID().String() + ">"
}
//
2020-01-22 07:24:15 +00:00
type UserMention discord.Snowflake
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-01-22 07:26:46 +00:00
func (m *UserMention) ID() discord.Snowflake {
return discord.Snowflake(*m)
}
2020-01-24 04:54:46 +00:00
func (m *UserMention) Mention() string {
return "<@" + m.ID().String() + ">"
}
//
2020-01-22 07:24:15 +00:00
type RoleMention discord.Snowflake
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-01-22 07:26:46 +00:00
func (m *RoleMention) ID() discord.Snowflake {
return discord.Snowflake(*m)
}
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 {
return errors.New("Invalid " + item)
}
2020-01-22 07:24:15 +00:00
id, err := discord.ParseSnowflake(matches[1])
if err != nil {
return errors.New("Invalid " + item)
}
*output = id
2020-01-19 06:06:00 +00:00
return nil
}