@@ -3,11 +3,16 @@ package commandimpls
3
3
import (
4
4
"fmt"
5
5
"io"
6
+ "regexp"
7
+ "strconv"
6
8
"strings"
9
+ "time"
10
+
11
+ "github.com/Bios-Marcel/discordemojimap"
12
+ "github.com/Bios-Marcel/discordgo"
7
13
8
14
"github.com/Bios-Marcel/cordless/config"
9
15
"github.com/Bios-Marcel/cordless/ui/tviewutil"
10
- "github.com/Bios-Marcel/discordgo"
11
16
)
12
17
13
18
const (
@@ -25,7 +30,9 @@ const (
25
30
[::b]status-get (default)
26
31
prints the status of the given user or yourself
27
32
[::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`
29
36
30
37
statusSetHelpPage = `[::b]NAME
31
38
status-set - allows updating your own status
@@ -58,12 +65,35 @@ const (
58
65
[yellow]idle
59
66
60
67
[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`
62
91
)
63
92
64
93
type StatusCmd struct {
65
- statusGetCmd * StatusGetCmd
66
- statusSetCmd * StatusSetCmd
94
+ statusGetCmd * StatusGetCmd
95
+ statusSetCmd * StatusSetCmd
96
+ statusSetCustomCmd * StatusSetCustomCmd
67
97
}
68
98
69
99
type StatusGetCmd struct {
@@ -74,10 +104,15 @@ type StatusSetCmd struct {
74
104
session * discordgo.Session
75
105
}
76
106
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 {
78
112
return & StatusCmd {
79
- statusGetCmd : statusGetCmd ,
80
- statusSetCmd : statusSetCmd ,
113
+ statusGetCmd : statusGetCmd ,
114
+ statusSetCmd : statusSetCmd ,
115
+ statusSetCustomCmd : statusSetCustomCmd ,
81
116
}
82
117
}
83
118
@@ -89,21 +124,42 @@ func NewStatusSetCommand(session *discordgo.Session) *StatusSetCmd {
89
124
return & StatusSetCmd {session }
90
125
}
91
126
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
+
92
146
func statusToString (status discordgo.Status ) string {
147
+ statusString := "[" + statusColor (status ) + "]"
93
148
switch status {
94
149
case discordgo .StatusOnline :
95
- return "[green] Online[white] "
150
+ statusString += " Online"
96
151
case discordgo .StatusDoNotDisturb :
97
- return "[" + tviewutil . ColorToHex ( config . GetTheme (). ErrorColor ) + "] Do not disturb[white] "
152
+ statusString += " Do not disturb"
98
153
case discordgo .StatusIdle :
99
- return "[yellow] Idle[white] "
154
+ statusString += " Idle"
100
155
case discordgo .StatusInvisible :
101
- return "[gray] Invisible[white] "
156
+ statusString += " Invisible"
102
157
case discordgo .StatusOffline :
103
- return "[gray] Offline[white] "
158
+ statusString += " Offline"
104
159
default :
105
- return "Unknown status"
160
+ statusString += "Unknown status"
106
161
}
162
+ return statusString
107
163
}
108
164
109
165
func (cmd * StatusGetCmd ) Execute (writer io.Writer , parameters []string ) {
@@ -114,7 +170,13 @@ func (cmd *StatusGetCmd) Execute(writer io.Writer, parameters []string) {
114
170
}
115
171
116
172
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 " )
118
180
return
119
181
}
120
182
@@ -182,6 +244,8 @@ func (cmd *StatusCmd) Execute(writer io.Writer, parameters []string) {
182
244
cmd .statusSetCmd .Execute (writer , parameters [1 :])
183
245
} else if parameters [0 ] == "get" {
184
246
cmd .statusGetCmd .Execute (writer , parameters [1 :])
247
+ } else if parameters [0 ] == "set-custom" || parameters [0 ] == "custom-set" {
248
+ cmd .statusSetCustomCmd .Execute (writer , parameters [1 :])
185
249
} else {
186
250
cmd .statusGetCmd .Execute (writer , parameters )
187
251
}
@@ -190,6 +254,75 @@ func (cmd *StatusCmd) Execute(writer io.Writer, parameters []string) {
190
254
}
191
255
}
192
256
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
+
193
326
func (cmd * StatusCmd ) PrintHelp (writer io.Writer ) {
194
327
fmt .Fprintln (writer , statusHelpPage )
195
328
}
@@ -202,6 +335,14 @@ func (cmd *StatusGetCmd) PrintHelp(writer io.Writer) {
202
335
fmt .Fprintln (writer , statusGetHelpPage )
203
336
}
204
337
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
+
205
346
func (cmd * StatusSetCmd ) Name () string {
206
347
return "status-set"
207
348
}
@@ -214,6 +355,10 @@ func (cmd *StatusCmd) Name() string {
214
355
return "status"
215
356
}
216
357
358
+ func (cmd * StatusSetCustomCmd ) Aliases () []string {
359
+ return []string {"status-custom" }
360
+ }
361
+
217
362
func (cmd * StatusSetCmd ) Aliases () []string {
218
363
return []string {"status-update" }
219
364
}
0 commit comments