Skip to content

Commit e1dc1a7

Browse files
committed
Add support for webhook modification
1 parent 4f3c175 commit e1dc1a7

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ $ ./discord.sh \
132132
133133
![](https://i.imgur.com/lni4fI3.png)
134134

135+
### `--modify`
136+
> You can permanently change the username and avatar of the webhook.
137+
> The following options are valid: `--username` and `--modify`
138+
139+
> **Warning:**
140+
> No other options may be passed, including those for sending messages.
141+
142+
#### Example
143+
```bash
144+
$ ./discord.sh \
145+
--modify \
146+
--username "NotifBot" \
147+
--avatar "https://i.imgur.com/12jyR5Q.png"
148+
```
149+
150+
Once executed, all other webhook messages by default will contain the username and avatar set.
151+
152+
![](https://i.imgur.com/ZYUBiil.png)
135153
## Advanced Options
136154

137155
Now we're going to look at how to setup a custom embed message.

discord.sh

+53-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ File options:
5959
Identity options:
6060
--username <name> Set username to <name>
6161
--avatar <url> Set avatar to image located at <url>
62+
--modify Modify webhook (pass with avatar/username permanantly update webhook)
6263
6364
Embedded content options:
6465
Main:
@@ -136,6 +137,8 @@ while (( "$#" )); do
136137
--webhook-url=*) webhook_url=${1/--webhook-url=/''}; shift;;
137138
--webhook-url*) webhook_url=${2}; shift; shift;;
138139

140+
--modify*) modify=1; shift;;
141+
139142
--username=*) username=${1/--username=/''}; shift;;
140143
--username*) username=${2}; shift; shift;;
141144

@@ -331,6 +334,28 @@ build_embed() {
331334
echo ", \"embeds\": [{ \"_\": \"_\"${_title}${_desc}${_eurl}${_color}${_ts}${_author}${_thumb}${_image}${_fields}${_footer} }]"
332335
}
333336

337+
convert_image_link_to_base64() {
338+
local _image
339+
local _image_type
340+
local _image_base64
341+
342+
# save image to temp file
343+
_image="$(mktemp)"
344+
curl -s "${1}" > "${_image}"
345+
346+
# get image type
347+
_image_type="$(file -b --mime-type "${_image}")"
348+
349+
# convert to base64
350+
_image_base64=$(<"$_image" base64)
351+
352+
# remove temp file
353+
rm "${_image}"
354+
355+
# output
356+
echo "data:${_image_type};base64,${_image_base64}"
357+
}
358+
334359

335360
build() {
336361
local _content
@@ -344,8 +369,16 @@ build() {
344369
[[ -z "${embed_title}" ]] && \
345370
[[ -z "${embed_description}" ]] && \
346371
[[ -z "${embed_imageurl}" ]] && \
372+
[[ -z "${modify}" ]] && \
373+
[[ -z "${username}" ]] && \
374+
[[ -z "${avatar_url}" ]] && \
347375
echo "fatal: nothing to build" && exit 1
348-
376+
377+
# if only specified modify but not username/avatar, exit with error
378+
[[ -n "${modify}" ]] && \
379+
[[ -z "${username}" ]] && \
380+
[[ -z "${avatar_url}" ]] && \
381+
echo "fatal: must pass --username or --avatar with --modify" && exit 1
349382
# strip 0x prefix and convert hex to dec if necessary
350383
[[ -n "${embed_color}" ]] && [[ "${embed_color}" =~ ^0x[0-9a-fA-F]+$ ]] && embed_color="$(( embed_color ))"
351384

@@ -356,8 +389,14 @@ build() {
356389
# let's build, boys
357390
[[ -n "${is_tts}" ]] && _tts=", \"tts\": true"
358391
[[ -n "${text}" ]] && _content=", \"content\": \"${text}\""
359-
[[ -n "${username}" ]] && _username=", \"username\": \"${username}\""
360-
[[ -n "${avatar_url}" ]] && _avatar=", \"avatar_url\": \"${avatar_url}\""
392+
# if we're modifying, change the username field to name
393+
[[ -n "${username}" ]] && [[ -n "${modify}" ]] && _username=", \"name\": \"${username}\""
394+
[[ -n "${username}" ]] && [[ -z "${modify}" ]] && _username=", \"username\": \"${username}\""
395+
# if avatar_url is set and modify is set, change the avatar_url field to avatar and convert to base64
396+
# if avatar_url is set and modify is not set, change the avatar_url field to avatar
397+
[[ -n "${avatar_url}" ]] && [[ -n "${modify}" ]] && _avatar=", \"avatar\": \"$(convert_image_link_to_base64 "${avatar_url}")\""
398+
[[ -n "${avatar_url}" ]] && [[ -z "${modify}" ]] && _avatar=", \"avatar_url\": \"${avatar_url}\""
399+
361400
[[ -n "${embedding}" ]] && _embed="$(build_embed)"
362401

363402
local _prefix="\"wait\": true${_tts}${_content}${_username}${_avatar}${_embed}"
@@ -378,6 +417,17 @@ send()
378417
# dry run?
379418
[[ -n "${is_dry}" ]] && [[ "${is_dry}" -ne 0 ]] && echo "${1}" && exit 0;
380419

420+
# If any arguments except for username, modify and avatar are specified (modify option required), then exit with an error
421+
if [[ -n "${username}" ]] || [[ -n "${avatar_url}" ]] && [[ "${modify}" = 1 ]]; then
422+
if [[ -n "${text}" ]] && [[ -n "${embed_title}" ]] && [[ -n "${embed_description}" ]] && [[ -n "${embed_url}" ]] && [[ -n "${embed_color}" ]] && [[ -n "${embed_timestamp}" ]] && [[ -n "${embed_authorurl}" ]] && [[ -n "${embed_authoricon}" ]] && [[ -n "${embed_authorname}" ]] && [[ -n "${embed_thumbnail}" ]] && [[ -n "${embed_imageheight}" ]] && [[ -n "${embed_imagewidth}" ]] && [[ -n "${embed_imageurl}" ]] && [[ -n "${embed_footertext}" ]] && [[ -n "${embed_footericon}" ]] && [[ -n "${fields}" ]] && [[ -n "${file_path}" ]]; then
423+
echo "fatal: no arguments specified (except for username, modify and avatar)"; exit 1;
424+
fi
425+
_result=$(curl -H "Content-Type: application/json" -H "Expect: application/json" -X PATCH "${webhook_url}" -d "${_sendme}" 2>/dev/null)
426+
send_ok=$?
427+
[[ "${send_ok}" -ne 0 ]] && echo "fatal: curl failed with code ${send_ok}" && exit $send_ok
428+
exit 0;
429+
fi
430+
381431
# make the POST request and parse the results
382432
# results should be empty if there's no problem. otherwise, there should be code and message
383433
local _result

tests/04.modify.bats

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bats
2+
3+
load pre
4+
5+
# Modify without username or avatar
6+
@test "modify: --modify (should fail)" {
7+
run bash discord.sh --modify
8+
[ "${lines[0]}" = "fatal: must pass --username or --avatar with --modify" ]
9+
[ "$status" -eq 1 ]
10+
}
11+
# Modify with username
12+
@test "modify: --modify --username --gnu-style <>" {
13+
run bash discord.sh --modify --username "Modify with username"
14+
[ "$status" -eq 0 ]
15+
}
16+
@test "modify: --modify --username --gnu-style=<>" {
17+
run bash discord.sh --modify --username="Modify with username"
18+
[ "$status" -eq 0 ]
19+
}
20+
# Modify with avatar
21+
@test "modify: --modify --avatar --gnu-style <>" {
22+
run bash discord.sh --modify --avatar "https://i.imgur.com/12jyR5Q.png"
23+
[ "$status" -eq 0 ]
24+
}
25+
@test "modify: --modify --avatar --gnu-style=<>" {
26+
run bash discord.sh --modify --avatar="https://i.imgur.com/12jyR5Q.png"
27+
[ "$status" -eq 0 ]
28+
}
29+
# Modify with username and avatar
30+
@test "modify: --modify --username --avatar --gnu-style <>" {
31+
run bash discord.sh --modify --username "Modify with username and avatar" --avatar "https://i.imgur.com/12jyR5Q.png"
32+
[ "$status" -eq 0 ]
33+
}
34+
@test "modify: --modify --username --avatar --gnu-style=<>" {
35+
run bash discord.sh --modify --username "Modify with username and avatar" --avatar "https://i.imgur.com/12jyR5Q.png"
36+
[ "$status" -eq 0 ]
37+
}
38+
@test "modify: --modify --username --avatar --gnu-style varied" {
39+
run bash discord.sh --modify --username "Modify with username and avatar" --avatar="https://i.imgur.com/12jyR5Q.png"
40+
[ "$status" -eq 0 ]
41+
}

0 commit comments

Comments
 (0)