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 +1,6 @@
DATABASE_URL="postgresql://uabot@postgres:5432/uabot" DATABASE_URL="postgresql://uabot@postgres:5432/uabot"
MASTODON_ACCESS_TOKEN=
MASTODON_HOMESERVER_URL=
MATRIX_HOMESERVER_URL=
MATRIX_ACCESS_TOKEN=
MATRIX_ROOM_ID=

View file

@ -14,16 +14,6 @@ First, install Postgres.
Second, clone repo. Second, clone repo.
Copy `.env.example` to `.env` and edit with your data: Copy `.env.example` to `.env` and edit with your data:
```sh
DATABASE_URL="postgresql://uabot@postgres:5432/uabot"
```
Also copy `config.json.example` to `config.json` with your data:
* `mastodonClientId` - Mastodon Client Id
* `mastodonAccessToken` - Mastodon Access Token
* `mastodonHomeserverURL` - Mastodon Homeserver URL
* `matrixHomeserverURL` - Matrix Homeserver URL bot for moderation
* `matrixAccessToken` - Matrix Access Token for Matrix bot for moderation
* `matrixRoomId` - Matrix Room Id for moderation
Also create `nicknames.txt` and `domains.txt` (regex files): Also create `nicknames.txt` and `domains.txt` (regex files):
@ -36,7 +26,7 @@ Now you can install using **Dockerimage** or **manually**:
#### Docker #### Docker
Build `Dockerfile` and run Build `Dockerfile` and run
Also, mount `config.json`, `domains.txt` and `nicknames.txt` to the `/app` Also, mount `domains.txt` and `nicknames.txt` to the `/app`
### Manually ### Manually

View file

@ -1,8 +0,0 @@
{
"pleromaClientId": "",
"pleromaAccessToken": "",
"pleromaHomeserverURL": "",
"matrixHomeserverURL": "",
"matrixAccessToken": "",
"matrixRoomId": ""
}

View file

@ -23,6 +23,7 @@
}, },
"dependencies": { "dependencies": {
"@prisma/client": "4.14.1", "@prisma/client": "4.14.1",
"dotenv": "^16.3.1",
"masto": "^5.11.3", "masto": "^5.11.3",
"matrix-bot-sdk": "^0.6.6", "matrix-bot-sdk": "^0.6.6",
"prisma": "^4.16.2" "prisma": "^4.16.2"

View file

@ -8,6 +8,9 @@ dependencies:
'@prisma/client': '@prisma/client':
specifier: 4.14.1 specifier: 4.14.1
version: 4.14.1(prisma@4.16.2) version: 4.14.1(prisma@4.16.2)
dotenv:
specifier: ^16.3.1
version: 16.3.1
masto: masto:
specifier: ^5.11.3 specifier: ^5.11.3
version: 5.11.3 version: 5.11.3
@ -759,6 +762,11 @@ packages:
tslib: 2.6.0 tslib: 2.6.0
dev: false dev: false
/dotenv@16.3.1:
resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==}
engines: {node: '>=12'}
dev: false
/ecc-jsbn@0.1.2: /ecc-jsbn@0.1.2:
resolution: {integrity: sha512-eh9O+hwRHNbG4BLTjEl3nw044CkGm5X6LoaCf7LPp7UU8Qrt47JYNi6nPX8xjW97TKGKm1ouctg0QSpZe9qrnw==} resolution: {integrity: sha512-eh9O+hwRHNbG4BLTjEl3nw044CkGm5X6LoaCf7LPp7UU8Qrt47JYNi6nPX8xjW97TKGKm1ouctg0QSpZe9qrnw==}
dependencies: dependencies:

View file

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