Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function doGet(request) {
- const queueSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
- const actionType = request.parameter.action;
- const username = request.parameter.user;
- const channelName = request.parameter.channel;
- const userLevel = request.parameter.userlevel;
- const tipAmount = parseFloat(request.parameter.tip) || 0;
- if (!actionType) {
- return ContentService.createTextOutput("Missing action parameter.");
- }
- // Join the queue
- if (actionType === "join") {
- const existingQueue = queueSheet.getDataRange().getValues();
- const isUserInQueue = existingQueue.some((entry) => entry[0] === username && entry[1] === channelName);
- if (isUserInQueue) {
- return ContentService.createTextOutput(`${username} is already in the queue.`);
- }
- const isSubscriber = userLevel === "subscriber";
- const timestamp = new Date();
- // Add the user to the sheet with tip amount
- queueSheet.appendRow([username, channelName, timestamp, isSubscriber, tipAmount]);
- return ContentService.createTextOutput(`${username} has been added to the queue${tipAmount >= 6 ? " (Top Tipper)" : isSubscriber ? " (Subscriber)" : ""}.`);
- }
- // Leave the queue
- if (actionType === "leave") {
- const existingQueue = queueSheet.getDataRange().getValues();
- for (let i = 1; i < existingQueue.length; i++) {
- if (existingQueue[i][0] === username && existingQueue[i][1] === channelName) {
- queueSheet.deleteRow(i + 1);
- return ContentService.createTextOutput(`${username} has been removed from the queue.`);
- }
- }
- return ContentService.createTextOutput(`${username} is not in the queue.`);
- }
- // List the current queue
- if (actionType === "list") {
- const existingQueue = queueSheet.getDataRange().getValues();
- const queueEntries = existingQueue.slice(1).filter((entry) => entry[0]);
- if (queueEntries.length === 0) {
- return ContentService.createTextOutput("The queue is currently empty.");
- }
- // Sort by priority: Top Tippers ($6 or higher), then Subscribers, then Tips < $6, then Regular Users
- queueEntries.sort((a, b) => {
- const tipA = parseFloat(a[4]) || 0;
- const tipB = parseFloat(b[4]) || 0;
- if (tipA >= 6 && tipB < 6) return -1;
- if (tipA < 6 && tipB >= 6) return 1;
- if (a[3] === true && b[3] === false) return -1;
- if (a[3] === false && b[3] === true) return 1;
- if (tipA > 0 && tipB === 0) return -1;
- if (tipA === 0 && tipB > 0) return 1;
- return new Date(a[2]) - new Date(b[2]);
- });
- const formattedQueue = queueEntries.map((entry, index) => `${index + 1}. ${entry[0]} (${entry[1]})`).join(", ");
- return ContentService.createTextOutput(`Current queue: ${formattedQueue}`);
- }
- // Manually update the tip amount
- if (actionType === "tipupdate") {
- if (userLevel !== "moderator" && userLevel !== "owner") {
- return ContentService.createTextOutput("You do not have permission to use this command.");
- }
- const existingQueue = queueSheet.getDataRange().getValues();
- for (let i = 1; i < existingQueue.length; i++) {
- if (existingQueue[i][0] === username && existingQueue[i][1] === channelName) {
- queueSheet.getRange(i + 1, 5).setValue(tipAmount);
- return ContentService.createTextOutput(`Updated tip for ${username} to $${tipAmount}.`);
- }
- }
- return ContentService.createTextOutput(`${username} is not in the queue.`);
- }
- // Skip to the next person
- if (actionType === "next") {
- if (userLevel !== "moderator" && userLevel !== "owner") {
- return ContentService.createTextOutput("You do not have permission to use this command.");
- }
- const existingQueue = queueSheet.getDataRange().getValues();
- if (existingQueue.length <= 1) {
- return ContentService.createTextOutput("The queue is currently empty.");
- }
- let userRemoved = false;
- for (let i = 1; i < existingQueue.length; i++) {
- const tipAmount = parseFloat(existingQueue[i][4]) || 0;
- if (tipAmount >= 6) {
- queueSheet.deleteRow(i + 1);
- userRemoved = true;
- break;
- }
- }
- if (!userRemoved) {
- for (let i = 1; i < existingQueue.length; i++) {
- if (existingQueue[i][3] === true) {
- queueSheet.deleteRow(i + 1);
- userRemoved = true;
- break;
- }
- }
- }
- if (!userRemoved) {
- for (let i = 1; i < existingQueue.length; i++) {
- const tipAmount = parseFloat(existingQueue[i][4]) || 0;
- if (tipAmount > 0 && tipAmount < 6) {
- queueSheet.deleteRow(i + 1);
- userRemoved = true;
- break;
- }
- }
- }
- if (!userRemoved && existingQueue.length > 1) {
- queueSheet.deleteRow(2);
- }
- const updatedQueue = queueSheet.getDataRange().getValues();
- if (updatedQueue.length <= 1) {
- return ContentService.createTextOutput("The queue is now empty.");
- }
- const nextUser = updatedQueue[1][0];
- return ContentService.createTextOutput(`Skipped to the next user. Now it's ${nextUser}'s turn!`);
- }
- return ContentService.createTextOutput("Invalid action.");
- }
Advertisement
Add Comment
Please, Sign In to add comment