nationalcollegiateesports / discord-bot

MIT License
2 stars 0 forks source link

Reposting social media actions - e.g. tweets, going live #1

Open themaxdavitt opened 3 years ago

Nick-Sol commented 3 years ago

Twitter The way I have done a Twitter feed in the past is by using a package called twit (https://github.com/ttezel/twit#readme) from here it lets me interact with the Twitter API and gives a stream of data when an account we are pointing at does an event on Twitter we can filter this down to tweets and not retweets. Here is some code we are using currently

In the Main js file to open the stream


var twitter = new Twit({
    consumer_key:         consumer_keyU,
    consumer_secret:      consumer_secretU,
    access_token:         access_tokenU,
    access_token_secret:  access_token_secretU,
  })
var stream = twitter.stream('statuses/filter', { follow: '724480381840445440' })

stream.on('tweet', function (tweet) {
    client.commands.get('tweetPost').execute(tweet, client);
  })

The code to make an embedded message on a tweet event

const Discord = require('discord.js')
module.exports = {
    name: 'tweetPost',
    description: 'Sends a message in specific chat for twitter updates.',
    execute(tweet, client) {
        let tweetData = tweet;

        //Creates a embed message with the data from twitter api
        var url = "https://twitter.com/Redbird_Esports/status/" + tweetData.id_str
        if(tweetData.user.id_str == '724480381840445440'){
            const tweetEmbed = new Discord.MessageEmbed()
            .setColor('#FF0000')
            .setTitle('New Redbird Esports tweet!')
            .setURL('https://twitter.com/Redbird_Esports')
            .setAuthor('Reggie')
            .setThumbnail(tweetData.user.profile_image_url)
            .addFields(
                { name: 'Tweet Contents', value: tweetData.text },
                { name: 'Tweet link', value: url },
            )
            .setTimestamp()
            .setFooter('Bot made by REN');

            client.channels.cache.get('764109052950741012').send(tweetEmbed)
        }
    },
};

Livestream status For setting a users special role when they are streaming we do this in a presenceUpdate event and there is still currently some filtering issues I need to handle to not clog up the console but once that is done this should be workable


//This is how we handle assigning the streaming role to a user (This most likely can be its own command but first want to make sure it's running smoothly since I found some errors already)
client.on('presenceUpdate', (oldPresence, newPresence) =>{
    try{
        const streamRole = newPresence.guild.roles.cache.find(role => role.name === "🔴 - Currently Streaming");
        //console.log(streamRole);
        //The reason we use undefined here is because when a presence changes it does not require an activity (IE. going from away to online with not playing anything)
        //This will throw an error saying that 'newPresence.activities[0]' is undefined causing our logs to get spammed with errors. Their might be a better way of handling this but thats what I got for now.
        if(newPresence.activities[0] !== undefined){ 
            //once the users presence is streaming add the role.
            if(newPresence.activities[0].type === 'STREAMING'){
                newPresence.guild.members.cache.get(newPresence.userID).roles.add(streamRole);
                console.log("Stream Role Added");
            }
            //Once a user is no longer streaming and the user has the role take the role away
            else if((newPresence.activities[0].type !== 'STREAMING') && newPresence.guild.members.cache.get(newPresence.userID).roles.cache.has('748586477151060119')){
                console.log("Stream Role removed");
                newPresence.guild.members.cache.get(newPresence.user.id).roles.remove(streamRole);
            }
            else{}
        }
        //This is to deal with people who don't show game status so we check the oldPresence since the code block above doesn't take into account blank activities
        else if(newPresence.activities[0] === undefined && oldPresence.activities[0].type === "STREAMING"){
            console.log("Stream Role removed");
            newPresence.guild.members.cache.get(newPresence.user.id).roles.remove(streamRole);
        }
    }
    catch (error){
        console.log("Error in presenceUpdate code block");
    }

})

All of this can be changed and worked on to be better set for multiple servers since I didn't fully design this to scale up

Nick-Sol commented 3 years ago

So I did some more testing to help clean up the debug console on the streaming role to help handle user presence now the logs are clean and no issues (for now lol)

client.on('presenceUpdate', (oldPresence, newPresence) =>{
    try{
        const streamRole = newPresence.guild.roles.cache.find(role => role.name === "🔴 - Currently Streaming");
        //console.log(streamRole);
        //The reason we use undefined here is because when a presence changes it does not require an activity (IE. going from away to online with not playing anything)
        //This will throw an error saying that 'newPresence.activities[0]' is undefined causing our logs to get spammed with errors. Their might be a better way of handling this but thats what I got for now.
        if(newPresence.activities.length !== 0){ 
            //once the users presence is streaming add the role.
            if(newPresence.activities[0].type === 'STREAMING'){
                newPresence.guild.members.cache.get(newPresence.userID).roles.add(streamRole);
                console.log("Stream Role Added");
            }
            //Once a user is no longer streaming and the user has the role take the role away
            else if((newPresence.activities[0].type !== 'STREAMING') && newPresence.guild.members.cache.get(newPresence.userID).roles.cache.has('748586477151060119')){
                console.log("Stream Role removed");
                newPresence.guild.members.cache.get(newPresence.user.id).roles.remove(streamRole);
            }
            else{}
        }
        //This is to deal with people who don't show game status so we check the oldPresence since the code block above doesn't take into account blank activities
        else if(newPresence.activities.length == 0 && oldPresence != undefined){
            if(oldPresence.activities.length >= 1){
                if(oldPresence.activities[0].type === "STREAMING"){
                    console.log("Stream Role removed");
                    newPresence.guild.members.cache.get(newPresence.user.id).roles.remove(streamRole);
                    }
            }
        }
    }
    catch (error){
        console.log(error)
    }