














Posted on Tuesday, 17th of August 2021
When I saw Twitter Plays Snake bot, I knew I should create something similar to that.
It took me a while to think of game, and at first I made Blackjack, but it didn't gain any attention so my second attempt was Tetris, and seems like people really love it. Check it out!
So, TL;DR: you can control how pieces fall by doing something to a tweet: comment to make it move left, retweet to rotate it and like to move it right.

Mmm, I saw the light green text, I guess it was the info they still needed.
I replied:

And on next day:

Yay, now time to suffer and learn how API works.
I ended up using twitter-api-v2 library. And finally, the "Hello, world!" was posted:
import Twitter from 'twitter-api-v2';
const client = new Twitter.default({
appKey: 'consumer key',
appSecret: 'consumer secret',
accessToken: 'access token',
accessSecret: 'access secret',
});
await client.v1.tweet("Hello, world!");
.down(), .right(), .rotate(), etc...display that shows blocks in game. So gotta transform that into pretty string:
let out = "";
for(let i in display) {
for(let j in display[i]) out += display[i][j]
if(i == 2) out += ` New game.`;
if(i == 3) out += ` Score: ${score}`;
if(i == 4) out += ` Next: ${pNext.name}`;
out += "
";
}
out += "💬=⬅️ 🔁=🔄 ❤️=➡️";
index = 0 and this is the sending code:
try {
await client.v1.tweet(out);
} catch(e) {
out += ` ${index++}`;
await client.v1.tweet(out);
}
Well, we sent our start tweet, after 5 minutes bot will have to check for comment, like and retweet count, so let's implement that.
Getting like and retweet count is really easy, just have to get tweet ID and request tweet data:
// Get last tweet made by bot excluding replies and retweets
let lastTweet = (await client.v2.userTimeline('1424384057991995397', { exclude: 'replies,retweets' })).tweets[0];
// Get tweet data
lastTweet = await client.v1.get(`statuses/show.json`, {id: lastTweet.id});
let likes = lastTweet.favorite_count; // right
let retweets = lastTweet.retweet_count; // spin
// Get last tweet made by bot excluding replies and retweets
let lastTweet = (await client.v2.userTimeline('1424384057991995397', { exclude: 'replies,retweets' })).tweets[0];
// Get Conversation ID of that tweet
let conv = await client.v2.get(`tweets?ids=${lastTweet.id}&tweet.fields=conversation_id`);
// Get list of comments with additional fields
conv = (await client.v2.get(`tweets/search/recent?query=conversation_id:${conv.data[0].conversation_id}&tweet.fields=in_reply_to_user_id,author_id`)).data;
// If there are no comments, there will be no .data array
if(!conv) conv = [];
// Filter replies to other tweets, map it so it'll consists of author IDs, create Set which will remove all duplicates and create array again.
conv = [...new Set(
conv
.filter(i => i.in_reply_to_user_id === '1424384057991995397')
.map(i => i.author_id)
)
];
// Array now only consists of unique author IDs so we can just get it's length
let comments = conv.length; // left
comments, retweets and likes counts, so we just do actions to the game:
if(likes > comments && likes === retweets) {
// More likes than comments and equal to retweets
// Right + Rotate
p.moveRight();
p.rotate();
} else if(comments > likes && comments === retweets) {
// More comments than likes and equal to retweets
// Left + Rotate
p.moveLeft();
p.rotate();
} else if(retweets > likes && retweets > comments) {
// More retweets than likes and comments
// Rotate
p.rotate();
} else if(comments > likes) {
// More comments than likes
// Left
p.moveLeft();
} else if(likes > comments) {
// More likes than comments
// Right
p.moveRight();
}
// In the end, move piece down
p.moveDown();
And then we convert display to pretty string and send the tweet as mentioned before. And then interval waits for another 5 minutes, checks for counts, does action and post tweet, repeat forever.That's it, the game is done! You can check how people suck at it via this link. Cya in another, uh,,,, lot of months...
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。