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 0f35fae commit 77186cc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
60 changes: 37 additions & 23 deletions src/commands/other/exams.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import { SuccessMessageEmbed, WarningMessageEmbed } from '@/embeds';
import { getMessage, logger } from '@/utils';
import { getMessage, logger, keyv } from '@/utils';
import { Message } from 'discord.js';
import { Guild } from '@/models';

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,10 +38,22 @@ 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' )
}
Expand Down
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;
14 changes: 6 additions & 8 deletions src/extensions/telegram-bot.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
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 );
export const bot = new TelegramBot( environment.telegram_bot_token, botConfig );

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 );
}
const completeMessage = 'Eine neue Anfrage wurde auf dem Discord Server registriert:\n\n' + msg.cleanContent;
bot.sendMessage( environment.telegram_feed_id, completeMessage );
}

0 comments on commit 77186cc

Please sign in to comment.