Skip to content

Commit

Permalink
include a way to intentionally not send message to telegram
Browse files Browse the repository at this point in the history
or partially
  • Loading branch information
Rec0gnice committed Sep 9, 2021
1 parent 9b04563 commit cd46557
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
rules: {
"no-underscore-dangle": 'off',
"space-in-parens": 'warn',
"array-bracket-spacing": 'warn'
"array-bracket-spacing": 'warn',
"computed-property-spacing": 'warn',
},
};
19 changes: 13 additions & 6 deletions src/events/exams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,24 @@ export default async function examHandler( message: Message ) {
return;
}
if ( result === null ) {
// message was sent in different channel
// message was sent in different channel
return;
}
if ( result ) {
try {
const returnCode = await TelegramBot.sendMessage( message );
if ( returnCode === -1 ) {
const errorMessage = await sendErrorEmbed( message );
errorMessage.delete( deleteOptions );
} else {
logger.info( 'A message has been send to Telegram feed' );
let errorMessage;
switch ( returnCode ) {
case -1:
errorMessage = await sendErrorEmbed( message );
errorMessage.delete( deleteOptions );
break;
case -2:
message.react( '🔇' );
logger.info( 'Sending message to the Telegram API has been dropped intentionally' );
break;
default:
logger.info( 'A message has been send to Telegram feed' );
}
} catch ( error ) {
logger.error( `Sending message to Telegram Feed failed!\n' + ${error}` );
Expand Down
17 changes: 16 additions & 1 deletion src/extensions/telegram-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ const botConfig = {

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

const ignorePattern = /--telegram-ignore/gi;

async function sendMessage( msg: Message ): Promise< number > {
// evaluate if message should be send
let discordMessage = msg.cleanContent;
if ( discordMessage.match( ignorePattern ) ) {
const splittedMessages = discordMessage.split( ignorePattern );
if ( splittedMessages.length > 1 ) {
if ( splittedMessages[ 0 ] === '' ) {
// message is intended to be ignored
return -2;
}
discordMessage = splittedMessages[ 0 ].trim();
msg.react( '🔉' );
}
}
// there is no i18n usage here because of only german recipients
const formattedDate = msg.createdAt
.toLocaleString(
Expand All @@ -28,7 +43,7 @@ async function sendMessage( msg: Message ): Promise< number > {
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\n"${msg.cleanContent}"\n\n`
+ `Nachricht (${msg.id}):\n\n"${discordMessage}"\n\n`
+ `Folge dem Link, um direkt zur Nachricht zu gelangen:\n${msg.url}`;
try {
bot.sendMessage(
Expand Down

0 comments on commit cd46557

Please sign in to comment.