This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomstring.go
91 lines (79 loc) · 2.06 KB
/
randomstring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
{{ $default := 15 }}
{{ $max := 60 }}
{{ $lower := "abcdefghijklmnopqrstuvwxyz" }}
{{ $upper := upper $lower }}
{{ $alpha := print $lower $upper }}
{{ $num := "0123456789" }}
{{ $sym := "~!@#$%&*" }}
{{ $chars := sdict
"alpha" $alpha
"lower" $lower
"upper" $upper
"num" $num
"sym" $sym
"mix" (print $alpha $num $sym)
}}
{{ $amount := $default }}
{{ $list := $chars.mix }}
{{ $args := .CmdArgs }}
{{ $check := cslice "mix" }}
{{ if $args }}
{{ $list = "" }}
{{ $check = cslice }}
{{ range $args }}
{{- if reFind `^\d{1,}$` . -}}
{{- $amount = . | toInt -}}
{{- else -}}
{{- $type := lower . -}}
{{/* mix has all types, no need for other args */}}
{{- if eq $type "mix" -}}
{{- $list = $chars.mix -}}
{{- $check = cslice "mix" -}}
{{- break -}}
{{- end -}}
{{- if $chars.HasKey $type -}}
{{- if not (in $check $type) -}}
{{- $list = print $list ($chars.Get $type) -}}
{{- $check = $check.Append $type -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end }}
{{/* alpha already has lower and upper */}}
{{ if and (in $check "alpha") (or (in $check "upper") (in $check "lower")) }}
{{ $list = "" }}
{{ $new := cslice }}
{{ range $check }}
{{- if not (or (eq . "upper" "lower") (in $new .)) -}}
{{- $list = print $list ($chars.Get .) -}}
{{- $new = $new.Append . -}}
{{- end -}}
{{- end }}
{{ $check = $new }}
{{ end }}
{{ if not $list }}
{{ $list = $chars.mix }}
{{ $check = cslice "mix" }}
{{ end }}
{{ end }}
{{ $list = split $list "" }}
{{ $result := "" }}
{{ if or (lt $amount 1) (gt $amount $max) }}
{{ $amount = $default }}
{{ end }}
{{ range (seq 0 $amount) }}
{{- $result = print $result (index (shuffle $list) 0) -}}
{{- end }}
{{ $message := print
"```" $result "```" "\n"
"**Generated output:**" "\n"
"- Count: " $amount " characters" "\n"
"- Type: " (joinStr ", " $check.StringSlice) "\n"
}}
{{ $embed := cembed
"title" "Random String"
"description" $message
"footer" (sdict "text" "Options: alpha, lower, upper, num, sym, mix")
"color" 10530378
}}
{{ sendMessage nil $embed }}