migrated config to .env file

This commit is contained in:
qugalet 2023-07-10 16:34:35 +03:00
parent a1806cbac9
commit 672bf10546
6 changed files with 39 additions and 32 deletions

View file

@ -1,8 +1,21 @@
import 'dotenv/config';
import { MatrixClient, MessageEvent, SimpleFsStorageProvider } from 'matrix-bot-sdk';
import { PrismaClient } from '@prisma/client';
import { readFile } from 'fs/promises';
import { join } from 'path';
declare global {
namespace NodeJS {
interface ProcessEnv {
MASTODON_ACCESS_TOKEN: string;
MASTODON_HOMESERVER_URL: string;
MATRIX_HOMESERVER_URL: string;
MATRIX_ACCESS_TOKEN: string;
MATRIX_ROOM_ID: string;
}
}
}
function parseAsyncRegexFile(file: Buffer): RegExp[] {
const data = file.toString();
return data
@ -12,18 +25,16 @@ function parseAsyncRegexFile(file: Buffer): RegExp[] {
}
async function main() {
const config = JSON.parse((await readFile(join(__dirname, '..', 'config.json'))).toString());
console.log(config);
const mastodon = await (
await import('masto')
).login({
url: config.mastodonHomeserverURL,
accessToken: config.mastodonAccessToken,
url: process.env.MASTODON_HOMESERVER_URL,
accessToken: process.env.MASTODON_ACCESS_TOKEN,
disableVersionCheck: true
});
const matrix = new MatrixClient(
config.matrixHomeserverURL,
config.matrixAccessToken,
process.env.MATRIX_HOMESERVER_URL,
process.env.MATRIX_ACCESS_TOKEN,
new SimpleFsStorageProvider('matrix_db.json')
);
const nicknameRegexes = parseAsyncRegexFile(await readFile(join(__dirname, '..', 'domains.txt')));
@ -39,7 +50,7 @@ async function main() {
) {
await mastodon.v1.accounts.block(notification.account.id);
await matrix.sendText(
config.matrixRoomId,
process.env.MATRIX_ROOM_ID,
`Юзер ${notification.account.acct} був заблокований автоматично`
);
}
@ -51,7 +62,7 @@ async function main() {
notification.status?.visibility === 'public'
) {
const matrixId = await matrix.sendText(
config.matrixRoomId,
process.env.MATRIX_ROOM_ID,
`Одобряємо?\n${notification.status.url}\nт - Так\nн - Ні`
);
await db.post.create({
@ -71,7 +82,7 @@ async function main() {
});
matrix.on('room.message', async (roomId, event) => {
if (
roomId === config.matrixRoomId &&
roomId === process.env.MATRIX_ROOM_ID &&
event['content'] &&
['т', 'н'].includes(event['content']['body'].split('\n').at(-1).trim()) &&
event['content']['m.relates_to']['m.in_reply_to']
@ -85,14 +96,14 @@ async function main() {
console.log('test');
await mastodon.v1.statuses.reblog(post.postId);
await matrix.replyText(
config.matrixRoomId,
process.env.MATRIX_ROOM_ID,
event['content']['m.relates_to']['m.in_reply_to']['event_id'],
'Пост успішно опубліковано'
);
}
if (post && event['content']['body'].split('\n').at(-1) === 'н') {
await matrix.redactEvent(
config.matrixRoomId,
process.env.MATRIX_ROOM_ID,
event['content']['m.relates_to']['event_id'],
'Не одобрили :('
);
@ -102,7 +113,7 @@ async function main() {
}
});
await matrix.replyText(
config.matrixRoomId,
process.env.MATRIX_ROOM_ID,
event['content']['m.relates_to']['m.in_reply_to']['event_id'],
'Пост відхилено'
);