Advertisement
TrulyNaN

Untitled

Jan 30th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. const tmi = require('tmi.js');
  2.  
  3. // Define configuration options
  4. const opts = {
  5. identity: {
  6. username: "truly_not_a_bot",
  7. password: "oauth:some_long_string"
  8. },
  9. channels: [
  10. "trulynan"
  11. ]
  12. };
  13.  
  14.  
  15. // Create a client with our options
  16. const client = new tmi.client(opts);
  17.  
  18. // Register our event handlers (defined below)
  19. client.on('message', onMessageHandler);
  20. client.on('connected', onConnectedHandler);
  21.  
  22. // Connect to Twitch:
  23. client.connect().catch(
  24.  
  25. (error) => {
  26. console.log("test error"+error);
  27. return;
  28. }
  29. );
  30.  
  31.  
  32. // Called every time a message comes in
  33. function onMessageHandler (target, context, msg, self) {
  34. if (self) { return; } // Ignore messages from the bot
  35.  
  36. // Remove whitespace from chat message
  37. const commandName = msg.trim();
  38.  
  39. // If the command is known, let's execute it
  40. if (commandName === '!dice') {
  41. const num = rollDice();
  42. client.say(target, `You rolled a ${num}`);
  43. console.log(`* Executed ${commandName} command`);
  44. } else {
  45. console.log(`* Unknown command ${commandName}`);
  46. }
  47. }
  48.  
  49. // Function called when the "dice" command is issued
  50. function rollDice () {
  51. const sides = 6;
  52. return Math.floor(Math.random() * sides) + 1;
  53. }
  54.  
  55. // Called every time the bot connects to Twitch chat
  56. function onConnectedHandler (addr, port) {
  57. console.log(`* Connected to ${addr}:${port}`);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement