migrated config to .env file
This commit is contained in:
parent
a1806cbac9
commit
672bf10546
6 changed files with 39 additions and 32 deletions
|
@ -1 +1,6 @@
|
|||
DATABASE_URL="postgresql://uabot@postgres:5432/uabot"
|
||||
MASTODON_ACCESS_TOKEN=
|
||||
MASTODON_HOMESERVER_URL=
|
||||
MATRIX_HOMESERVER_URL=
|
||||
MATRIX_ACCESS_TOKEN=
|
||||
MATRIX_ROOM_ID=
|
12
README.md
12
README.md
|
@ -14,16 +14,6 @@ First, install Postgres.
|
|||
Second, clone repo.
|
||||
|
||||
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):
|
||||
|
||||
|
@ -36,7 +26,7 @@ Now you can install using **Dockerimage** or **manually**:
|
|||
|
||||
#### Docker
|
||||
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
|
||||
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
{
|
||||
"pleromaClientId": "",
|
||||
"pleromaAccessToken": "",
|
||||
"pleromaHomeserverURL": "",
|
||||
"matrixHomeserverURL": "",
|
||||
"matrixAccessToken": "",
|
||||
"matrixRoomId": ""
|
||||
}
|
|
@ -23,6 +23,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "4.14.1",
|
||||
"dotenv": "^16.3.1",
|
||||
"masto": "^5.11.3",
|
||||
"matrix-bot-sdk": "^0.6.6",
|
||||
"prisma": "^4.16.2"
|
||||
|
|
|
@ -8,6 +8,9 @@ dependencies:
|
|||
'@prisma/client':
|
||||
specifier: 4.14.1
|
||||
version: 4.14.1(prisma@4.16.2)
|
||||
dotenv:
|
||||
specifier: ^16.3.1
|
||||
version: 16.3.1
|
||||
masto:
|
||||
specifier: ^5.11.3
|
||||
version: 5.11.3
|
||||
|
@ -759,6 +762,11 @@ packages:
|
|||
tslib: 2.6.0
|
||||
dev: false
|
||||
|
||||
/dotenv@16.3.1:
|
||||
resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==}
|
||||
engines: {node: '>=12'}
|
||||
dev: false
|
||||
|
||||
/ecc-jsbn@0.1.2:
|
||||
resolution: {integrity: sha512-eh9O+hwRHNbG4BLTjEl3nw044CkGm5X6LoaCf7LPp7UU8Qrt47JYNi6nPX8xjW97TKGKm1ouctg0QSpZe9qrnw==}
|
||||
dependencies:
|
||||
|
|
35
src/index.ts
35
src/index.ts
|
@ -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'],
|
||||
'Пост відхилено'
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue