-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiscord-webhook.sh
executable file
·191 lines (147 loc) · 5.52 KB
/
discord-webhook.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
#!/usr/bin/bash
#### SET PATHS ####
# Get path to where script is located.
scriptPath=$(echo "${0%/*}")
# Set other paths.
limitCheckScrip=${scriptPath}/discord-webhook-data-limit-check.sh
lastMessageFile=${scriptPath}/discord-webhook-last-message.json
#### SCRIP USAGE FUNCTION ####
# Usage function.
usage() {
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -c, --content <content> Set the content of the Discord message."
echo " -e, --embeds <embeds> Set the embeds of the Discord message."
echo " -f, --file <file-path> Set the file attachment path (optional)."
echo " -d, --debug Turns on console output"
echo " -h, --help Show this help message and exit."
echo ""
echo "Refer to the Discord documentation for more information on Webhooks"
echo ""
echo " https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks"
echo ""
}
### SEND WEBHOOK FUNCTION ###
# Send Discord notification with or without payloaad.
sendWebhook() {
local discordJsonData="$1"
local includeAttachment="$2"
# Write last message that was attmepted to be sent to file
echo "${discordJsonData}" > ${lastMessageFile}
# Check if attachment pathe exits and if is should be included in the message
if [ -z "${discordAttachmentPath}" ] || [ "${includeAttachment}" = "false" ]; then
# Send message without attachment
reponse=$(curl -s -H "Content-Type: application/json" -d "$discordJsonData" ${discordWebhookBase}"/"${discordID}"/"${discordToken})
else
# Send message with attachment
response=$(curl -s -F payload_json="${discordJsonData}" -F "file1=@${discordAttachmentPath}" ${discordWebhookBase}"/"${discordID}"/"${discordToken})
fi
# Check if curl was successful
if [ $? -ne 0 ]; then
echo "Error: Failed to send webhook message." >&2
exit 1
fi
# Output curl reposnse if debug enabled
[ ${debug} ] && echo "${response}" >&2
}
#### PARSE ARGUMENTS ####
# Parsed from command line arguments.
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--content)
discordMessageContent="$2"
shift 2
;;
-e|--embeds)
discordMessageEmbeds="$2"
shift 2
;;
-f|--file)
discordAttachmentPath="$2"
shift 2
;;
-d|--debug)
debug=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: $1" >&2
usage
exit 1
;;
esac
done
# Check if JSON is provided and not empty
if [ -z "${discordMessageContent}" ] && [ -z "${discordMessageEmbeds}" ]; then
echo "Error: Either a message 'content' or message 'embed' is required." >&2
usage
exit 1
fi
#### DISCORD VARIABLES ####
# Discord webhook address base.
discordWebhookBase="https://discord.com/api/webhooks"
# Import secret variables.
source ${scriptPath}/discord-variables.sh
# Discord secret variables.
discordToken=${DISCORD_TOKEN}
discordID=${DISCORD_ID}
discordUsername=${DISCORD_USER}
discordAvatarURL=${DISCORD_AVATAR_URL}
discordRoleID=${DISCORD_ROLE_ID}
#### REPLACE ROLES ####
# Replace @admin mention with correct ID.
discordMessageEmbeds=$(echo "${discordMessageEmbeds}" | sed 's/\@admin/\<\@\&'${discordRoleID}'/g')
#### BUILD DISCORD MESSAGE ####
# Complete the Discord JSON string.
discordJson='{ "username":"'"${discordUsername}"'",
"content":"'"${discordMessageContent}"'",
"avatar_url":"'"${discordAvatarURL}"'",
"allowed_mentions": {
"roles": [ "'"${discordRoleID}"'" ]
},
"embeds": [ '${discordMessageEmbeds%?}' ]
}'
#### MESSAGE LIMIT CHECK & SEND ####
# Call script to perform limit checks (pass on debug argument if debug enabled)
${limitCheckScrip} -m "${discordJson}" ${debug:+-d}
# Send full, split or drop message based in limit check
case ${?} in
# Send full message
0)
# Output info if debug enabled
[ ${debug} ] && echo "Content within limits. Sending full webhook message." >&2
# Send full Json
sendWebhook "${discordJson}" true
;;
# Split message in multiple webhooks
1)
# Output info if debug enabled
[ ${debug} ] && echo "Content to large, but within manageable limits. Splitting webhook message" >&2
# Remove embeds section
discordJsonMinusEmbeds=$(jq "del(.embeds)" <<< "${discordJson}")
# Send message without embeds
sendWebhook "${discordJsonMinusEmbeds}" true
# Get number of embeds in original message
embedsCount=$(jq ".embeds | length" <<< "${discordJson}")
# Send each embed as a separate message
for (( index=0; index<$embedsCount; index++ )); do
# Get current embed in embeds section
embed=$(jq ".embeds[$index]" <<< "${discordJson}")
# Replace embeds in orignal message with current single embed and remove content section
discordJsonSingleEmbed=$(jq ".embeds=[$embed] | del(.content)" <<< "${discordJson}")
# Send message with single embed and without the rest of the data
sendWebhook "${discordJsonSingleEmbed}" false
done
;;
# Drop message and exit with error
*)
echo "Error: Limit check failed or content outside manageable limits. Unable to send webhook." >&2
exit 1
;;
esac