add fetch-points

All functions are untested
This commit is contained in:
flyingtoasters
2025-03-10 22:58:50 -04:00
parent bf6e44b620
commit 163e99cedc
2 changed files with 78 additions and 1 deletions

View File

@@ -90,6 +90,7 @@ To configure Discord to pull the latest code for a bot from GitHub, you can set
* Ensure the Service Account used for the Google Sheets API has "Editor" permissions for the spreadsheet.
* Ensure the Service Account used for the Google Sheets API has "Editor" permissions for the `links_from_discord` spreadsheet.
* Ensure the Service Account has "Reader" access to the `Card_DB` spreadsheet.
* Ensure the Google Sheets API service account has "Editor" access to the `points_fetch_log` spreadsheet.
5. Spreadsheet ID:
* Replace the placeholder YOUR_DISCORD_USERS_SPREADSHEET_ID with the actual ID of your Google Sheet.
6. Customization:
@@ -128,7 +129,17 @@ To configure Discord to pull the latest code for a bot from GitHub, you can set
* Ensures only one match is returned for ID-based searches.
* Handles cases where no matches are found.
* Returns data in the format of `header: data` for columns A, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, and R.
5. Command: `fetch-points`
* When the user types `fetch-points`, the bot retrieves the message author's Discord ID and username.
* Uses the Google Sheets API to search the spreadsheet for the Discord ID in column `A`.
* Retrieves the points from column `K` (10th index in the array).
* Formats the Mee6 command `/give-item member:discordID item:Yak Point amount:X` using the retrieved points.
* Logs the execution (add logic to send the command to the bot in Discord if necessary).
* Appends a log entry to the points_fetch_log spreadsheet, recording the username, Discord ID, and points awarded.
* Handles cases where the Discord ID doesn't exist or points are invalid (not a number).
* Spreadsheet Structures: Points Spreadsheet, Points Fetch Log
* The Mee6 command must be executed in a Discord channel where Mee6 is active. You can use the Discord API to send the command to the appropriate channel programmatically.
* Includes error handling for invalid Discord IDs, invalid points values, and connectivity issues with the Google Sheets API.
# Example Spreadsheet Structure

View File

@@ -9,6 +9,8 @@ const DISCORD_TOKEN = 'YOUR_DISCORD_BOT_TOKEN'; // Replace with your bot token
// Load Google Sheets credentials
const GOOGLE_SHEETS_CREDENTIALS = './credentials.json'; // Path to the JSON key file
const SPREADSHEET_ID = 'YOUR_SPREADSHEET_ID'; // Replace with your Google Spreadsheet ID
const POINTS_SPREADSHEET_ID = 'YOUR_POINTS_SPREADSHEET_ID'; // Replace with the spreadsheet containing points (e.g., "Card_DB")
const LOG_SPREADSHEET_ID = 'YOUR_POINTS_LOG_SPREADSHEET_ID'; // Replace with the points_fetch_log Spreadsheet ID
// Initialize the Discord client
const client = new Client({
@@ -162,6 +164,60 @@ async function checkCard(searchTerm) {
}
}
// Function to fetch points for a Discord ID
async function fetchPoints(discordId, username) {
try {
// Fetch data from the points spreadsheet
const range = 'Sheet1!A:K'; // Assuming Discord ID is in column A and points are in column K
const response = await sheets.spreadsheets.values.get({
spreadsheetId: POINTS_SPREADSHEET_ID,
range: range,
});
const rows = response.data.values;
if (!rows || rows.length === 0) {
return 'No data found in the points spreadsheet.';
}
// Find the row with the matching Discord ID
const matchingRow = rows.find(row => row[0] === discordId); // Column A is index 0
if (!matchingRow) {
return `No points found for Discord ID: ${discordId}`;
}
const points = matchingRow[10]; // Column K is index 10
if (!points || isNaN(points)) {
return `Invalid points value for Discord ID: ${discordId}`;
}
// Trigger the mee6 command
const mee6Command = `/give-item member:${discordId} item:Yak Point amount:${points}`;
console.log(`Executing Mee6 Command: ${mee6Command}`); // Replace with actual Mee6 command logic
// Note: Add logic to send the mee6 command to the correct channel in Discord if necessary
// Log the action in the points_fetch_log spreadsheet
const logRange = 'Sheet1!A:C'; // Assuming columns A, B, and C are for Username, Discord ID, and Points
const logValues = [[username, discordId, points]]; // Log the action
const logRequest = {
spreadsheetId: LOG_SPREADSHEET_ID,
range: logRange,
valueInputOption: 'USER_ENTERED',
insertDataOption: 'INSERT_ROWS',
resource: {
values: logValues,
},
};
await sheets.spreadsheets.values.append(logRequest);
return `Successfully fetched points for ${username}. They were awarded ${points} Yak Points!`;
} catch (error) {
console.error('Error fetching points:', error);
return 'There was an error fetching points.';
}
}
// Handle messages
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
@@ -216,6 +272,16 @@ client.on('messageCreate', async (message) => {
const result = await checkCard(searchTerm);
message.reply(result);
}
// Command: fetch-points
if (message.content.startsWith('fetch-points')) {
const discordId = message.author.id; // Use the message author's Discord ID
const username = message.author.username; // Get the username
// Fetch points for the user
const result = await fetchPoints(discordId, username);
message.reply(result);
}
});
// Handle Discord bot ready event