Skip to content

Commit

Permalink
Add Subs Table
Browse files Browse the repository at this point in the history
This gets the Substition table for the current day
  • Loading branch information
JonatanMGit committed Nov 27, 2021
1 parent f07b966 commit c7532f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/bot/commands/vertretung.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import needle from 'needle';
import fs from 'fs';
import { SlashCommandBuilder } from '@discordjs/builders';
const updateCookies = require('../../index');

module.exports = {
data: new SlashCommandBuilder().setName('vertretung').setDescription('Schickt den Vertretungsplan'),
async execute(interaction) {
updateCookies.updateCookies(true);
needle('get', process.env.iserv_url + 'iserv/plan/show/raw/Vertretungsplan%20Sek1%20Heute/subst_001.htm', {
cookies: JSON.parse(fs.readFileSync('./././data.json', 'utf-8')),
})
.then(function (resp) {
if (resp.statusCode !== 200) {
console.log('Login failed with statuscode ' + resp.statusCode);
return interaction.reply('Es ist ein Fehler aufgetreten');
} else {
process.env.iserv_console && console.log('Get Sub Table Sucessfull');
interaction.reply('Get Sub Table Sucessfull');
}
})
.catch(function (err) {
console.log(err);
return err;
});
return;
},
};
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ const options = {
},
};
// get Cookies from Iserv
function updateCookies(saveCookies: boolean) {
export function updateCookies(saveCookies: boolean) {
const cookie = fs.existsSync('data.json') ? JSON.parse(fs.readFileSync('data.json', 'utf-8')) : {};
// Check if cookies are saved cookies are valid
needle('get', process.env.iserv_url + 'iserv/', options)
needle('get', process.env.iserv_url + 'iserv/', { cookies: cookie })
.then(function (resp) {
if (resp.statusCode == 301) {
if (resp.statusCode == 302) {
process.env.iserv_debug && console.log('Cookies are invalid\nGetting new ones');
// incase they are invalid, get new cookies
needle('post', process.env.iserv_url + 'iserv/app/login', login, options)
Expand All @@ -28,7 +29,7 @@ function updateCookies(saveCookies: boolean) {
console.log('Unable to get cookies');
return false;
}
if (saveCookies === !false) {
if (saveCookies !== false) {
fs.writeFileSync('data.json', JSON.stringify(resp.cookies), 'utf-8');
console.log('Cookies saved');
}
Expand Down

0 comments on commit c7532f7

Please sign in to comment.