最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to integrate Discord.js and twit with each other for a live Twitter feed on a specified channel - Stack Overflo

programmeradmin2浏览0评论

Is it possible to make a crawler with Twit and is it also possible to output the received data in a Discord channel? Currently I have made a script with both modules and it's working fine. The console output works for Discord.js and Twit at the same time. I have entered the token for Discord and the several keys for the Twitter API. But my goal is a bit more plex. With twit I need to make a crawler that crawls tweets from twitter accounts in real-time and Discord.js is supposed to output this data in a Discord channel. Does anyone have any idea how to do this? I tried to experiment with the stream function of Twit but couldn't figure out how it works exactly. It crawled random tweets from any time span. I'm not sure how to configure it. And even if I figured that out I still need to integrate it with Discord.js

Is it possible to make a crawler with Twit and is it also possible to output the received data in a Discord channel? Currently I have made a script with both modules and it's working fine. The console output works for Discord.js and Twit at the same time. I have entered the token for Discord and the several keys for the Twitter API. But my goal is a bit more plex. With twit I need to make a crawler that crawls tweets from twitter accounts in real-time and Discord.js is supposed to output this data in a Discord channel. Does anyone have any idea how to do this? I tried to experiment with the stream function of Twit but couldn't figure out how it works exactly. It crawled random tweets from any time span. I'm not sure how to configure it. And even if I figured that out I still need to integrate it with Discord.js

Share Improve this question asked Oct 12, 2019 at 14:19 MCKMCK 6452 gold badges7 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The simplest way is as follows:

const Discord = require('discord.js');
const Twitter = require('twit');
const twitterConf = {
    consumer_key: process.env.TWITTER_CONSUMER_KEY,
    consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
    access_token: process.env.TWITTER_ACCESS_TOKEN_KEY,
    access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
  }
const client = new Discord.Client();
const twitterClient = new Twitter(twitterConf);
// Specify destination channel ID below
const dest = '11111111111111111111'; 

// Create a stream to follow tweets
const stream = twitterClient.stream('statuses/filter', {
  follow: '2899773086', // @Every3Minutes, specify whichever Twitter ID you want to follow
});

stream.on('tweet', tweet => {
  const twitterMessage = `${tweet.user.name} (@${tweet.user.screen_name}) tweeted this: https://twitter./${tweet.user.screen_name}/status/${tweet.id_str}`
  client.channels.get(dest).send(twitterMessage);
  return false;
});

client.on('ready', () => {
  console.log(`I'm in`);
});

client.login(process.env.DISCORD_TOKEN);

Of course the following assumes that you have Discord and Twitter keys specified in proper environment variables.

NOTE: By default, stream will contains more than user's own tweets: retweets, replies. You can easily filter them using the following function (not mine, source noted):

// SOURCE:
// https://github./ttezel/twit/issues/286#issuement-236315960
function isReply(tweet) {
  if (tweet.retweeted_status
    || tweet.in_reply_to_status_id
    || tweet.in_reply_to_status_id_str
    || tweet.in_reply_to_user_id
    || tweet.in_reply_to_user_id_str
    || tweet.in_reply_to_screen_name) return true;
  return false;
}

For testing purposes I used @Every3Minutes here, as it tweets every 3 minutes, which was nice for my testing.

I would do it this way :

  1. Create a stream for each user you want to track. (this may help you to target a user)
  2. Then link each stream.on('tweet' to a response of your discord bot.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论