With this snippet you can look for the Twitter trends of a certain city or country or why not of the whole world. To use this snippet you need to create a command with required string in the command builder. Usage /trends [city or country or you can also put Worldwide to show trends from around the world]. (Some famous trends may not be shown because their tweets appear as null. This table is ranked from highest to lowest tweets)
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
try {
const list = await lib.twitter.trends['@1.1.0'].available.list();
let message = context.params.event.data.options[0].value;
let splits = message.split(' '); //Capitalize each letter per word in case the user hasn't
for (let i = 0; i < splits.length; i++) {
splits[i] = splits[i].charAt(0).toUpperCase() + splits[i].slice(1);
}
const MessageResult = splits.join(' ');
let Search = list.find((o) => o.name === `${MessageResult}`);
const TrendList = await lib.twitter.trends['@1.1.0'].place.list({
id: `${Search.woeid}`,
});
const TrendsResults = TrendList[0].trends;
const sorted = TrendsResults.sort(
(prev, next) => next.tweet_volume - prev.tweet_volume
);
await lib.discord.channels['@0.3.2'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `Trends for ${Search.name}, ${Search.country}`,
description: `Showing 20 most famous trends in ${Search.name} \n
(Some famous trends may not be shown because their tweets appear as null. This table is ranked from highest to lowest tweets)`,
color: 0x00ffff,
fields: [
{
name: `1. ${sorted[0].name}`,
value:
`Tweets: ${sorted[0].tweet_volume} [Join to the trend](${sorted[0].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `2. ${sorted[1].name}`,
value:
`Tweets: ${sorted[1].tweet_volume} [Join to the trend](${sorted[1].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `3. ${sorted[2].name}`,
value:
`Tweets: ${sorted[2].tweet_volume} [Join to the trend](${sorted[2].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `4. ${sorted[3].name}`,
value:
`Tweets: ${sorted[3].tweet_volume} [Join to the trend](${sorted[3].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `5. ${sorted[4].name}`,
value:
`Tweets: ${sorted[4].tweet_volume} [Join to the trend](${sorted[4].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `6. ${sorted[5].name}`,
value:
`Tweets: ${sorted[5].tweet_volume} [Join to the trend](${sorted[5].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `7. ${sorted[6].name}`,
value:
`Tweets: ${sorted[6].tweet_volume} [Join to the trend](${sorted[6].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `8. ${sorted[7].name}`,
value:
`Tweets: ${sorted[7].tweet_volume} [Join to the trend](${sorted[7].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `9. ${sorted[8].name}`,
value:
`Tweets: ${sorted[8].tweet_volume} [Join to the trend](${sorted[8].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `10. ${sorted[9].name}`,
value:
`Tweets: ${sorted[9].tweet_volume} [Join to the trend](${sorted[9].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `11. ${sorted[10].name}`,
value:
`Tweets: ${sorted[10].tweet_volume} [Join to the trend](${sorted[10].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `12. ${sorted[11].name}`,
value:
`Tweets: ${sorted[11].tweet_volume} [Join to the trend](${sorted[11].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `13. ${sorted[12].name}`,
value:
`Tweets: ${sorted[12].tweet_volume} [Join to the trend](${sorted[12].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `14. ${sorted[13].name}`,
value:
`Tweets: ${sorted[13].tweet_volume} [Join to the trend](${sorted[13].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `15. ${sorted[14].name}`,
value:
`Tweets: ${sorted[14].tweet_volume} [Join to the trend](${sorted[14].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `16. ${sorted[15].name}`,
value:
`Tweets: ${sorted[15].tweet_volume} [Join to the trend](${sorted[15].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `17. ${sorted[16].name}`,
value:
`Tweets: ${sorted[16].tweet_volume} [Join to the trend](${sorted[16].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `18. ${sorted[17].name}`,
value:
`Tweets: ${sorted[17].tweet_volume} [Join to the trend](${sorted[17].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `19. ${sorted[18].name}`,
value:
`Tweets: ${sorted[18].tweet_volume} [Join to the trend](${sorted[18].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
{
name: `20. ${sorted[19].name}`,
value:
`Tweets: ${sorted[19].tweet_volume} [Join to the trend](${sorted[19].url})`
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ','),
},
],
thumbnail: {
url: `https://help.twitter.com/content/dam/help-twitter/brand/logo.png`,
height: 0,
width: 0,
},
footer: {
text: `Tip: You can place \"Worldwide\" to show trends from around the world`,
icon_url: `https://help.twitter.com/content/dam/help-twitter/brand/logo.png`,
},
},
],
});
} catch (e) {
await lib.discord.channels['@0.3.2'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: ``,
embeds: [
{
type: 'rich',
title: `TRENDS ERROR`,
description: `❌|Error: Country or city not available to search.`,
color: 0xff0505,
},
],
});
}