--- a/discord/commands.go Sun Apr 07 02:05:16 2019 -0500
+++ b/discord/commands.go Sun Apr 07 06:03:13 2019 -0500
@@ -4,23 +4,15 @@
+ "github.com/alecthomas/kong" "github.com/bwmarrin/discordgo"
- "github.com/dustin/go-humanize"
log "github.com/sirupsen/logrus"
- "bitbucket.org/TheToyz/nowdead/presence"
-type commandHandler func(c *DiscordClient, m *discordgo.MessageCreate) error
-var commands map[string]commandHandler
- commands = map[string]commandHandler{
- "!uptime": uptimeCommand,
- "!setchannel": setChannelCommand,
- "!islive": isLiveCommand,
+ UptimeCommand uptimeCommand `kong:"cmd,name='uptime'"` + StatusCommand statusCommand `kong:"cmd,name='status'"` + SetChannelCommand setChannelCommand `kong:"cmd,name='setchannel'"` func (c *DiscordClient) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
@@ -33,66 +25,30 @@
- log.Debugf("message contents: '%s'", m.Content)
- log.Debugf("our user id: %s", c.userID)
- parts := strings.Split(m.Content, " ")
- command := strings.ToLower(parts[1])
+ parser, err := kong.New( + kong.Exit(func(int) {}), + c.sendChannel(m.ChannelID, "failed to parse command") + log.Warnf("error creating parser: %v", err) - if handler, found := commands[command]; found {
- log.Warnf("failed to send response: %v", err)
- c.sendChannel(m.ChannelID, fmt.Sprintf("error: %#v", err))
+ args := strings.Split(m.Content, " ")[1:] + ctx, err := parser.Parse(args) + c.sendChannel(m.ChannelID, "failed to parse command") + log.Warnf("error parsing command: %#v : %v", args, err) + c.sendChannel(m.ChannelID, fmt.Sprintf("%v", err)) + log.Debugf("finished running command: '%v'", err)
-func uptimeCommand(c *DiscordClient, m *discordgo.MessageCreate) error {
- fmt.Sprintf("Started %s", humanize.Time(c.started)),
-func setChannelCommand(c *DiscordClient, m *discordgo.MessageCreate) error {
- c.db.SetChannel(m.GuildID, m.ChannelID)
- fmt.Sprintf("Set <#%s> as the announcement channel", m.ChannelID),
-func isLiveCommand(c *DiscordClient, m *discordgo.MessageCreate) error {
- args := strings.Split(m.Content, " ")
- return fmt.Errorf("invalid arguments")
- if len(m.Mentions) > 1 {
- member := m.Mentions[0]
- log.Debugf("getting presence on %s for %s", m.GuildID, member.ID)
- p := c.getPresence(m.GuildID, member.ID)
- log.Debugf("presences: %#v", p)
- return fmt.Errorf("no presence data found")
- if p.Game.Type == discordgo.GameTypeStreaming {
- uri = "https://twitch.tv/" + user
- presence, err := presence.GetPresence(uri)
- return c.sendPresence(m.GuildID, presence)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/discord/set_channel_command.go Sun Apr 07 06:03:13 2019 -0500
@@ -0,0 +1,20 @@
+ "github.com/bwmarrin/discordgo" +type setChannelCommand struct { + //Channel string `kong:"required,name='channel'"` +func (cmd *setChannelCommand) Run(c *DiscordClient, m *discordgo.MessageCreate) error { + c.db.SetChannel(m.GuildID, m.ChannelID) + fmt.Sprintf("Set <#%s> as the announcement channel", m.ChannelID), --- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/discord/status_command.go Sun Apr 07 06:03:13 2019 -0500
@@ -0,0 +1,45 @@
+ "github.com/bwmarrin/discordgo" + log "github.com/sirupsen/logrus" + "bitbucket.org/TheToyz/nowdead/presence" +type statusCommand struct { + Target string `kong:"arg,required,name='target'"` +func (cmd *statusCommand) Run(c *DiscordClient, m *discordgo.MessageCreate) error { + log.Debug("running status command") + if strings.HasPrefix(cmd.Target, "<@") { + mentionID := cmd.Target[2 : len(cmd.Target)-1] + for _, mentioned := range m.Mentions { + if mentioned.ID == mentionID { + p := c.getPresence(m.GuildID, mentioned.ID) + if p != nil && p.Game != nil && p.Game.Type == discordgo.GameTypeStreaming { + return fmt.Errorf("%s is not streaming", cmd.Target) + uri = "https://twitch.tv/" + cmd.Target + presence, err := presence.GetPresence(uri) + return c.sendPresenceChannel(m.ChannelID, presence) --- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/discord/uptime_command.go Sun Apr 07 06:03:13 2019 -0500
@@ -0,0 +1,17 @@
+ "github.com/bwmarrin/discordgo" + "github.com/dustin/go-humanize" +type uptimeCommand struct{} +func (cmd *uptimeCommand) Run(c *DiscordClient, m *discordgo.MessageCreate) error { + fmt.Sprintf("Started %s", humanize.Time(c.started)), --- a/go.mod Sun Apr 07 02:05:16 2019 -0500
+++ b/go.mod Sun Apr 07 06:03:13 2019 -0500
@@ -4,14 +4,17 @@
github.com/alecthomas/kingpin v2.2.6+incompatible
+ github.com/alecthomas/kong v0.1.16 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/bwmarrin/discordgo v0.19.0
github.com/dustin/go-humanize v1.0.0
github.com/go-redis/redis v6.15.2+incompatible
github.com/nicklaw5/helix v0.5.1
+ github.com/nicksnyder/go-i18n v1.10.0 // indirect github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
github.com/prologic/bitcask v0.1.6
github.com/sirupsen/logrus v1.4.1
+ gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c --- a/go.sum Sun Apr 07 02:05:16 2019 -0500
+++ b/go.sum Sun Apr 07 06:03:13 2019 -0500
@@ -2,6 +2,8 @@
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvrbUHiqye8wRJMlnYI=
github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
+github.com/alecthomas/kong v0.1.16 h1:BheBKuvr6FE1unlZVdqkdZo/D/eDu90rrVIlpPbOdgw= +github.com/alecthomas/kong v0.1.16/go.mod h1:0m2VYms8rH0qbCqVB2gvGHk74bqLIq0HXjCs5bNbNQU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
@@ -49,11 +51,14 @@
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/nicklaw5/helix v0.5.1 h1:Z6y2RLlLtMljcsVw+q/A3x+1TS8qnFoj7ddnM69jnVk=
github.com/nicklaw5/helix v0.5.1/go.mod h1:nRcok4VLg8ONQYW/iXBZ24wcfiJjTlDbhgk0ZatOrUY=
+github.com/nicksnyder/go-i18n v1.10.0 h1:5AzlPKvXBH4qBzmZ09Ua9Gipyruv6uApMcrNZdo96+Q= +github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2xBGjv0j365Q= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
+github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -110,6 +115,8 @@
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
+gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c h1:vTxShRUnK60yd8DZU+f95p1zSLj814+5CuEh7NjF2/Y= +gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c/go.mod h1:3HH7i1SgMqlzxCcBmUHW657sD4Kvv9sC3HpL3YukzwA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
--- a/main.go Sun Apr 07 02:05:16 2019 -0500
+++ b/main.go Sun Apr 07 06:03:13 2019 -0500
@@ -7,8 +7,8 @@
- "github.com/alecthomas/kingpin"
log "github.com/sirupsen/logrus"
+ "gopkg.in/alecthomas/kingpin.v3-unstable" "bitbucket.org/TheToyz/nowdead/database"
"bitbucket.org/TheToyz/nowdead/discord"