[javascript] ig 1.3

Viewer

  1. const tmi = require('tmi.js');
  2. const axios = require('axios');
  3. const { channel, username, password } = require('./settings.json');
  4.  
  5. const options = {
  6.   options: { debug: true },
  7.   connection: { reconnect: true, secure: true },
  8.   identity: { username, password },
  9.   channels: [channel],
  10. };
  11.  
  12. const client = new tmi.Client(options);
  13.  
  14. let odds = 1;
  15. let base_odds = 10;
  16. let daily_odds = (odds * 100);
  17. if (odds === 1) {
  18.   daily_odds = 0;
  19. }
  20.  
  21. let emotes = [];
  22.  
  23. async function fetchEmotes() {
  24.   try {
  25.     const response = await axios.get('https://api.7tv.app/v2/users/wantep/emotes');
  26.     return response.data.map((emote) => ({
  27.       name: emote.name,
  28.       rarity: assignRarity()
  29.     }));
  30.   } catch (error) {
  31.     console.error(`Failed to fetch emotes: ${error.message}`);
  32.     return [];
  33.   }
  34. }
  35.  
  36. function assignRarity() {
  37.   const rand = Math.random();
  38.  
  39.   if (rand < 0.6) {
  40.     return 'common';
  41.   } else if (rand < 0.85) {
  42.     return 'rare';
  43.   } else if (rand < 0.98) {
  44.     return 'epic';
  45.   } else {
  46.     return 'legendary';
  47.   }
  48. }
  49.  
  50. fetchEmotes()
  51.   .then((fetchedEmotes) => {
  52.     emotes = fetchedEmotes;
  53.     console.log(`Fetched ${emotes.length} emotes from the API`);
  54.   })
  55.   .catch((error) => {
  56.     console.error(`Failed to fetch emotes: ${error.message}`);
  57.   });
  58.  
  59. const userTimestamps = {};
  60. const emoteleaderboard = {};
  61.  
  62. setInterval(() => {
  63.   const threshold = 60 * 60 * 1000;
  64.   const now = Date.now();
  65.  
  66.   for (const user in userTimestamps) {
  67.     if (userTimestamps.hasOwnProperty(user)) {
  68.       const timestamp = userTimestamps[user];
  69.  
  70.       if (now - timestamp > threshold) {
  71.         delete userTimestamps[user];
  72.       }
  73.     }
  74.   }
  75. }, 60 * 60 * 1000);
  76.  
  77. const userPointsAndRods = {};
  78.  
  79. function updateLeaderboard(username, rarity) {
  80.   if (!emoteleaderboard[username]) {
  81.     emoteleaderboard[username] = 0;
  82.   }
  83.  
  84.   if (!userPointsAndRods[username]) {
  85.     userPointsAndRods[username] = { points: 0, rod: 1 };
  86.   }
  87.  
  88.   let pointsToAdd;
  89.   switch (rarity) {
  90.     case 'common':
  91.       pointsToAdd = 1;
  92.       break;
  93.     case 'rare':
  94.       pointsToAdd = 5;
  95.       break;
  96.     case 'epic':
  97.       pointsToAdd = 10;
  98.       break;
  99.     case 'legendary':
  100.       pointsToAdd = 20;
  101.       break;
  102.   }
  103.   emoteleaderboard[username] += pointsToAdd;
  104.   userPointsAndRods[username].points += pointsToAdd;
  105. }
  106.  
  107. function upgradeRod(username) {
  108.   const userData = userPointsAndRods[username];
  109.   if (!userData) {
  110.     userPointsAndRods[username] = { points: 1, rod: 1 };
  111.     return;
  112.   }
  113.  
  114.   const rodCost = userData.rod * 5;
  115.  
  116.   if (userData.points >= rodCost) {
  117.     userData.points -= rodCost;
  118.     userData.rod += 1;
  119.     return `Upgraded your rod to level ${userData.rod}! Your new balance is ${userData.points} points.`;
  120.   } else {
  121.     return `You need ${rodCost} points to upgrade your rod. You currently have ${userData.points} points.`;
  122.   }
  123. }
  124.  
  125. function catchEmote(emote, username) {
  126.   const rodLevel = userPointsAndRods[username]?.rod || 1;
  127.   const catchRateMultiplier = 1 + (rodLevel - 1) * 0.1;
  128.   const rand = Math.random();
  129.  
  130.   switch (emote.rarity) {
  131.     case 'common':
  132.       return rand < 0.06 * odds * catchRateMultiplier;
  133.     case 'rare':
  134.       return rand < 0.025 * odds * catchRateMultiplier;
  135.     case 'epic':
  136.       return rand < 0.013 * odds * catchRateMultiplier;
  137.     case 'legendary':
  138.       return rand < 0.002 * odds * catchRateMultiplier;
  139.     default:
  140.       return false;
  141.   }
  142. }
  143.  
  144. function isBroadcasterOrMod(user) {
  145.   return user.badges?.broadcaster === '1' || user.mod;
  146. }
  147.  
  148. function setNewOdds(odds2) {
  149.   if (odds2 >= 0 && odds2 <= 10) {
  150.     return true;
  151.   } else {
  152.     return false;
  153.   }
  154. }
  155.  
  156. function displayLeaderboard(channel) {
  157.   const sortedLeaderboard = Object.entries(emoteleaderboard)
  158.     .sort((a, b) => b[1] - a[1])
  159.     .slice(0, 10);
  160.  
  161.   let leaderboardMessage = '🎣 Fishing Leaderboard 🎣\n';
  162.  
  163.   sortedLeaderboard.forEach(([username, score], index) => {
  164.     leaderboardMessage += `${index + 1}. ${username} - ${score} points\n`;
  165.   });
  166.  
  167.   client.say(channel, leaderboardMessage);
  168. }
  169.  
  170.  
  171. client.connect().catch(console.error);
  172. client.on('connected', () => {
  173.   client.say(channel, `Fishing has BEGUN! The chances today are increased by ${daily_odds}% chance, now its ${odds*base_odds}% to catch a fish 'type fishing to fish :o`);
  174. });
  175.  
  176. client.on('message', (channel, user, message, self) => {
  177.   if (self) return;
  178.  
  179.   const lowerCaseMessage = message.toLowerCase();
  180.   const currentTime = Date.now();
  181.   const lastTimestamp = userTimestamps[user.username];
  182.  
  183.   if (lowerCaseMessage.startsWith('fishing')) {
  184.     handleFishing();
  185.   } else if (lowerCaseMessage.startsWith('!emoteleaderboard')) {
  186.     handleEmoteLeaderboard();
  187.   } else if (lowerCaseMessage.startsWith('!upgraderod')) {
  188.     handleUpgradeRod();
  189.   }
  190. });

Editor

You can edit this paste and save as new:


File Description
  • ig 1.3
  • Paste Code
  • 23 Mar-2023
  • 4.75 Kb
You can Share it: