From dce66714d8bc4afb568a847280a7d0a7e6d84ba7 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Sat, 27 Mar 2021 00:51:37 +0300 Subject: [PATCH 01/10] adding the first draft of the discord feature --- modules/message.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/modules/message.sh b/modules/message.sh index 62f742250c..cc818478e9 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -15,6 +15,11 @@ function message { echo "Your API access token is $access_token" return 0 } + function get_server_discord { + server_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds) + server_names=$(echo $server_info | python -m json.tool | jq '.[].name' | tr -d '"') + echo "$server_names" + } function get_channel_gitter { channel_info=$(curl -s -H "Accept: application/json" -H "Authorization: Bearer $access_token" "https://api.gitter.im/v1/rooms") channel_names=$(echo $channel_info | python -m json.tool | jq '.[].name' | tr -d '"') @@ -509,6 +514,37 @@ function message { echo "you have successfully authorized and your access token is $access_token " fi ;; + servers) + if check_apitoken discord; then + server_names=$(get_server_discord) + echo "Server Names:" + echo "$server_names" + else + log_comment_and_exit1 "Error: You do not have an authorized access token" + fi + ;; + channels) + if check_apitoken discord; then + server_name=$3 + channel_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') + channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${channel_id}/channels) + channel_names=$(echo $channel_info | python -m json.tool | jq '.[].name' | tr -d '"') + echo "Channel Names:" + echo "$channel_names" + else + log_comment_and_exit1 "Error: You do not have an authorized access token" + fi + ;; + send) + if check_apitoken discord; then + server_name=$3 + + else + log_comment_and_exit1 "Error: You do not have an authorized access token" + fi + ;; + read) + ;; *) log_help_and_exit1 "Error: This command does not exist" message esac From 3a498c3ef8e6c1aef200b1ff32fc0f0b757ddb34 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Sat, 27 Mar 2021 01:57:03 +0300 Subject: [PATCH 02/10] added servers and channels subcommand --- modules/message.sh | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/message.sh b/modules/message.sh index cc818478e9..72190e6374 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -526,8 +526,8 @@ function message { channels) if check_apitoken discord; then server_name=$3 - channel_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') - channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${channel_id}/channels) + server_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') + channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${server_id}/channels) channel_names=$(echo $channel_info | python -m json.tool | jq '.[].name' | tr -d '"') echo "Channel Names:" echo "$channel_names" @@ -535,15 +535,20 @@ function message { log_comment_and_exit1 "Error: You do not have an authorized access token" fi ;; - send) + read) if check_apitoken discord; then server_name=$3 - + channel_name=$4 + server_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') + channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${server_id}/channels) + channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${channel_name}\")" | jq .id | tr -d '"') + channel_messages=$(curl -s -X POST -H "Authorization: $TOKEN" -H "Content-Type: application/json" -d '{"content": "sent through vs"}' https://discordapp.com/api/channels/${channel_id}/messages) + echo $channel_messages else log_comment_and_exit1 "Error: You do not have an authorized access token" fi ;; - read) + send) ;; *) log_help_and_exit1 "Error: This command does not exist" message From b0009195281e8f963a367c34e8cf4f2c74ad3ae2 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Sat, 27 Mar 2021 03:51:30 +0300 Subject: [PATCH 03/10] update message.sh --- modules/message.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/message.sh b/modules/message.sh index 72190e6374..868c445fb5 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -542,8 +542,8 @@ function message { server_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${server_id}/channels) channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${channel_name}\")" | jq .id | tr -d '"') - channel_messages=$(curl -s -X POST -H "Authorization: $TOKEN" -H "Content-Type: application/json" -d '{"content": "sent through vs"}' https://discordapp.com/api/channels/${channel_id}/messages) - echo $channel_messages + channel_messages=$(curl -s -H "Authorization: $access_token" -H "Content-Type: application/json" https://discordapp.com/api/channels/${channel_id}/messages | jq '.[].content') + echo "$channel_messages" else log_comment_and_exit1 "Error: You do not have an authorized access token" fi From 7ab03dd2dde2b20e92649455c26a955519aefc14 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Sat, 27 Mar 2021 06:40:52 +0300 Subject: [PATCH 04/10] added channels,server,read,send features --- modules/message.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/modules/message.sh b/modules/message.sh index 868c445fb5..8772772045 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -549,6 +549,18 @@ function message { fi ;; send) + if check_apitoken discord; then + server_name=$3 + channel_name=$4 + message=$5 + server_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') + channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${server_id}/channels) + channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${channel_name}\")" | jq .id | tr -d '"') + send_messages=$(curl -s -X POST -H "Authorization: $access_token" -H "Content-Type: application/json" -d "{\"content\": \"${message}\"}" https://discordapp.com/api/channels/${channel_id}/messages | jq .) + echo "$send_messages" + else + log_comment_and_exit1 "Error: You do not have an authorized access token" + fi ;; *) log_help_and_exit1 "Error: This command does not exist" message From 20cabab1edad9893fc35389edb9425f03e0ac244 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Mon, 29 Mar 2021 22:40:30 +0300 Subject: [PATCH 05/10] added features --- README.md | 2 ++ _treehouses | 4 ++++ modules/help.sh | 2 ++ modules/message.sh | 14 ++++++++++++++ 4 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 21b732a74f..3de4194c6b 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,8 @@ message sends message to chat service sets api/channel info in config file sends/recieves messages in slack sets api/channel info in config file + list servers/channels in discord + reads/sends messages in discord shutdown [now|in|force] shutdown the system ``` diff --git a/_treehouses b/_treehouses index 38d8a2feb9..abd0842339 100644 --- a/_treehouses +++ b/_treehouses @@ -235,6 +235,10 @@ treehouses memory used gb treehouses memory used mb treehouses message discord apitoken treehouses message discord authorize +treehouses message discord channels +treehouses message discord read +treehouses message discord send +treehouses message discord servers treehouses message gitter apitoken treehouses message gitter authorize treehouses message gitter mark diff --git a/modules/help.sh b/modules/help.sh index f3ee201f0e..6ca3dbe80d 100644 --- a/modules/help.sh +++ b/modules/help.sh @@ -149,6 +149,8 @@ message sends message to chat service sets api/channel info in config file sends/recieves messages in slack sets api/channel info in config file + list servers/channels in discord + reads/sends messages in discord shutdown [now|in|force] shutdown the system EOF echo "$helpdefault" diff --git a/modules/message.sh b/modules/message.sh index 8772772045..b6c0820e71 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -640,4 +640,18 @@ function message_help { echo " $BASENAME message discord authorize \"1234567890\"" echo " Sets and saves API token" echo + echo " $BASENAME message discord server" + echo " List all servers the user is in" + echo + echo " $BASENAME message discord channels \"server name\"" + echo " List all channels in the server the user specified" + echo + echo " $BASENAME message discord read \"server name\" \"channel name\"" + echo " Reads messages using server and channel name" + echo + echo " $BASENAME message discord send \"server name\" \"channel name\" \"message\"" + echo " Sends a message using server and channel name" + echo +} + } From 9b89f980446db9f88a71d2a7e4a2199c3e2a14c0 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Mon, 29 Mar 2021 22:45:14 +0300 Subject: [PATCH 06/10] removed extra bracket --- modules/message.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/message.sh b/modules/message.sh index b6c0820e71..1b560ffe77 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -653,5 +653,3 @@ function message_help { echo " Sends a message using server and channel name" echo } - -} From a43fd961b63a46d2b14b40cb6e3a9148d3645d64 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Mon, 29 Mar 2021 22:58:13 +0300 Subject: [PATCH 07/10] fixed send unnecessary echo json --- modules/message.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/message.sh b/modules/message.sh index 1b560ffe77..044458157b 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -556,8 +556,12 @@ function message { server_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${server_id}/channels) channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${channel_name}\")" | jq .id | tr -d '"') - send_messages=$(curl -s -X POST -H "Authorization: $access_token" -H "Content-Type: application/json" -d "{\"content\": \"${message}\"}" https://discordapp.com/api/channels/${channel_id}/messages | jq .) - echo "$send_messages" + message_response=$(curl -s -X POST -H "Authorization: $access_token" -H "Content-Type: application/json" -d "{\"content\": \"${message}\"}" https://discordapp.com/api/channels/${channel_id}/messages | python -m json.tool | jq '.code' | tr -d '"') + if [[ $message_response == 0 ]]; then + log_comment_and_exit1 "Error: message not delivered" + else + echo "You successfully send a message to Discord" + fi else log_comment_and_exit1 "Error: You do not have an authorized access token" fi From b4dd34e1fed613244a43e5a071c08e25c7d7aa31 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Mon, 29 Mar 2021 23:07:46 +0300 Subject: [PATCH 08/10] fixed code climate array issue --- modules/message.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/message.sh b/modules/message.sh index 044458157b..4bb1d6c7e7 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -538,10 +538,10 @@ function message { read) if check_apitoken discord; then server_name=$3 - channel_name=$4 + discord_channel=$4 server_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${server_id}/channels) - channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${channel_name}\")" | jq .id | tr -d '"') + channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${discord_channel}\")" | jq .id | tr -d '"') channel_messages=$(curl -s -H "Authorization: $access_token" -H "Content-Type: application/json" https://discordapp.com/api/channels/${channel_id}/messages | jq '.[].content') echo "$channel_messages" else @@ -551,11 +551,11 @@ function message { send) if check_apitoken discord; then server_name=$3 - channel_name=$4 + discord_channel=$4 message=$5 server_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${server_id}/channels) - channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${channel_name}\")" | jq .id | tr -d '"') + channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${discord_channel}\")" | jq .id | tr -d '"') message_response=$(curl -s -X POST -H "Authorization: $access_token" -H "Content-Type: application/json" -d "{\"content\": \"${message}\"}" https://discordapp.com/api/channels/${channel_id}/messages | python -m json.tool | jq '.code' | tr -d '"') if [[ $message_response == 0 ]]; then log_comment_and_exit1 "Error: message not delivered" From 1048a1a67d16c685db073004f15e9b6704706a9b Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Mon, 29 Mar 2021 23:25:27 +0300 Subject: [PATCH 09/10] added error handling for blank server (fixes #2152) --- modules/message.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/message.sh b/modules/message.sh index 4bb1d6c7e7..c6eea387f3 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -526,6 +526,9 @@ function message { channels) if check_apitoken discord; then server_name=$3 + if [[ $server_name == "" ]]; then + log_comment_and_exit1 "ERROR: Channel information is missing" "usage: $BASENAME message discord channels \"server name\"" + fi server_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${server_id}/channels) channel_names=$(echo $channel_info | python -m json.tool | jq '.[].name' | tr -d '"') From 645436993af071c4d79c7547b75a13346bae5f0d Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Tue, 30 Mar 2021 00:37:07 +0300 Subject: [PATCH 10/10] removed unnessary channels (fixes #2152) --- modules/message.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/message.sh b/modules/message.sh index c6eea387f3..b466a1dc57 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -531,7 +531,8 @@ function message { fi server_id=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') channel_info=$(curl -s -H "Authorization: $access_token" https://discordapp.com/api/guilds/${server_id}/channels) - channel_names=$(echo $channel_info | python -m json.tool | jq '.[].name' | tr -d '"') + channel_type=$(echo "$channel_info" | jq ".[] | select(.type==0)") + channel_names=$(echo "$channel_type" | jq '.name' | tr -d '"') echo "Channel Names:" echo "$channel_names" else