-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Reimplemented bots - Merge pull request #119 from tjarbo/feature/is…
…sue-116 Reimplementing bots
- Loading branch information
Showing
34 changed files
with
547 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
packages/backend/src/configuration/__mocks__/environment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum ConnectorLogType { | ||
Info = 'info', | ||
Warning = 'warning', | ||
Error = 'error' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { ConnectorLogType } from '.'; | ||
import { loggerFile } from '../../configuration/logger'; | ||
import { ConnectorLogItem } from './schemas/connectorLogItem.schema'; | ||
|
||
class ConnectorLogger { | ||
|
||
/** | ||
* Creates object with message and connector attribute for ConnectorLogItem | ||
* | ||
* @param message message that needs to be stored | ||
* @param objectId objectId of the connector | ||
* @returns object | ||
*/ | ||
private createContent(message: string, objectId: string): { [key: string]: string } { | ||
return { | ||
connector: objectId, | ||
message | ||
}; | ||
} | ||
|
||
/** | ||
* Prints and stores an info message | ||
* | ||
* @param message message that needs to be stored | ||
* @param objectId objectId of the connector | ||
* @param skipSave default: true - skips adding the message to ConnectorLogs | ||
* @returns void | ||
*/ | ||
public info(message: string, objectId: string, skipSave: boolean = false): void { | ||
loggerFile.info(message); | ||
|
||
if (skipSave) return; | ||
|
||
const content = this.createContent(message, objectId); | ||
new ConnectorLogItem({ ...content, type: ConnectorLogType.Info }).save(); | ||
} | ||
|
||
/** | ||
* Prints and stores a warning message | ||
* | ||
* @param message message that needs to be stored | ||
* @param objectId objectId of the connector | ||
* @param skipSave default: true - skips adding the message to ConnectorLogs | ||
* @returns void | ||
*/ | ||
public warn(message: string, objectId: string, skipSave: boolean = false): void { | ||
loggerFile.warn(message); | ||
|
||
if (skipSave) return; | ||
|
||
const content = this.createContent(message, objectId); | ||
new ConnectorLogItem({ ...content, type: ConnectorLogType.Warning }).save(); | ||
} | ||
|
||
/** | ||
* Prints and stores an error message | ||
* | ||
* @param message message that needs to be stored | ||
* @param objectId objectId of the connector | ||
* @param skipSave default: true - skips adding the message to ConnectorLogs | ||
* @returns void | ||
*/ | ||
public error(message: string, objectId: string, skipSave: boolean = false): void { | ||
loggerFile.error(message); | ||
|
||
if (skipSave) return; | ||
|
||
const content = this.createContent(message, objectId); | ||
new ConnectorLogItem({ ...content, type: ConnectorLogType.Error }).save(); | ||
} | ||
} | ||
|
||
// This step is not required, because all functions are static | ||
// But the usage should be similar to the loggerFile object | ||
export const connectorLogger = new ConnectorLogger(); |
57 changes: 57 additions & 0 deletions
57
packages/backend/src/controllers/connectors/plugins/connectorPlugin.class.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { IConnectorDocument } from '../schemas/connector.schema'; | ||
import { ConnectorLogItem, IConnectorLogItemDocument } from '../schemas/connectorLogItem.schema'; | ||
|
||
export abstract class ConnectorPlugin { | ||
|
||
protected abstract document: IConnectorDocument; | ||
|
||
public abstract send(message: string): void; | ||
public abstract update(body: { [key: string]: any }): Promise<IConnectorDocument>; | ||
|
||
/** | ||
* Returns an array of the newest log items of this connector. | ||
* The amount of items can be limited by parameter. Default is 50. | ||
* | ||
* @param {number} [limit=50] Set a limit to the amount of items | ||
* @return {Promise<IConnectorLogItemDocument[]>} Array of ConnectorLogItemDocuments | ||
* @memberof ConnectorPlugin | ||
*/ | ||
public async getLogs(limit: number = 50): Promise<IConnectorLogItemDocument[]> { | ||
const query = { | ||
connector: this.objectId | ||
}; | ||
return await ConnectorLogItem.find(query).sort({ createdAt: -1 }).limit(limit); | ||
} | ||
|
||
/** | ||
* Returns the mongoDB objectId, this connector is build on. | ||
* | ||
* @readonly | ||
* @type {string} | ||
* @memberof ConnectorPlugin | ||
*/ | ||
public get objectId(): string { return this.document.id; } | ||
|
||
/** | ||
* Returns all courses, that are assigned to this bot. | ||
* | ||
* @readonly | ||
* @type {{ [key: string]: string; }[]} | ||
* @memberof ConnectorPlugin | ||
*/ | ||
public get courses(): number[] { | ||
return this.document.courses; | ||
} | ||
|
||
/** | ||
* Returns true, if this plugin is an default handler for not | ||
* assigned courses. | ||
* | ||
* @readonly | ||
* @type {boolean} | ||
* @memberof ConnectorPlugin | ||
*/ | ||
public get isDefault(): boolean { | ||
return this.document.default; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
packages/backend/src/controllers/connectors/plugins/discordBot.class.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import Discord, { TextChannel } from 'discord.js'; | ||
import { config } from '../../../configuration/environment'; | ||
import { IConnectorDocument } from '../schemas/connector.schema'; | ||
import { connectorLogger } from '../logger'; | ||
import { ConnectorPlugin } from './connectorPlugin.class'; | ||
import { object, ObjectSchema, string } from '@hapi/joi'; | ||
import { ApiError } from '../../../utils/api'; | ||
|
||
export class DiscordBotConnectorPlugin extends ConnectorPlugin { | ||
private readonly client: Discord.Client = new Discord.Client(); | ||
private readonly updateRequestSchema: ObjectSchema = object({ | ||
channel: string().alphanum().length(18), | ||
}).required(); | ||
|
||
/** | ||
* Creates an instance of DiscordBotConnectorPlugin. | ||
* | ||
* @param {IConnectorDocument} document mongoose document | ||
* @memberof DiscordBotConnectorPlugin | ||
*/ | ||
constructor(protected document: IConnectorDocument) { | ||
super(); | ||
|
||
this.setUpListeners(); | ||
this.client.login(config.discordToken); | ||
} | ||
|
||
/** | ||
* Creates all listeners to provide better overview about the health of the bot. | ||
* | ||
* @private | ||
* @memberof DiscordBotConnectorPlugin | ||
*/ | ||
private setUpListeners(): void { | ||
this.client.once('ready', () => { | ||
connectorLogger.info(`Logged in as ${this.client.user.tag}!`, this.objectId); | ||
}); | ||
|
||
this.client.on('warn', (info) => { | ||
connectorLogger.warn(`discord.js: ${info}`, this.objectId); | ||
}); | ||
|
||
this.client.on('disconnect', (info) => { | ||
connectorLogger.error(`discord.js: ${info}`, this.objectId); | ||
}); | ||
} | ||
|
||
/** | ||
* Sends the given message to the discord channel | ||
* | ||
* @param {string} message to send | ||
*/ | ||
public send(message: string): void { | ||
const discordChannel = this.client.channels.cache.get(this.document.socket.channel); | ||
if (!discordChannel) return connectorLogger.error(`Channel not in discord cache. Send a small 'test' message to the channel and try again.`, this.objectId); | ||
|
||
(discordChannel as TextChannel).send(message) | ||
.then(() => { | ||
connectorLogger.info('Successfully sent message via Discord bot!', this.objectId); | ||
}) | ||
.catch((error) => { | ||
connectorLogger.info(`Failed to send message via Discord bot! ${error.message}`, this.objectId); | ||
}); | ||
} | ||
|
||
/** | ||
* Applies the given patch to the discord bot document. | ||
* | ||
* @param {{ [key: string]: any }} body | ||
* @return {Promise<IConnectorDocument>} The updated document | ||
* @memberof DiscordBotConnectorPlugin | ||
*/ | ||
public async update(body: { [key: string]: any }): Promise<IConnectorDocument> { | ||
// Validate user input | ||
const updateRequest = this.updateRequestSchema.validate(body); | ||
if (updateRequest.error) throw new ApiError(400, updateRequest.error.message); | ||
|
||
// Apply changes | ||
this.document.socket.channel = updateRequest.value.channel; | ||
const result = await this.document.save(); | ||
|
||
// Log update process | ||
connectorLogger.info('New values have been applied', this.objectId); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export { DiscordBotConnectorPlugin } from './discordBot.class'; | ||
export { ConnectorPlugin }Â from './connectorPlugin.class'; | ||
|
||
export enum ConnectorType { | ||
Discord = 'discord', | ||
} |
Oops, something went wrong.