幕布斯7119047
最简单的方法如下: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 belowconst dest = '11111111111111111111'; // Create a stream to follow tweetsconst 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);当然,以下假设您在适当的环境变量中指定了 Discord 和 Twitter 密钥。注意:默认情况下,流将包含的不仅仅是用户自己的推文:转发、回复。您可以使用以下功能轻松过滤它们(不是我的,来源指出):// SOURCE:// https://github.com/ttezel/twit/issues/286#issuecomment-236315960function 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;}出于测试目的,我在这里使用了@Every3Minutes,因为它每 3 分钟发送一次推文,这对我的测试非常有用。