From 059f4bc081b52fe38b462cd739376046142e1894 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Thu, 1 Apr 2021 05:58:02 +0300 Subject: [PATCH 1/9] new discord subcommands (fixes #2154) --- README.md | 2 + _treehouses | 4 ++ modules/help.sh | 2 + modules/message.sh | 140 +++++++++++++++++++++++++++++++++------------ 4 files changed, 112 insertions(+), 36 deletions(-) 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 62f742250c..dbac92b15e 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -467,48 +467,104 @@ function message { discord) case "$2" in apitoken) + if [[ $3 == "" ]]; then + echo "bot token is missing" + else + access_token=$3 + conf_var_update "discord_apitoken" "$access_token" + echo "you have successfully added your bot token and your bot token is $access_token " + fi + ;; + authorize) if check_apitoken discord; then - get_apitoken discord - elif [[ $3 != "" ]] && [[ $4 != "" ]]; then - client_id=$3 - if [[ $4 == http?(s)://* ]]; then - redirect_uri=$4 + if [[ $3 != "" ]]; then + client_id=$3 + conf_var_update "discord_clientid" "$client_id" + authorization_url=$(curl -Ls -o /dev/null -w %'{'url_effective'}' https://discord.com/oauth2/authorize?client_id=${client_id}\&scope=bot\&permissions=671165489) + echo "To authorize your bot, navigate to" + echo + echo "$authorization_url" + echo + echo "Then, click \"Continue\" to provide the permissions for your bot" + echo else - log_and_exit1 "Invalid URL" + echo "You did not provide a client id" + echo "" + echo "To get a client id" + echo "" + echo "Navigate to https://discord.com/developers/applications. Navigate to your APP that contains your bot." + echo + echo "Click on the \"Oauth\" tab, then copy the client id by clicking on the blue \"Copy\" button under \"CLIENT ID\"" fi - conf_var_update "discord_clientid" "$client_id" - conf_var_update "discord_redirecturl" "$redirect_uri" - authorization_url=$(curl -Ls -o /dev/null -w %'{'url_effective'}' https://discord.com/api/oauth2/authorize?response_type=token\&client_id=${client_id}\&scope=identify) - echo "To get the access token, navigate to" - echo - echo "$authorization_url" - echo - echo "Then, click \"Authorize\" to provide permissions for your app" - echo "From the redirected link , you will get your access token for discord" - echo - echo "For example, if redirected link is \"http://localhost/token_type=Bearer&access_token=1234567890&expires_in=604800&scope=identify\"' then the access token is \"1234567890\"" - echo "Then, run $BASENAME message discord authorize " else - echo "You do not have an authorized access token" + echo "You do not have an authorized bot token" echo "" - echo "To get an authorized access token" + echo "To get an authorized bot token" echo "" echo "Navigate to https://discord.com/developers/applications. Create an APP by clicking \"New Application\" and provide a suitable name for your APP." - echo "Then move to the option \"OAuth2\" below \"General Information\" and add a redirect URL ( For eg: http://localhost/ ) by clicking \"Add Redirect\" below \"Redirects\"." - echo "Then, save the settings by pressing green save button at the bottom of the page." - echo "Note both the CLIENT ID and your Redirect URL" - echo "Run $BASENAME message discord apitoken " + echo "Then move to the option \"Bot\" below \"General Information\" and add a bot by clicking on the \"Add Bot\" button." + echo "Confirm Yes on the dialog box and click on the \"Copy\" button below the \"TOKEN\" label." + echo "Run $BASENAME message discord apitoken " fi - ;; - authorize) - if [[ $3 == "" ]]; then - echo "authorization code is missing" + ;; + servers) + if check_apitoken discord; then + server_info=$(curl -s -H "Authorization: Bot $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" else - access_token=$3 - conf_var_update "discord_apitoken" "$access_token" - echo "you have successfully authorized and your access token is $access_token " + log_comment_and_exit1 "Error: You do not have an authorized bot token" fi - ;; + ;; + 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: Bot $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') + channel_info=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/guilds/${server_id}/channels) + 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 + log_comment_and_exit1 "Error: You do not have an authorized bot token" + fi + ;; + read) + if check_apitoken discord; then + server_name=$3 + discord_channel=$4 + server_id=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') + channel_info=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/guilds/${server_id}/channels) + channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${discord_channel}\")" | jq .id | tr -d '"') + channel_messages=$(curl -s -H "Authorization: Bot $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 bot token" + fi + ;; + send) + if check_apitoken discord; then + server_name=$3 + discord_channel=$4 + message=$5 + server_id=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') + channel_info=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/guilds/${server_id}/channels) + channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${discord_channel}\")" | jq .id | tr -d '"') + webhook_id=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/guilds/${server_id}/webhooks | jq ".[] | select(.channel_id==\"${channel_id}\")" | jq .id | tr -d '"') + webhook_token=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/guilds/${server_id}/webhooks | jq ".[] | select(.channel_id==\"${channel_id}\")" | jq .token | tr -d '"') + message_response=$(curl -s -X POST -H "Content-Type: application/json" -d "{\"content\": \"${message}\"}" "https://discord.com/api/webhooks/${webhook_id}/${webhook_token}" | jq '.code' | tr -d '"') + if [[ $message_response == 0 ]]; then + log_comment_and_exit1 "Error: message not delivered" + else + echo "You successfully sent a message to Discord" + fi + else + log_comment_and_exit1 "Error: You do not have an authorized bot token" + fi + ;; *) log_help_and_exit1 "Error: This command does not exist" message esac @@ -581,10 +637,22 @@ function message_help { echo " $BASENAME message slack mark \"channel ID\"" echo " Marks messages of a slack channel using channel ID" echo - echo " $BASENAME message discord apitoken" - echo " Check for API token for discord" + echo " $BASENAME message discord apitoken \"bot token\"" + echo " Adds the bot token for discord" echo - echo " $BASENAME message discord authorize \"1234567890\"" - echo " Sets and saves API token" + echo " $BASENAME message discord authorize \"client id\"" + echo " Authorize the discord bot to communicate with the client" + 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 a32a20f039ef97e5141055ea4325ae3d93482dc7 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Mon, 5 Apr 2021 23:17:39 +0300 Subject: [PATCH 2/9] added websocket --- modules/message.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/message.sh b/modules/message.sh index dbac92b15e..7b0a347abb 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -507,6 +507,15 @@ function message { echo "Run $BASENAME message discord apitoken " fi ;; + ws) + if check_apitoken discord; then + echo '{"op":2,"d":{"token":"'"$TOKEN"'",' \ + '"properties":{"$os":"linux","$browser":"treehouses","$device":"RaspberryPI"},' \ + '"compress":false,"large_threshold":250}}' + | wscat --connect wss://gateway.discord.gg/ + else + log_comment_and_exit1 "Error: You do not have an authorized bot token" + fi servers) if check_apitoken discord; then server_info=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/users/@me/guilds) From a169284002c15c20ba520130cc102449db612f89 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Tue, 6 Apr 2021 00:55:12 +0300 Subject: [PATCH 3/9] update message.sh (fixes #2154) --- modules/message.sh | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/modules/message.sh b/modules/message.sh index 7b0a347abb..3df0a60fe2 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -1,4 +1,5 @@ function message { + check_missing_binary "wscat" "wscat not installed, please use: npm install -g wscat" chats="$1" function check_apitoken { channelname=$1_apitoken @@ -509,13 +510,16 @@ function message { ;; ws) if check_apitoken discord; then - echo '{"op":2,"d":{"token":"'"$TOKEN"'",' \ - '"properties":{"$os":"linux","$browser":"treehouses","$device":"RaspberryPI"},' \ - '"compress":false,"large_threshold":250}}' - | wscat --connect wss://gateway.discord.gg/ + { + sleep 1 + echo '{"op":2,"d":{"token":"'"$access_token"'",' \ + '"properties":{"$os":"linux","$browser":"treehouses","$device":"RaspberryPI"},' \ + '"compress":false,"large_threshold":250}}' + } | wscat --connect wss://gateway.discord.gg/ else log_comment_and_exit1 "Error: You do not have an authorized bot token" fi + ;; servers) if check_apitoken discord; then server_info=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/users/@me/guilds) @@ -562,18 +566,16 @@ function message { server_id=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') channel_info=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/guilds/${server_id}/channels) channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${discord_channel}\")" | jq .id | tr -d '"') - webhook_id=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/guilds/${server_id}/webhooks | jq ".[] | select(.channel_id==\"${channel_id}\")" | jq .id | tr -d '"') - webhook_token=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/guilds/${server_id}/webhooks | jq ".[] | select(.channel_id==\"${channel_id}\")" | jq .token | tr -d '"') - message_response=$(curl -s -X POST -H "Content-Type: application/json" -d "{\"content\": \"${message}\"}" "https://discord.com/api/webhooks/${webhook_id}/${webhook_token}" | jq '.code' | tr -d '"') + message_response=$(curl -s -X POST -H "Authorization: Bot $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 sent a message to Discord" fi else - log_comment_and_exit1 "Error: You do not have an authorized bot token" + 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 esac From 23fc6da67a889a208037bd909a617d5a09cf1f9b Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Thu, 8 Apr 2021 03:18:10 +0300 Subject: [PATCH 4/9] switched wscat to wsdump (fixes #2154) --- modules/message.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/message.sh b/modules/message.sh index 3df0a60fe2..ed6a7476d6 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -1,5 +1,4 @@ function message { - check_missing_binary "wscat" "wscat not installed, please use: npm install -g wscat" chats="$1" function check_apitoken { channelname=$1_apitoken @@ -515,7 +514,7 @@ function message { echo '{"op":2,"d":{"token":"'"$access_token"'",' \ '"properties":{"$os":"linux","$browser":"treehouses","$device":"RaspberryPI"},' \ '"compress":false,"large_threshold":250}}' - } | wscat --connect wss://gateway.discord.gg/ + } | wsdump.py -r wss://gateway.discord.gg/ else log_comment_and_exit1 "Error: You do not have an authorized bot token" fi From f978678d3296906ab1240f98cc6a9ba469f5d38f Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Thu, 8 Apr 2021 03:25:15 +0300 Subject: [PATCH 5/9] corrected codeclimate error (fixes #2154) --- modules/message.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/message.sh b/modules/message.sh index ed6a7476d6..86ce8dbceb 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -512,7 +512,7 @@ function message { { sleep 1 echo '{"op":2,"d":{"token":"'"$access_token"'",' \ - '"properties":{"$os":"linux","$browser":"treehouses","$device":"RaspberryPI"},' \ + "properties":{\"$os\":\"linux\",\"$browser\":\"treehouses\",\"$device\":\"RaspberryPI\"}, \ # The properties are not variables!! '"compress":false,"large_threshold":250}}' } | wsdump.py -r wss://gateway.discord.gg/ else From c1f17a439f7047feaebd7ec920d31227e6637a11 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Thu, 8 Apr 2021 06:43:15 +0300 Subject: [PATCH 6/9] moved websocket to send command (fixes #2154) --- modules/message.sh | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/modules/message.sh b/modules/message.sh index 86ce8dbceb..2d34230d6b 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -507,18 +507,6 @@ function message { echo "Run $BASENAME message discord apitoken " fi ;; - ws) - if check_apitoken discord; then - { - sleep 1 - echo '{"op":2,"d":{"token":"'"$access_token"'",' \ - "properties":{\"$os\":\"linux\",\"$browser\":\"treehouses\",\"$device\":\"RaspberryPI\"}, \ # The properties are not variables!! - '"compress":false,"large_threshold":250}}' - } | wsdump.py -r wss://gateway.discord.gg/ - else - log_comment_and_exit1 "Error: You do not have an authorized bot token" - fi - ;; servers) if check_apitoken discord; then server_info=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/users/@me/guilds) @@ -562,6 +550,12 @@ function message { server_name=$3 discord_channel=$4 message=$5 + { + # The properties are not variables!! + sleep 1 + echo '{"op":2,"d":{"token":"'"$access_token"'",' \ + "properties":{\"$os\":\"linux\",\"$browser\":\"treehouses\",\"$device\":\"RaspberryPI\"}, '"compress":false,"large_threshold":250}}' + } | wsdump.py -r wss://gateway.discord.gg/ > /dev/null server_id=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/users/@me/guilds | jq ".[] | select(.name==\"${server_name}\")" | jq .id | tr -d '"') channel_info=$(curl -s -H "Authorization: Bot $access_token" https://discordapp.com/api/guilds/${server_id}/channels) channel_id=$(echo $channel_info | jq ".[] | select(.name==\"${discord_channel}\")" | jq .id | tr -d '"') From b5ef62fb73b6a302a707dbf06c6c048fa92843a5 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Thu, 8 Apr 2021 07:03:01 +0300 Subject: [PATCH 7/9] update message.sh (fixes #2154) --- modules/message.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/message.sh b/modules/message.sh index 2d34230d6b..f187335da5 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -468,7 +468,7 @@ function message { case "$2" in apitoken) if [[ $3 == "" ]]; then - echo "bot token is missing" + get_apitoken discord else access_token=$3 conf_var_update "discord_apitoken" "$access_token" From 11130d4a6b5ccd300a854b0fa0e039cfa7264189 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Sat, 10 Apr 2021 03:21:14 +0300 Subject: [PATCH 8/9] update message.sh (fixes #2154) --- modules/message.sh | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/message.sh b/modules/message.sh index f187335da5..0505a984c7 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -468,9 +468,20 @@ function message { case "$2" in apitoken) if [[ $3 == "" ]]; then - get_apitoken discord + if check_apitoken ; then + get_apitoken discord + else + echo "You do not have an authorized bot token" + echo "" + echo "To get an authorized bot token" + echo "" + echo "Navigate to https://discord.com/developers/applications. Create an APP by clicking \"New Application\" and provide a suitable name for your APP." + echo "Then move to the option \"Bot\" below \"General Information\" and add a bot by clicking on the \"Add Bot\" button." + echo "Confirm Yes on the dialog box and click on the \"Copy\" button below the \"TOKEN\" label." + echo "Run $BASENAME message discord apitoken " + fi else - access_token=$3 + access_token=$3 conf_var_update "discord_apitoken" "$access_token" echo "you have successfully added your bot token and your bot token is $access_token " fi @@ -551,7 +562,7 @@ function message { discord_channel=$4 message=$5 { - # The properties are not variables!! + # The properties(excluding acces_token) are not variables!! sleep 1 echo '{"op":2,"d":{"token":"'"$access_token"'",' \ "properties":{\"$os\":\"linux\",\"$browser\":\"treehouses\",\"$device\":\"RaspberryPI\"}, '"compress":false,"large_threshold":250}}' From 0cc46b051612963c9250fec4bf00b43f0744aba6 Mon Sep 17 00:00:00 2001 From: rjpadilla Date: Sat, 10 Apr 2021 03:31:03 +0300 Subject: [PATCH 9/9] update message.sh (fixes #2154) --- modules/message.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/message.sh b/modules/message.sh index 0505a984c7..b2a89e7194 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -658,7 +658,7 @@ function message_help { echo " $BASENAME message discord authorize \"client id\"" echo " Authorize the discord bot to communicate with the client" echo - echo " $BASENAME message discord server" + echo " $BASENAME message discord servers" echo " List all servers the user is in" echo echo " $BASENAME message discord channels \"server name\""