-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiscord-webhook-data-limit-check.sh
executable file
·218 lines (157 loc) · 6.11 KB
/
discord-webhook-data-limit-check.sh
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash
#### SCRIP USAGE FUNCTION ####
# Usage function.
usage() {
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -m, --message <json-content> The Discord webhook message JSON to be checked."
echo " -d, --debug Turns on console output"
echo " -h, --help Show this help message and exit."
echo ""
echo "This script will check a Discord webhook message JSON string against content"
echo "limits set by Discord. The message data must be within the limits in order"
echo "to be successfully sent. The script will return an exit number based on the"
echo "results of the check."
echo ""
echo " 0) The message data is within the limimts."
echo " 1) The message data is outside limits, but can be sent in multiple messages."
echo " 2) The message data is outside limits and cannot be sent."
echo ""
echo "Refer to Discord Webook documentation for more details on the webhook limits:"
echo ""
echo " https://birdie0.github.io/discord-webhooks-guide/other/field_limits.html"
echo ""
}
#### PARSE ARGUMENTS ####
# Parsed from command line arguments.
while [[ $# -gt 0 ]]; do
case "$1" in
-m|--message)
jsonMessage="$2"
shift 2
;;
-d|--debug)
debug=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: $1" >&2
usage
exit 2
;;
esac
done
# Check if JSON is provided and not empty
if [ -z "${jsonMessage}" ]; then
echo "Error: The --message option is mandatory." >&2
usage
exit 2
fi
#### INITIALIZATION & PARAMETERS ####
# Character lenght limits & object count limits (the order is important!)
limits=(
'Username .username 80 char 1'
'Content .content 2000 char 1'
'Embeds .embeds 10 obj 0'
'Author .embeds[].author.name 256 char 1'
'Title .embeds[].title 256 char 1'
'Description .embeds[].description 1024 char 1'
'Fields .embeds[].fields 25 obj 1'
'Name .embeds[].fields[].name 256 char 1'
'Value .embeds[].fields[].value 1024 char 1'
'Footer .embeds[].footer.text 2048 char 1'
'Totals na 6000 char 1'
'Total na 6000 char 0'
)
# Set limit parameter names
limitParameters="name section value type critical count"
# Initialize result variables
criticalResult=false
outsideLimits=false
results=""
totalCharactersAllEmbeds=0
#### LIMIT CHECK & RESULTS FUNCTION ####
# Get count, check against limit and update results
function updateResults() {
local indexedSection="$1"
# Get the object/character count if section is an object
[ "${indexedSection}" != "na" ] && count=$(jq "$indexedSection | length" <<< "${jsonMessage}")
# Output info if debug enabled
[ ${debug} ] && echo "${name}: ${count} / ${value}" >&2
# Compare count with limit value
if [[ ${count} -gt ${value} ]]; then
# Set 'outside limit' flag
outsideLimits=true
# Check if limit is a critical one
if [ "${critical}" == "1" ]; then
# Set 'critical limit' flag
criticalResult=true
fi
# Append to result if outside limit
results+="${name} ${indexedSection} ${value} ${type} ${critical} ${count}\n"
fi
}
#### CHECK CONTENT ####
# Check content agains each limit
for limit in "${limits[@]}"; do
# Read parameters from limit
IFS=' ' read -r $limitParameters <<< "$limit"
# Check if limit relates to the embeds section
if [[ "${section}" == *"embeds[]"* ]]; then
# Check each embed section individually for applicable limits
for (( i=0; i<$(jq ".embeds | length" <<< "${jsonMessage}"); i++ )); do
# Inject array index in embeds element for correct parsing
embedSection="${section/embeds[]/embeds[${i}]}"
# Check if section is a field section as this is an array
if [[ "${section}" == *"fields[]"* ]]; then
# Check each field section individually for applicable limits
for (( j=0; j<$(jq ".embeds[$i].fields | length" <<< "${jsonMessage}"); j++ )); do
# Inject array index in fields element for correct parsing
fieldSection="${embedSection/fields[]/fields[${j}]}"
# Check limits and update results
updateResults "${fieldSection}"
# Add to total characters if character type
[ "${type}" == "char" ] && ((embedTotals[$i]+=count))
done
else
# Check limits and update results
updateResults "${embedSection}"
# Add to total characters if character type
[ "${type}" == "char" ] && ((embedTotals[$i]+=count))
fi
done
elif [[ "${name}" == "Totals" ]]; then
# Read the total character count for each embed section
for count in "${embedTotals[@]}"; do
# Check agains limit and update result
updateResults "${section}" ${count}
# Add to the overall total character count
((totalCharactersAllEmbeds+=count))
done
elif [[ "${name}" == "Total" ]]; then
# Get the total character count for all embeds
count=${totalCharactersAllEmbeds}
# Check agains limit and update result
updateResults "${section}" ${count}
else
# Check agains limit and update result
updateResults "${section}"
fi
done
#### RETURN RESULTS ####
# Output results
[ ${debug} ] && [ -n "${results}" ] && echo -e "${results%??}" >&2
# Exit with appropriate status
if ${criticalResult}; then
exit 2
elif ${outsideLimits}; then
exit 1
else
exit 0
fi