Senne-1009

Nighbot Commands

Nov 9th, 2024
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function doGet(request) {
  2.   const queueSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  3.   const actionType = request.parameter.action;
  4.   const username = request.parameter.user;
  5.   const channelName = request.parameter.channel;
  6.   const userLevel = request.parameter.userlevel;
  7.   const tipAmount = parseFloat(request.parameter.tip) || 0;
  8.  
  9.   if (!actionType) {
  10.     return ContentService.createTextOutput("Missing action parameter.");
  11.   }
  12.  
  13.   // Join the queue
  14.   if (actionType === "join") {
  15.     const existingQueue = queueSheet.getDataRange().getValues();
  16.     const isUserInQueue = existingQueue.some((entry) => entry[0] === username && entry[1] === channelName);
  17.  
  18.     if (isUserInQueue) {
  19.       return ContentService.createTextOutput(`${username} is already in the queue.`);
  20.     }
  21.  
  22.     const isSubscriber = userLevel === "subscriber";
  23.     const timestamp = new Date();
  24.  
  25.     // Add the user to the sheet with tip amount
  26.     queueSheet.appendRow([username, channelName, timestamp, isSubscriber, tipAmount]);
  27.     return ContentService.createTextOutput(`${username} has been added to the queue${tipAmount >= 6 ? " (Top Tipper)" : isSubscriber ? " (Subscriber)" : ""}.`);
  28.   }
  29.  
  30.   // Leave the queue
  31.   if (actionType === "leave") {
  32.     const existingQueue = queueSheet.getDataRange().getValues();
  33.     for (let i = 1; i < existingQueue.length; i++) {
  34.       if (existingQueue[i][0] === username && existingQueue[i][1] === channelName) {
  35.         queueSheet.deleteRow(i + 1);
  36.         return ContentService.createTextOutput(`${username} has been removed from the queue.`);
  37.       }
  38.     }
  39.     return ContentService.createTextOutput(`${username} is not in the queue.`);
  40.   }
  41.  
  42.   // List the current queue
  43.   if (actionType === "list") {
  44.     const existingQueue = queueSheet.getDataRange().getValues();
  45.     const queueEntries = existingQueue.slice(1).filter((entry) => entry[0]);
  46.  
  47.     if (queueEntries.length === 0) {
  48.       return ContentService.createTextOutput("The queue is currently empty.");
  49.     }
  50.  
  51.     // Sort by priority: Top Tippers ($6 or higher), then Subscribers, then Tips < $6, then Regular Users
  52.     queueEntries.sort((a, b) => {
  53.       const tipA = parseFloat(a[4]) || 0;
  54.       const tipB = parseFloat(b[4]) || 0;
  55.  
  56.       if (tipA >= 6 && tipB < 6) return -1;
  57.       if (tipA < 6 && tipB >= 6) return 1;
  58.  
  59.       if (a[3] === true && b[3] === false) return -1;
  60.       if (a[3] === false && b[3] === true) return 1;
  61.  
  62.       if (tipA > 0 && tipB === 0) return -1;
  63.       if (tipA === 0 && tipB > 0) return 1;
  64.  
  65.       return new Date(a[2]) - new Date(b[2]);
  66.     });
  67.  
  68.     const formattedQueue = queueEntries.map((entry, index) => `${index + 1}. ${entry[0]} (${entry[1]})`).join(", ");
  69.     return ContentService.createTextOutput(`Current queue: ${formattedQueue}`);
  70.   }
  71.  
  72.   // Manually update the tip amount
  73.   if (actionType === "tipupdate") {
  74.     if (userLevel !== "moderator" && userLevel !== "owner") {
  75.       return ContentService.createTextOutput("You do not have permission to use this command.");
  76.     }
  77.  
  78.     const existingQueue = queueSheet.getDataRange().getValues();
  79.     for (let i = 1; i < existingQueue.length; i++) {
  80.       if (existingQueue[i][0] === username && existingQueue[i][1] === channelName) {
  81.         queueSheet.getRange(i + 1, 5).setValue(tipAmount);
  82.         return ContentService.createTextOutput(`Updated tip for ${username} to $${tipAmount}.`);
  83.       }
  84.     }
  85.     return ContentService.createTextOutput(`${username} is not in the queue.`);
  86.   }
  87.  
  88.   // Skip to the next person
  89.   if (actionType === "next") {
  90.     if (userLevel !== "moderator" && userLevel !== "owner") {
  91.       return ContentService.createTextOutput("You do not have permission to use this command.");
  92.     }
  93.  
  94.     const existingQueue = queueSheet.getDataRange().getValues();
  95.     if (existingQueue.length <= 1) {
  96.       return ContentService.createTextOutput("The queue is currently empty.");
  97.     }
  98.  
  99.     let userRemoved = false;
  100.  
  101.     for (let i = 1; i < existingQueue.length; i++) {
  102.       const tipAmount = parseFloat(existingQueue[i][4]) || 0;
  103.       if (tipAmount >= 6) {
  104.         queueSheet.deleteRow(i + 1);
  105.         userRemoved = true;
  106.         break;
  107.       }
  108.     }
  109.  
  110.     if (!userRemoved) {
  111.       for (let i = 1; i < existingQueue.length; i++) {
  112.         if (existingQueue[i][3] === true) {
  113.           queueSheet.deleteRow(i + 1);
  114.           userRemoved = true;
  115.           break;
  116.         }
  117.       }
  118.     }
  119.  
  120.     if (!userRemoved) {
  121.       for (let i = 1; i < existingQueue.length; i++) {
  122.         const tipAmount = parseFloat(existingQueue[i][4]) || 0;
  123.         if (tipAmount > 0 && tipAmount < 6) {
  124.           queueSheet.deleteRow(i + 1);
  125.           userRemoved = true;
  126.           break;
  127.         }
  128.       }
  129.     }
  130.  
  131.     if (!userRemoved && existingQueue.length > 1) {
  132.       queueSheet.deleteRow(2);
  133.     }
  134.  
  135.     const updatedQueue = queueSheet.getDataRange().getValues();
  136.     if (updatedQueue.length <= 1) {
  137.       return ContentService.createTextOutput("The queue is now empty.");
  138.     }
  139.  
  140.     const nextUser = updatedQueue[1][0];
  141.     return ContentService.createTextOutput(`Skipped to the next user. Now it's ${nextUser}'s turn!`);
  142.   }
  143.  
  144.   return ContentService.createTextOutput("Invalid action.");
  145. }
Advertisement
Add Comment
Please, Sign In to add comment