diff --git a/README.md b/README.md index 21b732a74..3de4194c6 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 38d8a2feb..abd084233 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 f3ee201f0..6ca3dbe80 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 62f742250..b466a1dc5 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,62 @@ 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 + 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_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 access token" + fi + ;; + read) + if check_apitoken discord; then + server_name=$3 + 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==\"${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 + log_comment_and_exit1 "Error: You do not have an authorized access token" + fi + ;; + send) + if check_apitoken discord; then + server_name=$3 + 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==\"${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" + 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 + ;; *) log_help_and_exit1 "Error: This command does not exist" message esac @@ -587,4 +648,16 @@ 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 }