[text] look at line 8 and line 89

Viewer

copydownloadembedprintName: look at line 8 and line 89
  1. Step 1:
  2. npm install sqlite3
  3.  
  4. Step 2:
  5. create a new file called database.js and write the following code:
  6.  
  7. javascript:
  8. DATABASE.js
  9. const sqlite3 = require('sqlite3').verbose();
  10.  
  11. const db = new sqlite3.Database('data.db', (err) => {
  12.   if (err) {
  13.     console.error('Error opening database:', err.message);
  14.   } else {
  15.     console.log('Connected to the SQLite database.');
  16.     db.run(
  17.   `CREATE TABLE IF NOT EXISTS users (
  18.     username TEXT PRIMARY KEY,
  19.     points INTEGER,
  20.     rod INTEGER,
  21.     last_fishing INTEGER
  22.   )`,
  23.   (err) => {
  24.     if (err) {
  25.       console.error('Error creating table:', err.message);
  26.     }
  27.   }
  28. );
  29.  
  30.   }
  31. });
  32.  
  33. module.exports = db;
  34.  
  35. This code initializes an SQLite database and creates a table called users to store 
  36. the username, points, and rod level for each user. Next, update your main code to 
  37. use the SQLite database to store and retrieve user data. Import the database.js file
  38. and replace the existing userPointsAndRods object with database queries:
  39.  
  40.  
  41. // ... (previous imports)
  42. const db = require('./database');
  43.  
  44. // ... (previous code)
  45.  
  46. function updateUserPointsAndRods(username, pointsToAdd, rodToAdd, fishingTimestamp) {
  47.   db.get('SELECT * FROM users WHERE username = ?', [username], (err, row) => {
  48.     if (err) {
  49.       console.error(err.message);
  50.       return;
  51.     }
  52.  
  53.     if (row) {
  54.       // Update existing user
  55.       db.run(
  56.         'UPDATE users SET points = points + ?, rod = rod + ?, last_fishing = ? WHERE username = ?',
  57.         [pointsToAdd, rodToAdd, fishingTimestamp, username],
  58.         (err) => {
  59.           if (err) {
  60.             console.error(err.message);
  61.           }
  62.         }
  63.       );
  64.     } else {
  65.       // Insert new user
  66.       db.run(
  67.         'INSERT INTO users (username, points, rod, last_fishing) VALUES (?, ?, ?, ?)',
  68.         [username, pointsToAdd, 1 + rodToAdd, fishingTimestamp],
  69.         (err) => {
  70.           if (err) {
  71.             console.error(err.message);
  72.           }
  73.         }
  74.       );
  75.     }
  76.   });
  77. }
  78.  
  79.  
  80. // ... (update the rest of the code to use the new updateUserPointsAndRods function)
  81.  
  82. This code replaces the existing updateLeaderboard function with a new function 
  83. called updateUserPointsAndRods, which uses the SQLite database to store and 
  84. retrieve user data. Make sure to update the rest of your code to use the new 
  85. updateUserPointsAndRods function instead of directly accessing the userPointsAndRods 
  86. object.
  87.  
  88.  
  89. LOSER.JS:
  90. const db = require('./database');
  91.  
  92. const tmi = require('tmi.js');
  93. const axios = require('axios');
  94.  
  95. const { channel, username, password } = require('./settings.json');
  96.  
  97. const options = {
  98.   options: { debug: true },
  99.   connection: {
  100.     reconnect: true,
  101.     secure: true,
  102.   },
  103.   identity: {
  104.     username,
  105.     password,
  106.   },
  107.   channels: [channel],
  108. };
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. let odds = 1
  117. //for showing odds at the begging of bot initlaization has no effect on actual odds
  118. let base_odds = 10
  119. daily_odds = (odds*100)
  120. if (odds === 1){
  121.     daily_odds = 0
  122. }
  123.  
  124.  
  125. const client = new tmi.Client(options);
  126.  
  127. // Define a function to fetch emotes from the 7TV API
  128. async function fetchEmotes() {
  129.   try {
  130.     const response = await axios.get('https://api.7tv.app/v2/users/wantep/emotes');
  131.     return response.data.map((emote) => ({
  132.       name: emote.name,
  133.       rarity: assignRarity()
  134.     }));
  135.   } catch (error) {
  136.     console.error(`Failed to fetch emotes: ${error.message}`);
  137.     return [];
  138.   }
  139. }
  140.  
  141.  
  142.  
  143. // Function to assign rarity to emotes
  144. function assignRarity() {
  145.  
  146.   const rand = Math.random();
  147.  
  148.   rarities = [0.6, 0.85, 0.95, 1]
  149.   rarityTitles = ["common", "rare", "epic", "legendary"]
  150.  
  151.   for (let i = 0; i < rarities.length; i++)
  152.   {
  153.       if (rand < rarities[i])
  154.       {
  155.           return rarityTitles[i];
  156.       }
  157.   }
  158. }
  159.  
  160.  
  161.  
  162. let emotes = [];
  163.  
  164. // Fetch emotes from the API and store them in the emotes array
  165. fetchEmotes()
  166.   .then((fetchedEmotes) => {
  167.     emotes = fetchedEmotes;
  168.     console.log(`Fetched ${emotes.length} emotes from the API`);
  169.   })
  170.   .catch((error) => {
  171.     console.error(`Failed to fetch emotes: ${error.message}`);
  172.   });
  173.  
  174. const userTimestamps = {};
  175. const emoteleaderboard = {};
  176.  
  177. setInterval(() => {
  178.   const threshold = 60 * 60 * 1000;
  179.   const now = Date.now();
  180.  
  181.   for (const user in userTimestamps) {
  182.     if (userTimestamps.hasOwnProperty(user)) {
  183.       const timestamp = userTimestamps[user];
  184.  
  185.       if (now - timestamp > threshold) {
  186.         delete userTimestamps[user];
  187.       }
  188.     }
  189.   }
  190. }, 60 * 60 * 1000);
  191.  
  192.  
  193. function updateLeaderboard(username, rarity) {
  194.     if (!emoteleaderboard[username]) {
  195.       emoteleaderboard[username] = 0;
  196.     }
  197.   
  198.     switch (rarity) {
  199.       case 'common':
  200.         emoteleaderboard[username] += 1;
  201.         break;
  202.       case 'rare':
  203.         emoteleaderboard[username] += 5;
  204.         break;
  205.       case 'epic':
  206.         emoteleaderboard[username] += 10;
  207.         break;
  208.       case 'legendary':
  209.         emoteleaderboard[username] += 20;
  210.         break;
  211.     }
  212.   }
  213.  
  214.   // Add a new object to store user points and rods
  215. const userPointsAndRods = {};
  216.  
  217. // Update this function to also update user points based on the emote rarity
  218. function updateLeaderboard(username, rarity) {
  219.     if (!emoteleaderboard[username]) {
  220.       emoteleaderboard[username] = 0;
  221.     }
  222.    
  223.     if (!userPointsAndRods[username]) {
  224.       userPointsAndRods[username] = { points: 0, rod: 1 };
  225.     }
  226.    
  227.     let pointsToAdd;
  228.     switch (rarity) {
  229.       case 'common':
  230.         pointsToAdd = 1;
  231.         break;
  232.       case 'rare':
  233.         pointsToAdd = 5;
  234.         break;
  235.       case 'epic':
  236.         pointsToAdd = 10;
  237.         break;
  238.       case 'legendary':
  239.         pointsToAdd = 20;
  240.         break;
  241.     }
  242.     emoteleaderboard[username] += pointsToAdd;
  243.     userPointsAndRods[username].points += pointsToAdd;
  244.   }
  245.  
  246.   function upgradeRod(username) {
  247.   db.get('SELECT * FROM users WHERE username = ?', [username], (err, userData) => {
  248.     if (err) {
  249.       console.error(err.message);
  250.       return;
  251.     }
  252.  
  253.     if (!userData) {
  254.       updateUserPointsAndRods(username, 1, 1, Date.now());
  255.       client.say(channel, `@${user.username}, you have successfully upgraded your rod to level 2! Your new balance is 0 points.`);
  256.       return;
  257.     }
  258.  
  259.     const rodCost = userData.rod * 5;
  260.     if (userData.points >= rodCost) {
  261.       updateUserPointsAndRods(username, -rodCost, 1, userData.last_fishing);
  262.         client.say(channel, `@${user.username}, you have successfully upgraded your rod to level ${userData.rod + 1}! Your new balance is ${userData.points - rodCost} points.`);
  263.     } else {
  264.       client.say(channel, `@${user.username}, you need ${rodCost} points to upgrade your rod. You currently have ${userData.points} points.`);
  265.     }
  266.   });
  267. }
  268.  
  269.   function didYouCatch(){
  270.     const rodLevel = userPointsAndRods[username]?.rod || 1;
  271.     const catchRateMultiplier = 1 + (rodLevel - 1) * 0.1;
  272.     const rand = Math.random();
  273.  
  274.     return (rand < 0.1 * odds * catchRateMultiplier)
  275.  
  276.   }
  277.   
  278.   function catchEmote(emote, username) {
  279.  
  280.     if(didYouCatch()===true){
  281.       const rand = Math.random()
  282.       const rodLevel = userPointsAndRods[username]?.rod || 1;
  283.       const catchRateMultiplier = 1 + (rodLevel - 1) * 0.1;
  284.  
  285.       if (assignRarity() ==="common"){
  286.         return true
  287.       }
  288.       else if (assignRarity() === "rare"){
  289.         return true
  290.       }
  291.       else if (assignRarity() === "epic"){
  292.  
  293.       } else {
  294.         return true
  295.       }
  296.    
  297.     }
  298.     
  299.     
  300.    
  301.     
  302.     
  303.    /* switch (emote.rarity) {
  304.       case 'common':
  305.         return rand < 0.06 * odds * catchRateMultiplier;
  306.       case 'rare':
  307.         return rand < 0.025 * odds * catchRateMultiplier;
  308.       case 'epic':
  309.         return rand < 0.013 * odds * catchRateMultiplier;
  310.       case 'legendary':
  311.         return rand < 0.002 * odds * catchRateMultiplier;
  312.       default:
  313.         return false;
  314.     }
  315.     */
  316.   }
  317.  
  318.   function isBroadcasterOrMod(user) {
  319.     return user.badges?.broadcaster === '1' || user.mod;
  320.   }
  321.    
  322.   function setNewOdds(odds2) {
  323.     if (odds2 >= 0 && odds2 <= 100) {
  324.       return true;
  325.     } else {
  326.       return false;
  327.     }
  328.   }
  329.  
  330. function displayLeaderboard(channel) {
  331.   const sortedLeaderboard = Object.entries(emoteleaderboard)
  332.     .sort((a, b) => b[1] - a[1])
  333.     .slice(0, 10);
  334.  
  335.   let leaderboardMessage = '🎣 Fishing Leaderboard 🎣\n';
  336.  
  337.   sortedLeaderboard.forEach(([username, score], index) => {
  338.     leaderboardMessage += `${index + 1}. ${username} - ${score} points\n`;
  339.   });
  340.  
  341.   client.say(channel, leaderboardMessage);
  342. }
  343.  
  344. client.connect().catch(console.error);
  345. client.on('connected', () => {
  346.  
  347.   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`);
  348. });
  349.  
  350.  
  351.  
  352.   
  353.  
  354. client.on('message', (channel, user, message, self) => {
  355.  
  356.   if (self) return;
  357.   const lowerCaseMessage = message.toLowerCase();
  358.   if (message.toLowerCase().startsWith('fishing')) {
  359.     const currentTime = Date.now();
  360.     const lastTimestamp = userTimestamps[user.username];
  361.  
  362.   if (!lastTimestamp || currentTime - lastTimestamp > 100) {
  363.   updateUserPointsAndRods(user.username, 0, 0, currentTime);
  364.         
  365.       const randFakeChance = Math.floor(Math.random()*10)+ 1;
  366.       const emoterange = Math.floor(Math.random() * emotes.length);
  367.       const selectedEmote = emotes[emoterange];
  368.  
  369.  
  370.       if (catchEmote(selectedEmote)) {
  371.         updateLeaderboard(user.username, selectedEmote.rarity);
  372.         client.say(channel, `@${user.username}, WOHOOOO YOU CAUGHT A ${selectedEmote.rarity.toUpperCase()} EMOTE! HERE IT IS --> ${selectedEmote.name}`);
  373.       } else {
  374.         client.say(channel, `@${user.username}, No luck... (${randFakeChance} cm. away)`);
  375.       }
  376.     } else {
  377.   client.say(channel, `@${user.username}, please wait a bit longer before triggering the command again.`);
  378. }
  379.   } else if (message.toLowerCase().startsWith('!emoteleaderboard')) {
  380.     displayLeaderboard(channel);
  381.   } else if (message.toLowerCase().startsWith('!upgraderod')) {
  382.     const upgradeResult = upgradeRod(user.username);
  383.     client.say(channel, `@${user.username}, ${upgradeResult}`);
  384.   } else if (lowerCaseMessage.startsWith('!setodds')) {
  385.     if (isBroadcasterOrMod(user)) {
  386.       // Parse the command and set the new odds
  387.       const [command, enteredOdds] = lowerCaseMessage.split(' ');
  388.  
  389.       // Validate and convert the odds to numbers
  390.       const newEnteredOdds = parseFloat(enteredOdds);
  391.       console.log("newodds",newEnteredOdds)
  392.       if (setNewOdds(newEnteredOdds)) {
  393.         odds = newEnteredOdds;
  394.         client.say(
  395.           channel,
  396.           `@${user.username}, the odds have been updated.`
  397.         );
  398.       } else {
  399.         client.say(
  400.           channel,
  401.           `@${user.username}, failed to update odds. Make sure the odds are between 0 and 100.`
  402.         );
  403.       }
  404.     } else {
  405.       client.say(channel, `@${user.username}, you don't have permission to change the odds.`);
  406.     }
  407.   }
  408.  
  409.   console.log("odds",odds)
  410.   
  411.  
  412. });

Editor

You can edit this paste and save as new:


File Description
  • look at line 8 and line 89
  • Paste Code
  • 24 Mar-2023
  • 10.99 Kb
You can Share it: