grim/wasdead

add a show-config command
draft
2019-05-11, Gary Kramlich
6efee9941570
add a show-config command
package discord
import (
"fmt"
"strings"
"github.com/alecthomas/kong"
"github.com/bwmarrin/discordgo"
"github.com/dustin/go-humanize"
"bitbucket.org/TheToyz/nowdead/database"
"bitbucket.org/TheToyz/nowdead/presence"
)
type Globals struct {
client *DiscordClient
msg *discordgo.MessageCreate
ctx *kong.Context
}
type Commands struct {
Globals
Help HelpCmd `kong:"cmd,help='displays this help message'"`
SetChannel SetChannelCmd `kong:"cmd,help='set the current channel as the channel to announce new streams'"`
ShowConfig ShowConfigCmd `kong:"cmd,help='show the configuration for the current guild.'"`
Status StatusCmd `kong:"cmd,help='get the streaming status of someone'"`
Uptime UptimeCmd `kong:"cmd,help='display how long the bot has been running for'"`
}
type HelpCmd struct {
Command []string `kong:"arg,optional,help='command to get help on'"`
}
func (c *HelpCmd) Run(g *Globals) error {
ctx, err := kong.Trace(g.ctx.Kong, c.Command)
if err != nil {
return err
}
if ctx.Error != nil {
return ctx.Error
}
err = ctx.PrintUsage(true)
if err != nil {
return err
}
fmt.Fprintln(g.ctx.Stdout)
return nil
}
type SetChannelCmd struct{}
func (c *SetChannelCmd) Run(g *Globals) error {
guild, err := database.LoadGuild(g.msg.GuildID)
if err != nil {
return err
}
resp := fmt.Sprintf("<#%s> is already set as the announcement channel", g.msg.ChannelID)
if guild.Channel != g.msg.ChannelID {
guild.Channel = g.msg.ChannelID
guild.Save()
resp = fmt.Sprintf("Set <#%s> as the announcement channel", g.msg.ChannelID)
}
g.client.sendChannel(g.msg.ChannelID, resp)
return nil
}
type ShowConfigCmd struct{}
func (c *ShowConfigCmd) Run(g *Globals) error {
guild, err := database.LoadGuild(g.msg.GuildID)
if err != nil {
return err
}
channel, err := g.client.client.Channel(guild.Channel)
if err != nil {
return err
}
embed := &discordgo.MessageEmbed{
Title: "@wasdead configuration",
Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField{
Name: "Announcement Channel",
Value: channel.Mention(),
},
},
}
g.client.sendChannelEmbed(g.msg.ChannelID, embed)
return nil
}
type StatusCmd struct {
Target string `kong:"arg"`
}
func (c *StatusCmd) Run(g *Globals) error {
uri := ""
if strings.HasPrefix(c.Target, "<@") {
mentionID := c.Target[2 : len(c.Target)-1]
for _, mentioned := range g.msg.Mentions {
if mentioned.ID == mentionID {
p := g.client.getPresence(g.msg.GuildID, mentioned.ID)
if p != nil && p.Game != nil && p.Game.Type == discordgo.GameTypeStreaming {
uri = p.Game.URL
} else {
g.client.sendChannel(
g.msg.ChannelID,
fmt.Sprintf("%s is not currently streaming", c.Target),
)
}
}
}
}
if uri == "" {
uri = "https://twitch.tv/" + c.Target
}
presence, err := presence.GetPresence(uri)
if err != nil {
return err
}
return g.client.sendPresenceChannel(g.msg.ChannelID, presence)
}
type UptimeCmd struct{}
func (c *UptimeCmd) Run(g *Globals) error {
g.client.sendChannel(
g.msg.ChannelID,
fmt.Sprintf("Started %s", humanize.Time(g.client.started)),
)
return nil
}
func (c *DiscordClient) processCommand(args []string, m *discordgo.MessageCreate) error {
cmd := Commands{
Globals: Globals{
client: c,
msg: m,
},
}
buf := &strings.Builder{}
parser, err := kong.New(
&cmd,
kong.Description("This bot auto announces when people start streaming"),
kong.Exit(func(int) {}),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
kong.Name("@wasdead"),
kong.NoDefaultHelp(),
kong.Writers(buf, buf),
)
if err != nil {
c.sendChannel(m.ChannelID, fmt.Sprintf("error: %v", err))
return err
}
ctx, err := parser.Parse(args)
if err != nil {
c.sendChannel(m.ChannelID, fmt.Sprintf("error: %v", err))
return err
}
cmd.Globals.ctx = ctx
err = ctx.Run(&cmd.Globals)
if err != nil {
c.sendChannel(m.ChannelID, fmt.Sprintf("error: %v", err))
return err
}
if buf.Len() > 0 {
c.sendChannel(m.ChannelID, fmt.Sprintf("```%s```", buf.String()))
}
return nil
}