Skip to content
53 changes: 53 additions & 0 deletions modules/message.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 '"')
Expand Down Expand Up @@ -509,6 +514,54 @@ 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
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"
else
log_comment_and_exit1 "Error: You do not have an authorized access token"
fi
;;
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 -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
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
esac
Expand Down