mirror of
https://github.com/RGBCube/TwitterDiscordWebhook
synced 2025-07-27 13:07:45 +00:00
Modernize main.js
This commit is contained in:
parent
5ae7096e4a
commit
7343795ee6
1 changed files with 40 additions and 37 deletions
89
main.js
89
main.js
|
@ -1,47 +1,50 @@
|
|||
const Twitter = require(`twit`)
|
||||
const Webhook = require(`discord-webhook-node`)
|
||||
const config = require(`./config/config.json`)
|
||||
import Twitter from "twit";
|
||||
import { Webhook } from "discord-webhook-node";
|
||||
|
||||
const wClient = new Webhook.Webhook(`${config.webhook_url}`)
|
||||
wClient.setAvatar(`${config.webhook_avatar}`)
|
||||
wClient.setUsername(`${config.webhook_name}`)
|
||||
const config = require("config.json");
|
||||
|
||||
const wClient = new Webhook(config.webhook_url);
|
||||
wClient.setAvatar(config.webhook_avatar);
|
||||
wClient.setUsername(config.webhook_name);
|
||||
|
||||
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}`
|
||||
})
|
||||
|
||||
tClient.get('account/verify_credentials', { skip_status: true })
|
||||
.catch(function (err) {
|
||||
console.log(`Unable to login due to: ${err}`)
|
||||
})
|
||||
.then(function (result) {
|
||||
console.log(`Logged in as: ${result.data.name}`);
|
||||
})
|
||||
|
||||
var stream = tClient.stream('statuses/filter', { follow: `${config.user_id}` })
|
||||
console.log(`Fetching tweets from id: ${config.user_id}`)
|
||||
|
||||
stream.on('connected', function (response) {
|
||||
console.log(`Connected to twitter`)
|
||||
})
|
||||
|
||||
stream.on('tweet', function (tweet) {
|
||||
if(tweet.retweeted_status || tweet.in_reply_to_status_id || tweet.is_quote_status) return
|
||||
let twitterLink = `https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`
|
||||
wClient.send(`${config.webhook_message} ${twitterLink}`)
|
||||
})
|
||||
|
||||
stream.on('disconnect', function (disconnectMessage) {
|
||||
console.log(`Disconnected from twitter`)
|
||||
})
|
||||
|
||||
stream.on('reconnect', function (request, response, connectInterval) {
|
||||
console.log(`reconnecting to twitter`)
|
||||
})
|
||||
|
||||
process.on('uncaughtException', function (err) {
|
||||
console.log("something happened!", err);
|
||||
consumer_key: config.api_key,
|
||||
consumer_secret: config.api_secret,
|
||||
access_token: config.access_token,
|
||||
access_token_secret: config.acesss_secret,
|
||||
});
|
||||
|
||||
tClient.get("account/verify_credentials", { skip_status: true })
|
||||
.catch(err => {
|
||||
console.log(`Unable to login due to the following error:\n${err}`)
|
||||
})
|
||||
.then(res => {
|
||||
console.log(`Logged in as '${res.data.name}'!`);
|
||||
});
|
||||
|
||||
let stream = tClient.stream("statuses/filter", { follow: config.user_id });
|
||||
|
||||
console.log(`Fetching tweets from id '${config.user_id}'.`);
|
||||
|
||||
stream.on("connected", _ => {
|
||||
console.log("Successfully connected to Twitter!");
|
||||
});
|
||||
|
||||
stream.on("tweet", twt => {
|
||||
if(twt.retweeted_status || twt.in_reply_to_status_id || twt.is_quote_status) return;
|
||||
|
||||
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!");
|
||||
});
|
||||
|
||||
stream.on("reconnect", function (_, _, _) => {
|
||||
console.log("Reconnecting to Twitter!");
|
||||
});
|
||||
|
||||
process.on("uncaughtException", err => {
|
||||
console.log(`Something (bad) happened: ${err}`);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue