Skip to content

Commit

Permalink
enhance telegram message
Browse files Browse the repository at this point in the history
add condition to handle unwanted log messages
  • Loading branch information
Rec0gnice committed Sep 2, 2021
1 parent a530fd0 commit a2799b6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
],
rules: {
"no-underscore-dangle": 'off',
"space-in-parens": 'warn'
"space-in-parens": 'warn',
"array-bracket-spacing": 'warn'
},
};
21 changes: 11 additions & 10 deletions src/commands/admin/init-exam-channel.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
/* eslint-disable linebreak-style */
import { GuildChannel, Message } from 'discord.js';
import { WarningMessageEmbed } from '@/embeds';
import { getMessage, logger, keyv } from '@/utils';
import { Message } from 'discord.js';

export default {
name: 'init-exam-channel',
guildOnly: true,
cooldown: 0,
permissions: ['ADMINISTRATOR'],
execute(message: Message) {
permissions: [ 'ADMINISTRATOR' ],
execute( message: Message ) {
const channelId = message.content.split( ' ' )[1];

function isInvalid() {
return !(channelId && message.guild?.channels.cache.get(channelId));
const examChannel = message.guild?.channels.cache.get( channelId );
return !( channelId && examChannel );
}

if (isInvalid()) {
if ( isInvalid() ) {
message.reply(
new WarningMessageEmbed(
{
description: getMessage('command.exams.invalid') + message.content,
description: getMessage( 'command.exams.invalid' ) + message.content,
},
),
);
logger.warn(`Invalid Channel ID: "${message.content}" provided`);
logger.warn( `Invalid Channel ID: "${message.content}" provided` );
return;
}
const key = keyv('exams');
key.set('channel_id', channelId);
logger.info(`New Exam Channel ID: "${channelId}" have been persisted`);
const key = keyv( 'exams' );
key.set( 'channel_id', channelId );
logger.info( `New Exam Channel ID: "${channelId}" have been persisted` );
},
};
12 changes: 8 additions & 4 deletions src/events/exams.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable linebreak-style */
import { Message } from 'discord.js';
import { SuccessMessageEmbed, WarningMessageEmbed } from '@/embeds';
import { getMessage, logger, keyv } from '@/utils';
import { Message } from 'discord.js';

import { sendMessage } from '@/extensions/telegram-bot';

Expand All @@ -25,7 +25,7 @@ function rejectRequest( rejectedMessage: Message ) {
);
}

async function evaluate( message: Message ): Promise< boolean | undefined > {
async function evaluate( message: Message ): Promise< boolean | undefined | null > {
const examChannelId = await keyv( 'exams' ).get( 'channel_id' );

if ( !examChannelId ) {
Expand All @@ -46,13 +46,17 @@ async function evaluate( message: Message ): Promise< boolean | undefined > {
replyMessage.delete( deleteOptions );
return isRequestNotStupid;
}
return undefined;
return null;
}

export default async function examHandler( message: Message ) {
const result = await evaluate( message );
if ( typeof result === 'undefined' ) {
logger.error( 'Exam evaluation returned an unexpected error' );
logger.error( 'Message evaluation for exams returned an unexpected error' );
return;
}
if ( result === null ) {
// message was sent in different channel
return;
}
if ( result ) {
Expand Down
33 changes: 25 additions & 8 deletions src/extensions/telegram-bot.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
/* eslint-disable linebreak-style */
import { Message } from 'discord.js';
import environment from '@/environment';
import { Message } from "discord.js";

const TelegramBot = require( 'node-telegram-bot-api' );

const botConfig = {
polling: true
}
polling: true,
};

const bot = new TelegramBot( environment.telegram_bot_token, botConfig );

async function sendMessage( msg: Message ) {
const completeMessage = 'Eine neue Anfrage wurde auf dem Discord Server registriert:\n\n' + msg.cleanContent;
bot.sendMessage( environment.telegram_feed_id, completeMessage );
// there is no i18n usage here because of only german recipients
const formattedDate = msg.createdAt
.toLocaleString(
'de-DE',
// creating a seperate object and assign it as param somehow doesn't work
{
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
},
);
const channelName = msg.guild?.channels.cache.get( msg.channel.id )?.name;
const completeMessage = `Eine neue Anfrage wurde auf dem Discord Server im ${channelName}-Channel registriert:\n\n`
+ `Nickname: ${msg.author.username}\nUser: ${msg.author.tag}\nID: ${msg.author.id}\nZeitstempel: ${formattedDate} Uhr\n\n`
+ `Nachricht (${msg.id}):\n${msg.cleanContent}`;
bot.sendMessage( environment.telegram_feed_id, completeMessage );
}

export {
sendMessage
}
export default {
sendMessage,
};

0 comments on commit a2799b6

Please sign in to comment.