Skip to content

Commit

Permalink
refactor evironment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Rec0gnice committed Jul 10, 2021
1 parent e7b7a51 commit 5655c2c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ KEYV_URI=redis://localhost:6379

# telegram-bot
TELEGRAM_BOT_TOKEN=
TELEGRAM_FEED_ID=
TELEGRAM_FEED_ID=
2 changes: 1 addition & 1 deletion src/commands/admin/init-exam-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export default {
key.set( 'channel_id', channelId );
logger.info( `New Exam Channel ID: "${channelId}" have been persisted` );
}
}
}
61 changes: 37 additions & 24 deletions src/commands/other/exams.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { SuccessMessageEmbed, WarningMessageEmbed } from '@/embeds';
import { getMessage, logger } from '@/utils';
import { getMessage, logger, keyv } from '@/utils';
import { Message } from 'discord.js';

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

const deleteOptions = {
timeout: 30000
}

function acceptRequest( message: Message ) {
return message.reply(
new SuccessMessageEmbed(
{
description: getMessage( 'command.exams.success' )
} ) );
}

function rejectRequest( message: Message ) {
return message.reply(
new WarningMessageEmbed(
{
description: getMessage( 'command.exams.not_allowed' )
} ) );
}

async function evaluate( message: Message ) {
const isRequestNotStupid = !( message.cleanContent.match( /(höma|höhere|hö\sma|mathe)+/gi ) );

const deleteOptions = {
timeout: 30000
}

function acceptRequest( message: Message ) {
return message.reply(
new SuccessMessageEmbed(
{
description: getMessage( 'command.exams.success' )
} ) );
}

function rejectRequest( message: Message ) {
return message.reply(
new WarningMessageEmbed(
{
description: getMessage( 'command.exams.not_allowed' )
} ) );
}

const replyPromise = isRequestNotStupid ? acceptRequest( message ) : rejectRequest( message );
const replyMessage = await Promise.resolve( replyPromise );
replyMessage.delete( deleteOptions );
Expand All @@ -36,11 +37,23 @@ export default {
name: 'exams',
guildOnly: true,
cooldown: 0,
permissions: [ 'SEND_MESSAGES' ],
aliases: [ 'exam', 'klausur', 'klausuren', 'altklausuren', 'ak' ],
async execute( message: Message ) {
const examChannelId = await keyv( 'exams' ).get( 'channel_id' );
if ( message.channel.id !== examChannelId ) {
return;
}

if ( await evaluate( message ) ) {
sendMessage( message );
logger.info( 'A message has been send to Telegram feed' );
try {
sendMessage( message );
logger.info( 'A message has been send to Telegram feed' );
}
catch ( error ) {
logger.error( 'Sending message to Telegram Feed failed!\n' + error );
}
}
logger.warn( 'An unnecessary request has been dropped' )
}
}
}
6 changes: 5 additions & 1 deletion src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ interface Environment {
mongodb_uri: string
sentry_dsn: string
keyv_uri: string
shard: number
shard: number,
telegram_bot_token: string,
telegram_feed_id: number
}

const environment: Environment = {
Expand All @@ -18,6 +20,8 @@ const environment: Environment = {
sentry_dsn: process.env.SENTRY_DSN || '',
keyv_uri: process.env.KEYV_URI || '',
shard: Number(process.env.SHARD) || 0,
telegram_bot_token: process.env.TELEGRAM_BOT_TOKEN ||'',
telegram_feed_id: Number( process.env.TELEGRAM_FEED_ID ) || -1
};

export default environment;
18 changes: 10 additions & 8 deletions src/extensions/telegram-bot.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import environment from '@/environment';
import { Message } from "discord.js";
import { keyv } from "@/utils";
//import { keyv } from "@/utils";

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

const token = process.env.TELEGRAM_BOT_TOKEN;

const botConfig = {
polling: true
}

export const bot = new TelegramBot( token, botConfig );
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 );
}

export async function sendMessage( msg: Message ) {
const channelId = keyv( 'exams' ).get( 'channel_id' );
const completeMessage = 'Eine neue Anfrage wurde auf dem Discord Server registriert:\n\n' + msg;
bot.sendMessage( channelId, completeMessage );
export {
sendMessage
}

0 comments on commit 5655c2c

Please sign in to comment.