diff --git a/README.md b/README.md index c77ec2a..2f9d676 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,15 @@ cd discord-twitter-webhook ### Configure Edit the `config.example.json` and rename it to `config.json` with the given information below: -* `webhook_url`: The Discord webhook url, for more information check out [this](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) page. -* `webhook_name`: The username of your Discord webhook. -* `webhook_avatar`: The avatar of your Discord webhook. -* `webhook_message`: The message before your Twitter link. Formatted as `{message} {tweet_link}`, no newline added. -* `api_key`: The Twitter API key, if you do not have a Twitter developer account then check out [this](https://developer.twitter.com/en) page. -* `api_secret`: The Twitter API secret, requires a Twitter developer account. -* `access_token`: The Twitter account access token, requires a Twitter developer account. -* `access_secret`: The Twitter account acesss secret, requires a Twitter developer account. -* `user_id`: The user ID for the Twitter account you want notifications from, to find a Twitter id use [this](https://tweeterid.com/) tool. +* `discord.webhookUrl`: The Discord webhook url, for more information check out [this](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) page. +* `discord.webhookName`: The username of your Discord webhook. +* `discord.webhookAvatar`: The avatar of your Discord webhook. +* `discord.webhookMessage`: The message before your Twitter link. Formatted as `{message} {tweet_link}`, no newline added. +* `twitter.apiKey`: The Twitter API key, if you do not have a Twitter developer account then check out [this](https://developer.twitter.com/en) page. +* `twitter.apiSecret`: The Twitter API secret, requires a Twitter developer account. +* `twitter.accessToken`: The Twitter account access token, requires a Twitter developer account. +* `twitter.accessSecret`: The Twitter account acesss secret, requires a Twitter developer account. +* `followingUserId`: The user ID for the Twitter account you want notifications from, to find a Twitter id use [this](https://tweeterid.com/) tool. ### Install Dependencies > You will need Node.js installed to do this and the next section! If you don't have it installed, you can install it from [here](https://nodejs.org/en/). diff --git a/config.example.json b/config.example.json index fe0bc09..7d84320 100644 --- a/config.example.json +++ b/config.example.json @@ -1,11 +1,15 @@ { - "webhook_url": "", - "webhook_name": "", - "webhook_avatar": "", - "webhook_message": "", - "api_key": "", - "api_secret": "", - "access_token": "", - "acesss_secret": "", - "user_id": "" + "discord": { + "webhookUrl": "", + "webhookName": "", + "webhookAvatar": "", + "webhookMessage": "" + }, + "twitter": { + "apiKey": "", + "apiSecret": "", + "accessToken": "", + "acessSecret": "" + }, + "followingUserId": "" } diff --git a/main.js b/main.js index 44d692a..27c0a29 100644 --- a/main.js +++ b/main.js @@ -3,48 +3,41 @@ import { Webhook } from "discord-webhook-node"; const config = require("config.json"); -const wClient = new Webhook(config.webhook_url); -wClient.setAvatar(config.webhook_avatar); -wClient.setUsername(config.webhook_name); +const dClient = new Webhook(config.discord.webhookUrl); +dClient.setUsername(config.discord.webhookName); +dClient.setAvatar(config.discord.webhookAvatar); const tClient = new Twitter({ - consumer_key: config.api_key, - consumer_secret: config.api_secret, - access_token: config.access_token, - access_token_secret: config.acesss_secret, + consumer_key: config.twitter.apiKey, + consumer_secret: config.twitter.apiSecret, + access_token: config.twitter.accessToken, + access_token_secret: config.twitter.acessSecret, }); tClient.get("account/verify_credentials", { skip_status: true }) .catch(err => - console.log(`Unable to login due to the following error:\n${err}`) + console.error(`Unable to login due to the following error:\n${err}`) ) .then(res => - console.log(`Logged in as '${res.data.name}'!`) + console.info(`Logged in as '${res.data.name}'!`) ); -let stream = tClient.stream("statuses/filter", { follow: config.user_id }); +console.info(`Fetching tweets from ID '${config.followingUserId}'.`); -console.log(`Fetching tweets from id '${config.user_id}'.`); +tClient.stream("statuses/filter", { follow: config.followingUserId }) + .on("tweet", twt => { + if (twt.retweeted_status || twt.in_reply_to_status_id || twt.is_quote_status) return; -stream.on("connected", () => - console.log("Successfully connected to Twitter!") -); + let twitterLink = `https://twitter.com/${twt.user.screen_name}/status/${twt.id_str}`; + dClient.send(`${config.discord.webhookMessage} ${twitterLink}`); + }) -stream.on("tweet", twt => { - if(twt.retweeted_status || twt.in_reply_to_status_id || twt.is_quote_status) return; + .on("connected", () => console.info("Connected to Twitter!")) - let twitterLink = `https://twitter.com/${twt.user.screen_name}/status/${twt.id_str}`; - wClient.send(`${config.webhook_message} ${twitterLink}`); -}); - -stream.on("disconnect", () => - console.log("Disconnected from twitter!") -); + .on("disconnect", () => console.warn("Disconnected from Twitter!")) -stream.on("reconnect", () => - console.log("Reconnecting to Twitter!") -); + .on("reconnect", () => console.info("Reconnected to Twitter!")); process.on("uncaughtException", err => - console.log(`Something (bad) happened: ${err}`) + console.error(`Something (bad) happened:\n${err}`) );