Skip to content

Commit

Permalink
Merge pull request #51 from Contraversum/deploy-test
Browse files Browse the repository at this point in the history
Deploy test
  • Loading branch information
johan-t authored Sep 23, 2023
2 parents 9b1df55 + 250f891 commit 37029cf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
10 changes: 5 additions & 5 deletions commands/test/test-command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, SlashCommandBuilder, Guild, Role } from 'discord.js';
import { client, db } from '../../index';
import { client, db } from '../../common';
import cron from 'cron';

const questions = [
Expand Down Expand Up @@ -48,7 +48,7 @@ const checkForFeedbackRequests = async () => {
const oneWeekAgo = new Date(now.getTime() - (7 * 24 * 60 * 60 * 1000));

const users = await db.db('contrabot').collection("users").find({
completionTime: {
completionTime: {
$lt: oneWeekAgo.toISOString()
},
feedbackRequestSent: { $ne: true } // This ensures that you don't ask for feedback multiple times
Expand Down Expand Up @@ -77,9 +77,9 @@ const checkForFeedbackRequests = async () => {

// Update context for this user in the database
await db.db('contrabot').collection("users").updateOne(
{ userId: user.userId },
{
$set: {
{ userId: user.userId },
{
$set: {
feedbackRequestSent: true
}
}
Expand Down
18 changes: 18 additions & 0 deletions common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Client, Collection, GatewayIntentBits } from 'discord.js';
import { MongoClient } from "mongodb";

export interface ClientWithCommands extends Client {
commands: Collection<string, any>
}

export const db = new MongoClient(process.env.MONGO_URL!);

export const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages
]
}) as ClientWithCommands;

client.commands = new Collection();
1 change: 1 addition & 0 deletions deploy-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { REST, Routes } from 'discord.js';
import 'dotenv/config'
import fs from 'fs';
import path from 'path';
import 'dotenv/config'

const commands = [];
const foldersPath = path.join(__dirname, 'commands');
Expand Down
36 changes: 10 additions & 26 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
import 'dotenv/config'
import { Client, Collection, Events, GatewayIntentBits } from 'discord.js';
import { Events } from 'discord.js';
import { sendQuestion } from './commands/test/test-command';
import { sendSurveyQuestions, Feedbackquestions } from './commands/test/startSurvey';
import { sendSurveyQuestions, Feedbackquestions } from './startSurvey';
import * as fs from 'fs';
import path from 'path'
import { MongoClient } from "mongodb";
import { google } from 'googleapis';

export const db = new MongoClient(process.env.MONGO_URL!);

interface ClientWithCommands extends Client {
commands: Collection<string, any>
}
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages
]
}) as ClientWithCommands;

import { client, db, ClientWithCommands } from './common';

client.on(Events.ClientReady, async (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
await db.connect();
});
client.login(process.env.TOKEN); // Log in to the bot

client.commands = new Collection();
// Load commands
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
Expand Down Expand Up @@ -132,7 +116,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 @@ -148,15 +132,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 @@ -167,7 +151,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 @@ -181,15 +165,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 Down
File renamed without changes.

0 comments on commit 37029cf

Please sign in to comment.