admin管理员组文章数量:1384189
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 badges2 Answers
Reset to default 5The 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 :
- Create a stream for each user you want to track. (this may help you to target a user)
- Then link each
stream.on('tweet'
to a response of your discord bot.
本文标签:
版权声明:本文标题:javascript - How to integrate Discord.js and twit with each other for a live Twitter feed on a specified channel - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744462102a2607295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论