mastashake08 / discord-twitter-bot

A discord bot that sends messages to a channel whenever a specific user tweets.
ISC License
37 stars 21 forks source link

Apply filters to only post tweet and no retweets 🤔 #2

Closed siendomiguel closed 3 years ago

siendomiguel commented 3 years ago

I am trying to apply a filter, so that it only places the tweets of a user and not the retweets, but I still can't get it to work.

This would be the original code

const twitterClient = new Twitter(twitterConf);
// Specify destination channel ID below
const dest = 'config.channelId'; 

// Create a stream to follow tweets
const stream = twitterClient.stream('statuses/filter', {
  follow: 'config.userId', // user_id
});

stream.on('tweet', tweet => {
  const twitterMessage = `NEWS!! @here **${tweet.user.name}** acaba de realizar un nuevo tweet :grin: \n\n https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`;
  try {
      let channel =  client.channels.fetch(dest).then(channel => { channel.send(twitterMessage)
      }).catch(err => {
          console.log(err)
      })

  } catch (error) {
      console.error(error);
}
});

apply a possible solution, but it still does not work

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
}
siendomiguel commented 3 years ago

Ready, I got the solution to the problem, I was applying the filter incorrectly, it is not necessary to create a function if it is only going to be used once, as is my case, example:

const twitterClient = new Twitter(twitterConf);
// Specify destination channel ID below
const dest = 'config.channelId'; 

// Create a stream to follow tweets
const stream = twitterClient.stream('statuses/filter', {
  follow: 'config.userId', // user_id
});

stream.on('tweet', tweet => {
    if(tweet.retweeted || tweet.retweeted_status || tweet.in_reply_to_status_id || tweet.in_reply_to_user_id || tweet.delete) {
        // skip retweets and replies ------- new
        return;
    }
  const twitterMessage = `NEWS!! @here **${tweet.user.name}** acaba de realizar un nuevo tweet :grin: \n\n https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`;
  try {
      let channel =  client.channels.fetch(dest).then(channel => { channel.send(twitterMessage)
      }).catch(err => {
          console.log(err)
      })

  } catch (error) {
      console.error(error);
}
});