-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostdiscord
executable file
·109 lines (98 loc) · 3.34 KB
/
postdiscord
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
#!/usr/bin/env bash
# simple script to interact with discord using a webhook
# simple and rich post sending to a single discord channel
# (channel set in 'webhooks' in server settings)
#
# author: Little Tinker/Thom Cuddihy (https://github.com/thomcuddihy)
# MIT License 2017
HELP="
arguments:
T: Webhook token
G: Guild code
t: text to post (appears above rich message if used)
r: rich message
c: colour of rich message (as ascii integer or: good|warning|danger) (optional)
H: rich text header (title) (optional)
l: rich text header url link (optional)
a: avatar icon url (optional)
u: username to post as (optional)
h: help
notes:
Guild code and webhook token can be extracted from the webhook url:
https://discordapp.com/api/webhooks/{{ guild code }}/{{ token }}
One or both of '-t' and '-r' are required, all else optional
Webhook token and guild code can be omitted and set in environment instead:
export DISCORD_GUILD=...
export DISCORD_TOKEN=...
You can mention users/roles in messages using their id codes:
Enter '\@Something' in discord chat to get their id code
Then use in messages as '<@1234567890>'
'-c' takes either an ascii rgb integer or defined words
Ascii rgb integers are weird. blame discord for not using hex strings
Use this to convert: https://www.shodor.org/stella2java/rgbint.html
examples:
./postdiscord -t 'boop' -r 'I'm a rich embed!' -c 'good' -H 'test script' -u 'Feldb0t'
./postdiscord -t 'boop' -r 'I'm a rich embed with a custom colour!' -c '16738740' -H 'test script'
./postdiscord -t 'I'm a plain message' -u 'AppleBot'
./postdiscord -t 'Hi <@1234567890>' -r 'boop' -c 'warning' -H 'Mention example' -u 'Sweetie Bot'
"
## Check if null arguments
if [ $# -eq 0 ]; then
echo "$HELP"
exit
fi
#Set vars
URL_BASE="https://discordapp.com/api/webhooks"
COLOUR="8421504"
## Load var from environment:
GUILD=$DISCORD_GUILD
TOKEN=$DISCORD_TOKEN
## Setup vars from arguments
while getopts 'T:G:t:r:c:H:l:a:u:h' opt; do
case $opt in
T) TOKEN="$OPTARG" ;;
G) GUILD="$OPTARG" ;;
t) TEXT="$OPTARG" ;;
r) RICH="$OPTARG" ;;
c) COLOUR="$OPTARG" ;;
H) TITLE="$OPTARG" ;;
l) TITLE_URL="$OPTARG" ;;
a) AUTHOR_ICON="$OPTARG" ;;
u) USERNAME="$OPTARG" ;;
h) echo "$HELP"
exit ;;
*) echo "$HELP" >&2
exit 1 ;;
esac
done
## colour strings...
if [ ${COLOUR} = "good" ]; then COLOUR="65280"; fi
if [ ${COLOUR} = "warning" ]; then COLOUR="255"; fi
if [ ${COLOUR} = "danger" ]; then COLOUR="16711680"; fi
if [ ${COLOUR} = "pinkie" ]; then COLOUR="16738740"; fi
## post url
POST_URL=${URL_BASE}/${GUILD}/${TOKEN}
## Rich attachment post
if [[ -n ${RICH} ]]; then
curl -X POST ${POST_URL} \
--header "Content-Type: application/json" -d @- << EOF
{"content":"${TEXT}",
"username":"${USERNAME}",
"avatar_url":"${AUTHOR_ICON}",
"embeds":[{
"title": "${TITLE}",
"description": "${RICH}",
"color": "${COLOUR}",
"url": "${TITLE_URL}"
}]}
EOF
## Normal post
else
curl -X POST ${POST_URL} \
--header "Content-Type: application/json" -d @- << EOF
{"content":"${TEXT}",
"username":"${USERNAME}",
"avatar_url":"${AUTHOR_ICON}"
}
EOF
fi