Skip to content

Commit

Permalink
fix: invite tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
greg6775 committed Oct 22, 2023
1 parent 6352bfd commit 9f6184c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
16 changes: 8 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const jwtClient = new google.auth.JWT(
process.env.CLIENT_EMAIL,
undefined,
process.env.PRIVATE_KEY,
['https://www.googleapis.com/auth/spreadsheets']
[ 'https://www.googleapis.com/auth/spreadsheets' ]
);

const sheets = google.sheets({ version: 'v4', auth: jwtClient });
Expand All @@ -144,15 +144,15 @@ client.on(Events.MessageCreate, async (message) => {
let currentFeedbackQuestionIndex = userContext?.currentFeedbackQuestionIndex || 0;

// Calculate the column where the answer should be placed.
const columnForAnswer = COLUMNS[currentFeedbackQuestionIndex + 1]; // +1 to skip the first column which might have the userID
const columnForAnswer = COLUMNS[ currentFeedbackQuestionIndex + 1 ]; // +1 to skip the first column which might have the userID

// Find the row number for the current user (assuming the user's ID is in the first column)
const response = await sheets.spreadsheets.values.get({
spreadsheetId: SHEET_ID,
range: `${START_COLUMN}:${START_COLUMN}` // search in the first column only
});
const rows = response.data.values || [];
let rowIndex = rows.findIndex((row: any) => row[0] === message.author.id.toString()) + 1; // +1 because index is 0-based and rows in Google Sheets are 1-based.
let rowIndex = rows.findIndex((row: any) => row[ 0 ] === message.author.id.toString()) + 1; // +1 because index is 0-based and rows in Google Sheets are 1-based.

// If the user is not found, create a new row for them
if (rowIndex === 0) {
Expand All @@ -163,7 +163,7 @@ client.on(Events.MessageCreate, async (message) => {
insertDataOption: 'INSERT_ROWS',
resource: {
values: [
[message.author.id] // userID in the first column
[ message.author.id ] // userID in the first column
]
}
} as any);
Expand All @@ -177,15 +177,15 @@ client.on(Events.MessageCreate, async (message) => {
valueInputOption: 'RAW',
resource: {
values: [
[message.content]
[ message.content ]
]
}
} as any);

currentFeedbackQuestionIndex++;

if (currentFeedbackQuestionIndex < Feedbackquestions.length) {
message.author.send(Feedbackquestions[currentFeedbackQuestionIndex]);
message.author.send(Feedbackquestions[ currentFeedbackQuestionIndex ]);

await db.db('contrabot').collection("users").updateOne(
{ userId: message.author.id },
Expand All @@ -212,8 +212,8 @@ client.on(Events.MessageCreate, async (message) => {
}
});

client.on('guildMemberAdd', () => {
trackInvites();
client.on('guildMemberAdd', async () => {
await trackInvites();
});

export { client, db };
9 changes: 4 additions & 5 deletions inviteTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function trackInvites() {
const invites = await guild.invites.fetch();

// Create an object to store invite data per user
const inviteData: { [key: string]: number } = {};
const inviteData: { [ key: string ]: number } = {};

// Store number of invites per inviter
invites.forEach((invite) => {
Expand All @@ -29,8 +29,7 @@ export async function trackInvites() {
const inviteCount = (invite.uses !== null ? invite.uses : 0);

// Increment the invite count for the inviter
inviteData[inviterId] = (inviteData[inviterId] || 0) + inviteCount;
console.log(inviteData);
inviteData[ inviterId ] = (inviteData[ inviterId ] || 0) + inviteCount;
}
});

Expand All @@ -45,12 +44,12 @@ export async function trackInvites() {
continue; // Skip this user and continue with others
}

let inviteCount = inviteData[userId] || 0;
let inviteCount = inviteData[ userId ] || 0;

// Update the invite count for the user in the database
await db.db('contrabot').collection('users').updateOne(
{ userId: userId },
{ $set: { inviteCount: inviteCount } }
{ $set: { inviteCount } }
);

assignRoles(inviteCount, userId, guild);
Expand Down

0 comments on commit 9f6184c

Please sign in to comment.