add interviewer database and self-signup functionality

This commit is contained in:
alex 2021-05-26 21:26:26 -04:00
parent f50e1ba47a
commit 5affdb97ad
3 changed files with 46 additions and 13 deletions

View File

@ -1,22 +1,22 @@
import sqlite3 = require('sqlite3')
import { open, Database } from 'sqlite'
import Discord from 'discord.js'
import sqlite3 = require('sqlite3');
import { open, Database } from 'sqlite';
import Discord from 'discord.js';
let db : Database | null = null;
let db: Database | null = null;
export async function openDB () {
if(db == null){
export async function openDB() {
if (db == null) {
db = await open({
filename: './db/bot.db',
driver: sqlite3.Database
})
await db.run('CREATE TABLE IF NOT EXISTS saved_data (msg_id INTEGER PRIMARY KEY,data TEXT NOT NULL);')
});
await db.run('CREATE TABLE IF NOT EXISTS saved_data (msg_id INTEGER PRIMARY KEY,data TEXT NOT NULL);');
}
return db;
}
export async function testDb(message: Discord.Message, command: string, args: string[]){
switch(command){
export async function testDb(message: Discord.Message, command: string, args: string[]) {
switch (command) {
case 'save':
if (args.length < 1) {
await message.channel.send('no args');
@ -35,7 +35,7 @@ export async function testDb(message: Discord.Message, command: string, args: st
.setTitle('Database Dump')
.setURL('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
const res = await db.all('SELECT * FROM saved_data');
for(const rows of res){
for (const rows of res) {
console.log(rows['msg_id'], rows['data']);
outEmbed = outEmbed.addField(rows['msg_id'], rows['data'], true);
console.log(outEmbed);
@ -65,4 +65,4 @@ export async function testDb(message: Discord.Message, command: string, args: st
}
}
console.log('connected to db')
console.log('connected to db');

29
components/interview.ts Normal file
View File

@ -0,0 +1,29 @@
import { openDB } from './db';
import Discord from 'discord.js';
export async function initInterview() {
await openDB().then(async (db) => {
await db.run('CREATE TABLE IF NOT EXISTS Interviewers (UserId INTEGER PRIMARY KEY, Name TEXT, Link TEXT)');
});
}
export async function addInterviewer(message: Discord.Message, args: string[]) {
const id = message.author.id;
const name = args.shift();
const link = args.shift();
if (!name || !link) {
await message.channel.send('Missing arguments.');
return;
}
await openDB().then(async (db) => {
await db.get('SELECT * FROM Interviewers WHERE UserID = ?', id).then(async (res) => {
if (res == undefined) {
await db.run('INSERT INTO Interviewers (UserId, Name, Link) VALUES(? , ? , ?)', id, name, link);
await message.channel.send(`<@${id}>, your info has been added.`);
} else {
await db.run('UPDATE Interviewers SET Name = ?, Link = ? WHERE UserID = ?', name, link, id);
await message.channel.send(`<@${id}, your info has been changed.`);
}
});
});
}

View File

@ -4,6 +4,7 @@ dotenv.config();
import Discord from 'discord.js';
import _ from 'lodash';
import { openDB, testDb } from './components/db';
import { addInterviewer, initInterview } from './components/interview';
import logger from './logger';
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID || '.';
@ -45,6 +46,9 @@ const handleCommand = async (message: Discord.Message, command: string, args: st
case 'ping':
await message.channel.send('pong');
break;
case 'interviewer':
addInterviewer(message, args);
break;
}
//dev testing
@ -83,7 +87,7 @@ const startBot = async () => {
const notif = (await client.channels.fetch(NOTIF_CHANNEL_ID)) as Discord.TextChannel;
notif.send('Codey is up!');
});
initInterview();
client.on('message', handleMessage);
client.login(BOT_TOKEN);