skip to Main Content

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 complex. 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

2

Answers


  1. 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.
    Login or Signup to reply.
  2. 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.com/${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.com/ttezel/twit/issues/286#issuecomment-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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search