forked from Lea/UntisBot
move discord connection to own file
This commit is contained in:
parent
a9c3f42f17
commit
518a0d14eb
7
src/class/ConnectionModule.ts
Normal file
7
src/class/ConnectionModule.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { Connection } from './User';
|
||||||
|
import NotificationMessage from './NotificationMessage';
|
||||||
|
|
||||||
|
export default class ConnectionModule {
|
||||||
|
run: (message: NotificationMessage, connection: Connection) => void;
|
||||||
|
init: () => void;
|
||||||
|
}
|
|
@ -5,10 +5,12 @@ export default class User {
|
||||||
password: string,
|
password: string,
|
||||||
baseURL: string,
|
baseURL: string,
|
||||||
};
|
};
|
||||||
public connections: Array<{
|
public connections: Array<Connection>;
|
||||||
type: string,
|
|
||||||
userID: string,
|
|
||||||
channelID: string,
|
|
||||||
}>;
|
|
||||||
public ID: string;
|
public ID: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class Connection {
|
||||||
|
type: string;
|
||||||
|
userID: string;
|
||||||
|
channelID: string;
|
||||||
|
}
|
57
src/connections/discord.ts
Normal file
57
src/connections/discord.ts
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
import Discord from 'discord.js';
|
||||||
|
const client = new Discord.Client();
|
||||||
|
|
||||||
|
import User, { Connection } from '../class/User';
|
||||||
|
import NotificationMessage from '../class/NotificationMessage';
|
||||||
|
|
||||||
|
let run = async (message: NotificationMessage, connection: Connection) => {
|
||||||
|
if (!process.env.BOT_TOKEN) return; // Only run when client is enabled
|
||||||
|
if (!client.readyAt) await new Promise(r => client.once('ready', r)) as void; // Wait for client to log in
|
||||||
|
|
||||||
|
try {
|
||||||
|
const channel = await client.channels.fetch(connection.channelID).catch(()=>{}) as Discord.DMChannel;
|
||||||
|
if (!channel) return console.log(`Could not find channel: ${channel?.id}`);
|
||||||
|
|
||||||
|
let embed = new Discord.MessageEmbed()
|
||||||
|
.setAuthor(message.title)
|
||||||
|
.setDescription(message.body)
|
||||||
|
.setColor(message.color);
|
||||||
|
|
||||||
|
if (message.footer) embed.setFooter(message.footer);
|
||||||
|
|
||||||
|
// Couldn't figure out how to attach the images in the assets folder, so I use hardcoded URLs instead
|
||||||
|
if (message.isImportant) {
|
||||||
|
embed.setAuthor(embed.author.name, 'https://cdn.discordapp.com/attachments/637695358373199879/824676476405940284/info.png');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.isError) {
|
||||||
|
embed.color = 0xff0000;
|
||||||
|
embed.setAuthor(embed.author.name, 'https://cdn.discordapp.com/attachments/637695358373199879/824676465051828334/error.png' );
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!embed.timestamp) embed.setTimestamp();
|
||||||
|
channel.send(embed)
|
||||||
|
.catch(console.error);
|
||||||
|
} catch(e) {
|
||||||
|
console.warn(e);
|
||||||
|
|
||||||
|
// If error didn't occur while sending an error message, attempt to send an error to the user.
|
||||||
|
if (!message.isError) {
|
||||||
|
let msg = new NotificationMessage()
|
||||||
|
.setTitle('Error during delivery')
|
||||||
|
.setBody(`\`\`\`js\n${e}\`\`\``);
|
||||||
|
|
||||||
|
run(msg.error(), connection)
|
||||||
|
.catch(console.warn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let init = async () => {
|
||||||
|
if (!process.env.BOT_TOKEN) return console.log('Discord: No token specified');
|
||||||
|
|
||||||
|
client.login(process.env.BOT_TOKEN);
|
||||||
|
client.on('ready', () => console.log(`Discord Bot: Logged in as ${client.user.tag}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
export { run, init };
|
49
src/index.ts
49
src/index.ts
|
@ -1,17 +1,12 @@
|
||||||
import * as dotenv from 'dotenv';
|
import * as dotenv from 'dotenv';
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
import Discord from 'discord.js';
|
|
||||||
import Enmap from 'enmap';
|
import Enmap from 'enmap';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import * as WebUntis from 'webuntis';
|
import * as WebUntis from 'webuntis';
|
||||||
import User from './class/User';
|
import User from './class/User';
|
||||||
import NotificationMessage from './class/NotificationMessage';
|
import NotificationMessage from './class/NotificationMessage';
|
||||||
|
import ConnectionModule from './class/ConnectionModule';
|
||||||
console.log('Discord Bot: Logging in');
|
|
||||||
const client = new Discord.Client();
|
|
||||||
client.login(process.env.BOT_TOKEN);
|
|
||||||
client.on('ready', () => console.log(`Discord Bot: Logged in as ${client.user.tag}`));
|
|
||||||
|
|
||||||
const seenMessages = new Enmap({ name: "seenMessages" });
|
const seenMessages = new Enmap({ name: "seenMessages" });
|
||||||
const knownLessons = new Enmap({ name: "knownLessons" });
|
const knownLessons = new Enmap({ name: "knownLessons" });
|
||||||
|
@ -66,33 +61,15 @@ setInterval(fetchUpdates, 60000);
|
||||||
|
|
||||||
async function sendMessage(message: NotificationMessage, user: User) {
|
async function sendMessage(message: NotificationMessage, user: User) {
|
||||||
user.connections.forEach(async connection => {
|
user.connections.forEach(async connection => {
|
||||||
switch(connection.type) {
|
let path = require('path').join(__dirname, 'connections', connection.type + '.js') as string;
|
||||||
case 'discord':
|
if (fs.existsSync(path)) {
|
||||||
const channel = await client.channels.fetch(connection.channelID).catch(()=>{}) as Discord.DMChannel;
|
try {
|
||||||
if (!channel) return console.log(`Could not find channel: ${channel?.id}`);
|
require(path).run(message, connection);
|
||||||
|
} catch(e) {
|
||||||
let embed = new Discord.MessageEmbed()
|
console.warn(e);
|
||||||
.setAuthor(message.title)
|
}
|
||||||
.setDescription(message.body)
|
} else {
|
||||||
.setColor(message.color);
|
console.log(`Unknown connection type: ${connection.type}`);
|
||||||
|
|
||||||
if (message.footer) embed.setFooter(message.footer);
|
|
||||||
|
|
||||||
// Couldn't figure out how to attach the images in the assets folder, so I use hardcoded URLs instead
|
|
||||||
if (message.isImportant) {
|
|
||||||
embed.setAuthor(embed.author.name, 'https://cdn.discordapp.com/attachments/637695358373199879/824676476405940284/info.png');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.isError) {
|
|
||||||
embed.color = 0xff0000;
|
|
||||||
embed.setAuthor(embed.author.name, 'https://cdn.discordapp.com/attachments/637695358373199879/824676465051828334/error.png' );
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!embed.timestamp) embed.setTimestamp();
|
|
||||||
channel.send(embed)
|
|
||||||
.catch(console.error);
|
|
||||||
break;
|
|
||||||
default: console.log(`Unknown connection type: ${connection.type}`);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -100,7 +77,6 @@ async function sendMessage(message: NotificationMessage, user: User) {
|
||||||
export default {
|
export default {
|
||||||
untisInstances,
|
untisInstances,
|
||||||
usersDB,
|
usersDB,
|
||||||
bot: client,
|
|
||||||
sendEmbed: sendMessage,
|
sendEmbed: sendMessage,
|
||||||
db: {
|
db: {
|
||||||
seenMessages: seenMessages,
|
seenMessages: seenMessages,
|
||||||
|
@ -109,6 +85,11 @@ export default {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Initialize clients in dist/connections/*
|
||||||
|
fs.readdirSync(`${__dirname}/connections`).filter(file => file.endsWith('.js')).forEach(file => {
|
||||||
|
(require(`${__dirname}/connections/${file}`) as ConnectionModule).init();
|
||||||
|
});
|
||||||
|
|
||||||
// Run tasks in dist/tasks/*
|
// Run tasks in dist/tasks/*
|
||||||
fs.readdirSync(`${__dirname}/tasks`).filter(file => file.endsWith('.js')).forEach(file =>
|
fs.readdirSync(`${__dirname}/tasks`).filter(file => file.endsWith('.js')).forEach(file =>
|
||||||
require(`${__dirname}/tasks/${file}`));
|
require(`${__dirname}/tasks/${file}`));
|
|
@ -2,7 +2,7 @@ import * as WebUntis from 'webuntis';
|
||||||
import main from '../index';
|
import main from '../index';
|
||||||
import User from '../class/User';
|
import User from '../class/User';
|
||||||
import NotificationMessage from '../class/NotificationMessage';
|
import NotificationMessage from '../class/NotificationMessage';
|
||||||
const { bot, untisInstances, db, sendEmbed } = main;
|
const { db, sendEmbed } = main;
|
||||||
|
|
||||||
export async function run(untis: WebUntis.default, user: User) {
|
export async function run(untis: WebUntis.default, user: User) {
|
||||||
let news = await untis.getNewsWidget(new Date(), true);
|
let news = await untis.getNewsWidget(new Date(), true);
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as WebUntis from 'webuntis';
|
||||||
import main from '../index';
|
import main from '../index';
|
||||||
import User from '../class/User';
|
import User from '../class/User';
|
||||||
import NotificationMessage from '../class/NotificationMessage';
|
import NotificationMessage from '../class/NotificationMessage';
|
||||||
const { bot, untisInstances, db, sendEmbed } = main;
|
const { db, sendEmbed } = main;
|
||||||
|
|
||||||
export async function run(untis: WebUntis.default, user: User) {
|
export async function run(untis: WebUntis.default, user: User) {
|
||||||
let timetable = await untis.getOwnTimetableForRange(new Date(Date.now() - 86400000), new Date(Date.now() + (86400000 * 7)), true);
|
let timetable = await untis.getOwnTimetableForRange(new Date(Date.now() - 86400000), new Date(Date.now() + (86400000 * 7)), true);
|
||||||
|
|
Loading…
Reference in a new issue