1
Fork 0
mirror of https://github.com/RGBCube/TwitterDiscordWebhook synced 2025-07-27 13:07:45 +00:00

Modernize main.js

This commit is contained in:
RGBCube 2023-01-15 20:25:41 +03:00 committed by GitHub
parent 5ae7096e4a
commit 7343795ee6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

77
main.js
View file

@ -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}`
})
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}`);
})
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}'!`);
});
var stream = tClient.stream('statuses/filter', { follow: `${config.user_id}` })
console.log(`Fetching tweets from id: ${config.user_id}`)
let stream = tClient.stream("statuses/filter", { follow: config.user_id });
stream.on('connected', function (response) {
console.log(`Connected to twitter`)
})
console.log(`Fetching tweets from id '${config.user_id}'.`);
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("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', function (disconnectMessage) {
console.log(`Disconnected from twitter`)
})
stream.on("disconnect", _ => {
console.log("Disconnected from twitter!");
});
stream.on('reconnect', function (request, response, connectInterval) {
console.log(`reconnecting to twitter`)
})
stream.on("reconnect", function (_, _, _) => {
console.log("Reconnecting to Twitter!");
});
process.on('uncaughtException', function (err) {
console.log("something happened!", err);
});
process.on("uncaughtException", err => {
console.log(`Something (bad) happened: ${err}`);
});