Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit 03c2faa

Browse files
committed
Merge branch 'BigHeadGeorge-custom-status' into dev
2 parents 7b26748 + 629cf25 commit 03c2faa

File tree

4 files changed

+166
-19
lines changed

4 files changed

+166
-19
lines changed

app/app.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ func Run() {
120120
window.RegisterCommand(commandimpls.NewVersionCommand())
121121
statusGetCmd := commandimpls.NewStatusGetCommand(discord)
122122
statusSetCmd := commandimpls.NewStatusSetCommand(discord)
123+
statusSetCustomCmd := commandimpls.NewStatusSetCustomCommand(discord)
123124
window.RegisterCommand(statusSetCmd)
124125
window.RegisterCommand(statusGetCmd)
125-
window.RegisterCommand(commandimpls.NewStatusCommand(statusGetCmd, statusSetCmd))
126+
window.RegisterCommand(statusSetCustomCmd)
127+
window.RegisterCommand(commandimpls.NewStatusCommand(statusGetCmd, statusSetCmd, statusSetCustomCmd))
126128
window.RegisterCommand(commandimpls.NewFileSendCommand(discord, window))
127129
window.RegisterCommand(commandimpls.NewAccount(runNext, window))
128130
window.RegisterCommand(commandimpls.NewManualCommand(window))

commands/commandimpls/status.go

Lines changed: 160 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ package commandimpls
33
import (
44
"fmt"
55
"io"
6+
"regexp"
7+
"strconv"
68
"strings"
9+
"time"
10+
11+
"github.com/Bios-Marcel/discordemojimap"
12+
"github.com/Bios-Marcel/discordgo"
713

814
"github.com/Bios-Marcel/cordless/config"
915
"github.com/Bios-Marcel/cordless/ui/tviewutil"
10-
"github.com/Bios-Marcel/discordgo"
1116
)
1217

1318
const (
@@ -25,7 +30,9 @@ const (
2530
[::b]status-get (default)
2631
prints the status of the given user or yourself
2732
[::b]status-set
28-
updates your current status`
33+
updates your current status
34+
[::b]status-set-custom
35+
set your status to a custom text`
2936

3037
statusSetHelpPage = `[::b]NAME
3138
status-set - allows updating your own status
@@ -58,12 +65,35 @@ const (
5865
[yellow]idle
5966
6067
[gray]$ status-get Marcel#7299
61-
"[" + tviewutil.ColorToHex(config.GetTheme().ErrorColor) + "]Do not disturb`
68+
[red]Do not disturb`
69+
70+
statusSetCustomHelpPage = `[::b]NAME
71+
status-set-custom - set a custom status text
72+
73+
[::b]SYNOPSIS
74+
[::b]status-set-custom[::-] [OPTION[]...
75+
76+
[::b]DESCRIPTION
77+
This command allows you to set a custom status.
78+
79+
[::b]OPTIONS
80+
[::b]-s, --status
81+
status message
82+
[::b]-e, --emoji
83+
emoji in your status
84+
[::b]-i, --expire, --expiry <s|m|h>
85+
time that the status expires after
86+
87+
[::b]EXAMPLES
88+
[gray]$ status-set-custom -s "shining bright" -e :sun:
89+
[gray]$ status-set-custom -s "shining bright" -e 🌞
90+
[gray]$ status-set-custom -s test -i 1h`
6291
)
6392

6493
type StatusCmd struct {
65-
statusGetCmd *StatusGetCmd
66-
statusSetCmd *StatusSetCmd
94+
statusGetCmd *StatusGetCmd
95+
statusSetCmd *StatusSetCmd
96+
statusSetCustomCmd *StatusSetCustomCmd
6797
}
6898

6999
type StatusGetCmd struct {
@@ -74,10 +104,15 @@ type StatusSetCmd struct {
74104
session *discordgo.Session
75105
}
76106

77-
func NewStatusCommand(statusGetCmd *StatusGetCmd, statusSetCmd *StatusSetCmd) *StatusCmd {
107+
type StatusSetCustomCmd struct {
108+
session *discordgo.Session
109+
}
110+
111+
func NewStatusCommand(statusGetCmd *StatusGetCmd, statusSetCmd *StatusSetCmd, statusSetCustomCmd *StatusSetCustomCmd) *StatusCmd {
78112
return &StatusCmd{
79-
statusGetCmd: statusGetCmd,
80-
statusSetCmd: statusSetCmd,
113+
statusGetCmd: statusGetCmd,
114+
statusSetCmd: statusSetCmd,
115+
statusSetCustomCmd: statusSetCustomCmd,
81116
}
82117
}
83118

@@ -89,21 +124,42 @@ func NewStatusSetCommand(session *discordgo.Session) *StatusSetCmd {
89124
return &StatusSetCmd{session}
90125
}
91126

127+
func NewStatusSetCustomCommand(session *discordgo.Session) *StatusSetCustomCmd {
128+
return &StatusSetCustomCmd{session}
129+
}
130+
131+
func statusColor(status discordgo.Status) string {
132+
switch status {
133+
case discordgo.StatusOnline:
134+
return "green"
135+
case discordgo.StatusDoNotDisturb:
136+
return "red"
137+
case discordgo.StatusIdle:
138+
return "yellow"
139+
case discordgo.StatusInvisible, discordgo.StatusOffline:
140+
return "gray"
141+
default:
142+
return "gray"
143+
}
144+
}
145+
92146
func statusToString(status discordgo.Status) string {
147+
statusString := "[" + statusColor(status) + "]"
93148
switch status {
94149
case discordgo.StatusOnline:
95-
return "[green]Online[white]"
150+
statusString += "Online"
96151
case discordgo.StatusDoNotDisturb:
97-
return "[" + tviewutil.ColorToHex(config.GetTheme().ErrorColor) + "]Do not disturb[white]"
152+
statusString += "Do not disturb"
98153
case discordgo.StatusIdle:
99-
return "[yellow]Idle[white]"
154+
statusString += "Idle"
100155
case discordgo.StatusInvisible:
101-
return "[gray]Invisible[white]"
156+
statusString += "Invisible"
102157
case discordgo.StatusOffline:
103-
return "[gray]Offline[white]"
158+
statusString += "Offline"
104159
default:
105-
return "Unknown status"
160+
statusString += "Unknown status"
106161
}
162+
return statusString
107163
}
108164

109165
func (cmd *StatusGetCmd) Execute(writer io.Writer, parameters []string) {
@@ -114,7 +170,13 @@ func (cmd *StatusGetCmd) Execute(writer io.Writer, parameters []string) {
114170
}
115171

116172
if len(parameters) == 0 {
117-
fmt.Fprintln(writer, statusToString(cmd.session.State.Settings.Status))
173+
fmt.Fprintf(writer, statusToString(cmd.session.State.Settings.Status))
174+
175+
customStatus := cmd.session.State.Settings.CustomStatus
176+
if len(customStatus.Text) > 0 || len(customStatus.EmojiName) > 0 {
177+
fmt.Fprintf(writer, ": %s %s", customStatus.EmojiName, customStatus.Text)
178+
}
179+
fmt.Fprintf(writer, "[white]\n")
118180
return
119181
}
120182

@@ -182,6 +244,8 @@ func (cmd *StatusCmd) Execute(writer io.Writer, parameters []string) {
182244
cmd.statusSetCmd.Execute(writer, parameters[1:])
183245
} else if parameters[0] == "get" {
184246
cmd.statusGetCmd.Execute(writer, parameters[1:])
247+
} else if parameters[0] == "set-custom" || parameters[0] == "custom-set" {
248+
cmd.statusSetCustomCmd.Execute(writer, parameters[1:])
185249
} else {
186250
cmd.statusGetCmd.Execute(writer, parameters)
187251
}
@@ -190,6 +254,75 @@ func (cmd *StatusCmd) Execute(writer io.Writer, parameters []string) {
190254
}
191255
}
192256

257+
func (cmd *StatusSetCustomCmd) Execute(writer io.Writer, parameters []string) {
258+
if cmd.session.State.User.Bot {
259+
fmt.Fprintln(writer, "[red]This command can't be used by bots due to Discord API restrictions.")
260+
return
261+
}
262+
263+
if len(parameters) == 0 {
264+
fmt.Fprintln(writer, "["+tviewutil.ColorToHex(config.GetTheme().ErrorColor)+"]Invalid parameters")
265+
cmd.PrintHelp(writer)
266+
return
267+
}
268+
269+
errorColor := tviewutil.ColorToHex(config.GetTheme().ErrorColor)
270+
var customStatus discordgo.CustomStatus
271+
for index, param := range parameters {
272+
switch param {
273+
case "-s", "--status":
274+
if index != len(parameters)-1 {
275+
customStatus.Text = parameters[index+1]
276+
} else {
277+
fmt.Fprintf(writer, "[%s]Error, you didn't supply a status\n", errorColor)
278+
return
279+
}
280+
case "-e", "--emoji":
281+
if index != len(parameters)-1 {
282+
if discordemojimap.ContainsEmoji(parameters[index+1]) {
283+
customStatus.EmojiName = parameters[index+1]
284+
} else if emoji := discordemojimap.Replace(parameters[index+1]); emoji != parameters[index+1] {
285+
customStatus.EmojiName = emoji
286+
} else {
287+
fmt.Fprintf(writer, "[%s]Invalid emoji\n", errorColor)
288+
return
289+
}
290+
}
291+
case "-i", "--expire", "--expiry":
292+
if m, _ := regexp.MatchString(`\d+(s|m|h)`, parameters[index+1]); m {
293+
lastIndex := len(parameters[index+1]) - 1
294+
num, err := strconv.Atoi(parameters[index+1][:lastIndex])
295+
if err != nil {
296+
fmt.Fprintf(writer, "[%s]Invalid expiry\n", errorColor)
297+
return
298+
}
299+
300+
now := time.Now().UTC()
301+
switch parameters[index+1][lastIndex] {
302+
case 's':
303+
now = now.Add(time.Second * time.Duration(num))
304+
case 'm':
305+
now = now.Add(time.Minute * time.Duration(num))
306+
case 'h':
307+
now = now.Add(time.Hour * time.Duration(num))
308+
default:
309+
fmt.Fprintf(writer, "[%s]Invalid time character: %c != <s|m|h>\n", errorColor, parameters[index+1][lastIndex])
310+
return
311+
}
312+
customStatus.ExpiresAt = now.Format(time.RFC3339Nano)
313+
}
314+
}
315+
}
316+
317+
settings, err := cmd.session.UserUpdateStatusCustom(customStatus)
318+
if err != nil {
319+
fmt.Fprintf(writer, "[%s]Error updating custom status:\n\t[%s]'%s'\n", errorColor, errorColor, err.Error())
320+
return
321+
} else if settings != nil {
322+
cmd.session.State.Settings = settings
323+
}
324+
}
325+
193326
func (cmd *StatusCmd) PrintHelp(writer io.Writer) {
194327
fmt.Fprintln(writer, statusHelpPage)
195328
}
@@ -202,6 +335,14 @@ func (cmd *StatusGetCmd) PrintHelp(writer io.Writer) {
202335
fmt.Fprintln(writer, statusGetHelpPage)
203336
}
204337

338+
func (cmd *StatusSetCustomCmd) PrintHelp(writer io.Writer) {
339+
fmt.Fprintln(writer, statusSetCustomHelpPage)
340+
}
341+
342+
func (cmd *StatusSetCustomCmd) Name() string {
343+
return "status-set-custom"
344+
}
345+
205346
func (cmd *StatusSetCmd) Name() string {
206347
return "status-set"
207348
}
@@ -214,6 +355,10 @@ func (cmd *StatusCmd) Name() string {
214355
return "status"
215356
}
216357

358+
func (cmd *StatusSetCustomCmd) Aliases() []string {
359+
return []string{"status-custom"}
360+
}
361+
217362
func (cmd *StatusSetCmd) Aliases() []string {
218363
return []string{"status-update"}
219364
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.12
44

55
require (
66
github.com/Bios-Marcel/discordemojimap v1.0.1
7-
github.com/Bios-Marcel/discordgo v0.20.5-0.20191206185819-8703d1772aea
7+
github.com/Bios-Marcel/discordgo v0.20.5-0.20200107213334-6da23eee3010
88
github.com/Bios-Marcel/femto v0.0.0-20191230002044-a733a9347742
99
github.com/Bios-Marcel/goclipimg v0.0.0-20191117180634-d0f7b06fbe82
1010
github.com/Bios-Marcel/shortnotforlong v1.0.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/Bios-Marcel/discordemojimap v1.0.1 h1:b3UYPO7+h1+ciStkwU/KQCerOmpUNPHsBf4a7EjMdys=
22
github.com/Bios-Marcel/discordemojimap v1.0.1/go.mod h1:AoHIpUwf3EVCAAUmk+keXjb9khyZcFnW84/rhJd4IkU=
3-
github.com/Bios-Marcel/discordgo v0.20.5-0.20191206185819-8703d1772aea h1:yRHbn86fLllPO0tOg0cyNKCrTRuZ2OE3XV4UW9qEYxs=
4-
github.com/Bios-Marcel/discordgo v0.20.5-0.20191206185819-8703d1772aea/go.mod h1:bLnfQU0j/SejmPozgW5GepmKvd8CrbMIml2I0IZENVE=
3+
github.com/Bios-Marcel/discordgo v0.20.5-0.20200107213334-6da23eee3010 h1:f6LsOhETyxf3lkxZ65nrDladW85pvDQ5OVMRZFNIY7I=
4+
github.com/Bios-Marcel/discordgo v0.20.5-0.20200107213334-6da23eee3010/go.mod h1:bLnfQU0j/SejmPozgW5GepmKvd8CrbMIml2I0IZENVE=
55
github.com/Bios-Marcel/femto v0.0.0-20191230002044-a733a9347742 h1:Wz2inrn2ahZ+lTz78782MWy63TAllr/WzIxR+nDAoP8=
66
github.com/Bios-Marcel/femto v0.0.0-20191230002044-a733a9347742/go.mod h1:A/NU56Hy+SDheOTrEyk6iEW0erjHf2HTySbB7z8OgeA=
77
github.com/Bios-Marcel/goclipimg v0.0.0-20191117180634-d0f7b06fbe82 h1:gspJ6CW9bhboosSISmuX2iq03pUsYHzlJN0s+z4fz4E=

0 commit comments

Comments
 (0)