tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.54k stars 217 forks source link

How to get array of all default emotes #413

Closed PrinceKomali closed 3 years ago

PrinceKomali commented 3 years ago

Very new to all of this, sorry! Is there a way to get an array of all the default emoji names? I'm trying to make a command that sends a random emote but I can't get it to work. I have looked at this but cannot understand any of it: https://dev.twitch.tv/docs/v5/reference/chat/#get-all-chat-emoticons

AlcaDesign commented 3 years ago

Note that some of those Kraken endpoints are fairly old. The badges endpoint listed above that isn't even right anymore. The url for the emote isn't the recommended URL either. (Use https://static-cdn.jtvnw.net/emoticons/v1/EMOTE_ID/1.0 -- change EMOTE_ID to the emote ID and change 1.0 to 2.0 or 3.0/4.0 for larger scales)

I suggest using https://api.twitch.tv/kraken/chat/emoticon_images?emotesets=0 to get the "global" emote set. If you supply a clientId in the client options you'll automatically get this data added to client.emotesets after the 'emotesets' event.

const tmi = require('tmi.js');
const client = new tmi.Client({
    options: { clientId: '...', debug: true },
    connection: { secure: true, reconnect: true },
    identity: { /* ... */ }
});
client.connect();
client.once('emotesets', () => {
    const globalSet = client.emotesets[0];
    const randomEmote = globalSet[Math.floor(Math.random() * globalSet.length)];
    client.say('mychannel', randomEmote.code);
});

Writing a command from there is easy.

PrinceKomali commented 3 years ago

but what do I do with that link? Sorry if I am asking obvious questions but I have no idea. Also the client.say(target, randomEmote) doesn't seem to do anything

AlcaDesign commented 3 years ago

Can you share some code you're using? (Remove any tokens you paste) Assuming you supply a clientId option as I showed in the example, tmi.js will automatically get the data from the emotesets API. Twitch sends the emote sets list and tmi.js automatically makes the request. All you need to do is pull a random ID from client.emotesets[0] (the global set). I missed adding the .code which I have now updated but otherwise this should be usable code.

PrinceKomali commented 3 years ago

I should have done that (excuse the messy code, I was trying a bunch of stuff)


const emotes = "Kappa PogChamp LUL Keepo TakeNRG NotLikeThis GivePLZ".split(" ")
//const emotes = emotesets 
// Define configuration options
const opts = {
    identity: {
        username: 'komalibot',
        password: ''
    },
    channels: [
        'komali09'
    ]
};
                    // Create a client with our options
                    const client = new tmi.client(opts);
client.once('emotesets', () => {
    const globalSet = client.emotesets[0];
    const randomEmote = globalSet[Math.floor(Math.random() * globalSet.length)];
    //client.say('komali09', randomEmote); 
});
                    // Register our event handlers (defined below)
                    client.on('message', onMessageHandler);
                    client.on('connected', onConnectedHandler);

                    // Connect to Twitch:
                    client.connect();

                    // Called every time a message comes in
function onMessageHandler (target, context, msg, self) {
  if (self) { return; } // Ignore messages from the bot
console.log(client.emotesets)
  // Remove whitespace from chat message
  const commandName = msg.trim();

  if(commandName === '!emotetest'){
    client.say(target, randomEmote)
  }
  if (commandName === '!slots') {
    //client.say(target, `test`)
    var slotstr = ""
    var i
    for(i=0;i<3;i++){
slotstr = slotstr + emotes[Math.floor(Math.random()*emotes.length)] + " "
    }
    client.say(target, slotstr)
    client.say(target, `Please Play Again`) 
     }
  /*if(commandName === "!color"){
  client.say(target, "/color 0x00CC00")  
  }*/
}

function onConnectedHandler (addr, port) {
                        console.log(`Logged in to ${addr}:${port}`);
}```
Thanks for helping!
AlcaDesign commented 3 years ago

I asked you to not post your token 🤦‍♂️

That's sadly not how JavaScript works. The randomEmote variable will not be available in the message handler.

Again, make sure to add a client ID as I've suggested. Get one here.

      const opts = {
+         options: {
+             clientId: 'my Twitch client ID',
+             debug: true
+         },
          identity: {
              username: 'komalibot',
              password: ''
          },
          channels: [
              'komali09'
          ]
      };

Here's a simple command example:

client.on('message', (channel, tags, message, self) => {
    if(self) return;
    if(message === '!emote') {
        const globalSet = client.emotesets[0];
        const randomEmote = globalSet[Math.floor(Math.random() * globalSet.length)];
        client.say(channel, randomEmote.code);
    }
});
PrinceKomali commented 3 years ago

Ok thank you, I will try this! (also i am not sure why it posted my token; I edited it out after pasting it) Really sorry for putting you through all of this, but thank you so much

PrinceKomali commented 3 years ago

Ok, I have tried that, and it says that it cannot read property "length" of undefined when referring to the globalSet variable, which in turn relates to client.emotesets