* Status command has been split into `status-get` and `status-set`
* The status command now allows querying other users as well
* Improve documentation for status command
Partially Addresses
MODIFY
app/app.go
app/app.go
@@ -102,7 +102,11 @@ func Run() {
log.Fatalf("Error constructing window (%s).\n", createError.Error())
}
- window.RegisterCommand(commandimpls.NewStatusCommand(discord), "status")
+ statusGetCmd := commandimpls.NewStatusGetCommand(discord)
+ statusSetCmd := commandimpls.NewStatusSetCommand(discord)
+ window.RegisterCommand(statusSetCmd, "status-set", "status-update")
+ window.RegisterCommand(statusGetCmd, "status-get")
+ window.RegisterCommand(commandimpls.NewStatusCommand(statusGetCmd, statusSetCmd), "status")
window.RegisterCommand(commandimpls.NewFileSendCommand(discord, window), "file-send")
window.RegisterCommand(commandimpls.NewAccount(runNext, window), "account", "profile")
window.RegisterCommand(commandimpls.NewHelpCommand(window), "help")

MODIFY
commands/commandimpls/status.go
commands/commandimpls/status.go
@@ -8,34 +8,83 @@ import (
"github.com/Bios-Marcel/discordgo"
)
-const statusDocumentation = `[orange][::u]# status[white]
+const (
+ statusHelpPage = `[::b]NAME
+ status - view your or others status or update your own
-Current status: %s
+[::b]SYNPOSIS
+ [::b]status [subcommand]
-The status command always setting or reading the current status of your user.
+[::b]DESCRPTION
+ This command allows to either update your status or view a users status.
+ For more information check the help pages of the subcommands.
-If not supplied with any parameters, it will just print the help page and the current status.
+[::]SUBCOMMANDS
+ [::b]status-get (default)
+ prints the status of the given user or yourself
+ [::b]set-set
+ updates your current status`
-If supplied with a valid status value, that value will be set as the new status.
+ statusSetHelpPage = `[::b]NAME
+ status-set - allows updating your own status
-Valid values:
- * online
- * dnd
- * idle
- * invisible
-`
+[::b]SYNPOSIS
+ [::b]status-set[::-] <online|idle|dnd|invisible>
-// Status represents the command that allows the user to read and write his
-// user status on discord.
-type Status struct {
+[::b]DESCRPTION
+ This command can be used to set your current online status to the
+ value passed as the first parameter. Other users will immediately
+ see your status update.
+
+[::b]EXAMPLES
+ [gray]$ status-set invisible`
+
+ statusGetHelpPage = `[::b]NAME
+ status-get - prints your current status or the status of the given user
+
+[::b]SYNPOSIS
+ [::b]status-get[::-] [Username|Username#NNNN|UserID[]
+
+[::b]DESCRPTION
+ This command prints either your current status of no value was passed
+ or the status of the passed user, if the presence for that user could
+ be found. Due to a problem with the presences, this command might randomly
+ fail when trying to query specific users.
+
+[::b]EXAMPLES
+ [gray]$ status-get
+ [yellow]idle
+
+ [gray]$ status-get Marcel#7299
+ [red]Do not disturb`
+)
+
+type StatusCmd struct {
+ statusGetCmd *StatusGetCmd
+ statusSetCmd *StatusSetCmd
+}
+
+type StatusGetCmd struct {
session *discordgo.Session
}
-// NewStatusCommand creates a new ready to use status command.
-func NewStatusCommand(session *discordgo.Session) *Status {
- return &Status{
- session: session,
+type StatusSetCmd struct {
+ session *discordgo.Session
+}
+
+func NewStatusCommand(statusGetCmd *StatusGetCmd, statusSetCmd *StatusSetCmd) *StatusCmd {
+ return &StatusCmd{
+ statusGetCmd: statusGetCmd,
+ statusSetCmd: statusSetCmd,
+ }
+}
+
+func NewStatusGetCommand(session *discordgo.Session) *StatusGetCmd {
+ return &StatusGetCmd{session}
}
+
+func NewStatusSetCommand(session *discordgo.Session) *StatusSetCmd {
+ return &StatusSetCmd{session}
}
func statusToString(status discordgo.Status) string {
@@ -50,52 +99,98 @@ func statusToString(status discordgo.Status) string {
return "[gray]Invisible[white]"
case discordgo.StatusOffline:
return "[gray]Offline[white]"
- }
-
+ default:
return "Unknown status"
}
+}
-// Execute runs the command piping its output into the supplied writer.
-func (status *Status) Execute(writer io.Writer, parameters []string) {
+func (cmd *StatusGetCmd) Execute(writer io.Writer, parameters []string) {
+ if len(parameters) > 1 {
+ fmt.Fprintln(writer, "[red]Invalid parameters")
+ cmd.PrintHelp(writer)
+ return
+ }
if len(parameters) == 0 {
- fmt.Fprintf(writer, "Current status: %s \n", statusToString(status.session.State.Settings.Status))
- } else if len(parameters) == 1 {
+ fmt.Fprintln(writer, statusToString(cmd.session.State.Settings.Status))
+ return
+ }
+
+ input := parameters[0]
+ var matches []*discordgo.Presence
+ for _, presence := range cmd.session.State.Presences {
+ user := presence.User
+ if user.ID == input || user.Username == input || user.String() == input {
+ matches = append(matches, presence)
+ }
+ }
+
+ if len(matches) == 0 {
+ fmt.Fprintf(writer, "[red]No match for '%s'.\n", input)
+ } else if len(matches) > 1 {
+ fmt.Fprintf(writer, "Multiple matches were found for '%s'. Please be more precise.\n", input)
+ fmt.Fprintln(writer, "The following matches were found:")
+ for _, match := range matches {
+ fmt.Fprintf(writer, "\t%s\n", match.User.String())
+ }
+ } else {
+ fmt.Fprintln(writer, statusToString(matches[0].Status))
+ }
+}
+
+func (cmd *StatusSetCmd) Execute(writer io.Writer, parameters []string) {
+ if len(parameters) == 0 || len(parameters) > 1 {
+ fmt.Fprintln(writer, "[red]Invalid parameters")
+ cmd.PrintHelp(writer)
+ return
+ }
+
var settingStatusError error
var updatedSettings *discordgo.Settings
switch strings.ToLower(parameters[0]) {
case "online", "available":
- updatedSettings, settingStatusError = status.session.UserUpdateStatus(discordgo.StatusOnline)
+ updatedSettings, settingStatusError = cmd.session.UserUpdateStatus(discordgo.StatusOnline)
case "dnd", "donotdisturb", "busy":
- updatedSettings, settingStatusError = status.session.UserUpdateStatus(discordgo.StatusDoNotDisturb)
+ updatedSettings, settingStatusError = cmd.session.UserUpdateStatus(discordgo.StatusDoNotDisturb)
case "idle":
- updatedSettings, settingStatusError = status.session.UserUpdateStatus(discordgo.StatusIdle)
+ updatedSettings, settingStatusError = cmd.session.UserUpdateStatus(discordgo.StatusIdle)
case "invisible":
- updatedSettings, settingStatusError = status.session.UserUpdateStatus(discordgo.StatusInvisible)
+ updatedSettings, settingStatusError = cmd.session.UserUpdateStatus(discordgo.StatusInvisible)
default:
- status.PrintHelp(writer)
+ fmt.Fprintf(writer, "[red]Invalid status: '%s'\n", strings.ToLower(parameters[0]))
+ cmd.PrintHelp(writer)
}
if settingStatusError != nil {
- fmt.Fprintf(writer, "Error setting status: '%s'\n", settingStatusError.Error())
+ fmt.Fprintf(writer, "[red]Error setting status:\n\t[red]'%s'\n", settingStatusError.Error())
} else if updatedSettings != nil {
- fmt.Fprintf(writer, "Setting status to '%s'\n", statusToString(updatedSettings.Status))
- status.session.State.Settings = updatedSettings
+ cmd.session.State.Settings = updatedSettings
+ }
+}
+
+func (cmd *StatusCmd) Execute(writer io.Writer, parameters []string) {
+ if len(parameters) >= 1 {
+ if parameters[0] == "set" || parameters[0] == "update" {
+ cmd.statusSetCmd.Execute(writer, parameters[1:])
+ } else if parameters[0] == "get" {
+ cmd.statusGetCmd.Execute(writer, parameters[1:])
+ } else {
+ cmd.statusGetCmd.Execute(writer, parameters)
}
} else {
- status.PrintHelp(writer)
+ cmd.statusGetCmd.Execute(writer, parameters)
+ }
}
+func (cmd *StatusCmd) PrintHelp(writer io.Writer) {
+ fmt.Fprintln(writer, statusHelpPage)
}
-// Name represents this commands indentifier.
-func (status *Status) Name() string {
- return "status"
+func (cmd *StatusSetCmd) PrintHelp(writer io.Writer) {
+ fmt.Fprintln(writer, statusSetHelpPage)
}
-// PrintHelp the help page for this command. On top of the usual static content
-// this help page also contains the current user state.
-func (status *Status) PrintHelp(writer io.Writer) {
- fmt.Fprintf(writer, statusDocumentation, statusToString(status.session.State.Settings.Status))
+func (cmd *StatusGetCmd) PrintHelp(writer io.Writer) {
+ fmt.Fprintln(writer, statusGetHelpPage)
}

* Status command has been split into `status-get` and `status-set`
* The status command now allows querying other users as well
* Improve documentation for status command

Partially Addresses #75