Skip to content

Commit

Permalink
fix path and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
islamiaat committed Apr 17, 2023
1 parent 5bcbaad commit 65237ae
Show file tree
Hide file tree
Showing 48 changed files with 729 additions and 1,428 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"matrix_username": "Username to your Matrix account #aosus",
"matrix_password": "Password to your Matrix account #*****",
"matrix_homeserver_url": "https://matrix.org",
"matrix_access_token": "Put your matrix_access_token here", # you can generate it using "npm run generate_matrix_token"
"matrix_access_token": "Put your matrix_access_token here",
"matrix_autojoin": true,
"dataPath": "storage",
"matrix_encryption": true,
Expand Down
4 changes: 3 additions & 1 deletion discourse/CreatePosts.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import fetch from 'node-fetch';
import fs from 'fs-extra';
import path from 'path';

export default async function CreatePosts(Api_Username, title, raw, category) {

try {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let body = {

"title": title,
Expand Down
4 changes: 3 additions & 1 deletion discourse/EventPosts.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import fetch from 'node-fetch';
import fs from 'fs-extra';
import path from 'path';

export default async function EventPosts(callback) {

setInterval(async () => {

try {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let EventPostsJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, "/database/EventPosts.json"));
let response = await fetch(process.env.URL || config?.url + `/posts.json`, { method: 'GET' });
let data = await response.json();
Expand Down
4 changes: 3 additions & 1 deletion discourse/getCategories.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import fetch from 'node-fetch';
import fs from 'fs-extra';
import path from 'path';

export default async function getCategories() {

try {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let response = await fetch(process.env.URL || config?.url + `/categories.json`, { method: 'GET' });
let data = await response.json();

Expand Down
4 changes: 3 additions & 1 deletion discourse/getComment.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import fetch from 'node-fetch';
import fs from 'fs-extra';
import path from 'path';

export default async function getComment(topic_id, comment_id) {

try {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let response = await fetch(process.env.URL || config?.url + `/t/${topic_id}/posts.json`, { method: 'GET' });
let data = await response.json();

Expand Down
4 changes: 3 additions & 1 deletion discourse/get_latest_posts.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import fetch from 'node-fetch';
import fs from 'fs-extra';
import path from 'path';

export default async function get_latest_posts() {

try {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let response = await fetch(process.env.URL || config?.url + `/posts.json`, { method: 'GET' });
let data = await response.json();

Expand Down
4 changes: 3 additions & 1 deletion discourse/sendComment.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import fetch from 'node-fetch';
import fs from 'fs-extra';
import path from 'path';

export default async function sendComment(Api_Username, topic_id, raw) {

try {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let body = {

"raw": raw,
Expand Down
4 changes: 3 additions & 1 deletion discourse/sendMessagePrivate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import fetch from 'node-fetch';
import fs from 'fs-extra';
import path from 'path';

export default async function sendMessagePrivate(Api_Username, title, raw, sendTo) {

try {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let body = {
title: title,
raw: raw,
Expand Down
4 changes: 3 additions & 1 deletion matrix/EventPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import getrRoomMatrix from '../module/getrRoomMatrix.js';
import sendFile from './sendFile.js';
import fs from 'fs-extra';
import Translation from '../module/translation.js';
import path from 'path';


export default async function EventPosts_(client) {
Expand All @@ -13,7 +14,8 @@ export default async function EventPosts_(client) {

try {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let translation = await Translation(`${process.env.LANGUAGE || config?.language}`);
let name = e?.name;
let username = e?.username;
Expand Down
6 changes: 4 additions & 2 deletions matrix/EventReply.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import fs from 'fs-extra';
import sendComment from '../discourse/sendComment.js';
import Translation from '../module/translation.js';
import path from 'path';

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let translation = await Translation(`${process.env.LANGUAGE || config?.language}`);

export default async function EventReply(roomId, sender, meId, body, replySender, replyBody, event, RichReply, client) {

if (replySender?.includes(meId)) {

let topic_id = replyBody?.split(`${translation.number_topic}:</b> `)[1];
let memberJson = fs.readJsonSync(`./database/matrix/member/${sender}.json`);
let memberJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`));

if (topic_id) {

Expand Down
4 changes: 3 additions & 1 deletion matrix/generate_matrix_token.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { MatrixAuth } from "matrix-bot-sdk";
import fs from 'fs-extra';
import * as dotenv from 'dotenv'
import path from 'path';
dotenv.config({ override: true })

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let matrix_homeserver_url = process.env.MATRIX_HOMESERVER_URL || config?.matrix_homeserver_url;
let username = process.env.MATRIX_USERNAME || config?.matrix_username;
let password = process.env.MATRIX_PASSWORD || config?.matrix_password;
Expand Down
11 changes: 6 additions & 5 deletions matrix/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
matrix_autojoinRoomsMixin,
AutojoinRoomsMixin,
LogLevel,
LogService,
MatrixClient,
Expand All @@ -14,14 +14,15 @@ import menu from '../module/menu.js';
import { database_matrix, database_matrix_member } from '../module/database_matrix.js';
import EventPosts_ from './EventPosts.js';
import EventReply from './EventReply.js';
import * as path from "path";
import path from 'path';
import fs from 'fs-extra';

export default async function MatrixBot() {

try {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
LogService.setLevel(LogLevel.name);

let storage = new SimpleFsStorageProvider(path.join(process.env.DATAPATH || config?.dataPath, "matrix.json"));
Expand All @@ -34,7 +35,7 @@ export default async function MatrixBot() {
let client = new MatrixClient(process.env.MATRIX_HOMESERVER_URL || config?.matrix_homeserver_url, process.env.MATRIX_ACCESS_TOKEN || config?.matrix_access_token, storage, cryptoStore);
// Setup the matrix_autojoin mixin (if enabled)
if (process.env.MATRIX_ACCESS_TOKEN === "true" || config?.matrix_autojoin) {
matrix_autojoinRoomsMixin.setupOnClient(client);
AutojoinRoomsMixin.setupOnClient(client);
}

client.addPreprocessor(new RichRepliesPreprocessor(false));
Expand Down Expand Up @@ -102,7 +103,7 @@ export default async function MatrixBot() {

console.log(error);

if (error?.body[0]?.errcode === 'M_UNKNOWN') {
if (error?.body?.[0]?.errcode === 'M_UNKNOWN') {

console.log('Run the command "npm run generate_matrix_token" and restart the bridge');
}
Expand Down
8 changes: 5 additions & 3 deletions matrix/menu/CreatePosts/CreatePosts_1.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import fs from 'fs-extra';
import { database_matrix_member } from '../../../module/database_matrix.js';
import Translation from '../../../module/translation.js';
import path from 'path';

export default {
async exec({ meId, roomId, sender, name, checkRoom, roomIdOrAlias, body, replyBody, replySender, roomName, event_id, usersAdmin, RichReply, event, client }) {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let translation = await Translation(`${process.env.LANGUAGE || config?.language}`);

if (!isNaN(body)) {

let memberJson = fs.readJsonSync(`./database/matrix/member/${sender}.json`);
let memberJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`));
let message = `${translation.topic_title} 📝`
let reply = RichReply.createFor(roomId, event, message, message);
await client.sendMessage(roomId, reply).catch(error => console.log(error));
memberJson.CreatePosts_1 = Number(body);
fs.writeJsonSync(`./database/matrix/member/${sender}.json`, memberJson, { spaces: '\t' });
fs.writeJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`), memberJson, { spaces: '\t' });
await database_matrix_member({ sender: sender, menu: 'CreatePosts_2' }).catch(error => console.log(error));
}

Expand Down
10 changes: 6 additions & 4 deletions matrix/menu/CreatePosts/CreatePosts_2.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import fs from 'fs-extra';
import { database_matrix_member } from '../../../module/database_matrix.js';
import Translation from '../../../module/translation.js';
import path from 'path';

export default {
async exec({ meId, roomId, sender, name, checkRoom, roomIdOrAlias, body, replyBody, replySender, roomName, event_id, usersAdmin, RichReply, event, client }) {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let translation = await Translation(`${process.env.LANGUAGE || config?.language}`);

if (body) {
let memberJson = fs.readJsonSync(`./database/matrix/member/${sender}.json`);

let memberJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`));
let message = `${translation.topic_content} 📝`
let reply = RichReply.createFor(roomId, event, message, message);
await client.sendMessage(roomId, reply).catch(error => console.log(error));
memberJson.CreatePosts_2 = body;
fs.writeJsonSync(`./database/matrix/member/${sender}.json`, memberJson, { spaces: '\t' });
fs.writeJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`), memberJson, { spaces: '\t' });
await database_matrix_member({ sender: sender, menu: 'CreatePosts_3' }).catch(error => console.log(error));
}

Expand Down
6 changes: 4 additions & 2 deletions matrix/menu/CreatePosts/CreatePosts_3.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import fs from 'fs-extra';
import CreatePosts from '../../../discourse/CreatePosts.js';
import { database_matrix_member } from '../../../module/database_matrix.js';
import Translation from '../../../module/translation.js';
import path from 'path';

export default {
async exec({ meId, roomId, sender, name, checkRoom, roomIdOrAlias, body, replyBody, replySender, roomName, event_id, usersAdmin, RichReply, event, client }) {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let translation = await Translation(`${process.env.LANGUAGE || config?.language}`);

if (body) {

let memberJson = fs.readJsonSync(`./database/matrix/member/${sender}.json`);
let memberJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`));
let url = process.env.URL || config?.url
let category = memberJson?.CreatePosts_1;
let title = memberJson?.CreatePosts_2;
Expand Down
8 changes: 5 additions & 3 deletions matrix/menu/activation.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import fs from 'fs-extra';
import { database_matrix_member } from '../../module/database_matrix.js';
import Translation from '../../module/translation.js';
import path from 'path';

export default {
async exec({ meId, roomId, sender, name, checkRoom, roomIdOrAlias, body, replyBody, replySender, roomName, event_id, usersAdmin, RichReply, event, client }) {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let translation = await Translation(`${process.env.LANGUAGE || config?.language}`);

if (!isNaN(body)) {

let roomJson = fs.readJsonSync(`./database/matrix/${checkRoom}/${roomId}.json`);
let roomJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/${checkRoom}/${roomId}.json`));
let message = `${translation.active_bot} ✅`;
let reply = RichReply.createFor(roomId, event, message, message);

roomJson.evenPost = true;
roomJson.categories = Number(body)
fs.writeJsonSync(`./database/matrix/${checkRoom}/${roomId}.json`, roomJson, { spaces: '\t' });
fs.writeJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/${checkRoom}/${roomId}.json`), roomJson, { spaces: '\t' });
await client.sendMessage(roomId, reply).catch(error => console.log(error));
await database_matrix_member({ sender: sender, menu: 'main' }).catch(error => console.log(error));
}
Expand Down
8 changes: 5 additions & 3 deletions matrix/menu/discourse/discourse_1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ import fs from 'fs-extra';
import { database_matrix_member } from '../../../module/database_matrix.js';
import sendMessagePrivate from '../../../discourse/sendMessagePrivate.js';
import Translation from '../../../module/translation.js';
import path from 'path';

export default {
async exec({ meId, roomId, sender, name, checkRoom, roomIdOrAlias, body, replyBody, replySender, roomName, event_id, usersAdmin, RichReply, event, client }) {

let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let translation = await Translation(`${process.env.LANGUAGE || config?.language}`);

if (body) {

let memberJson = fs.readJsonSync(`./database/matrix/member/${sender}.json`);
let memberJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`));
let title = `${translation.verification_code}`
let raw = `${translation.verification_code_for} ${memberJson?.sender ? sender : memberJson?.name} <br><br>`;
raw += memberJson?.verification_code;
memberJson.discourse_username = body;
let Private = await sendMessagePrivate(process.env.DISCOURSE_USERNAME || config?.discourse_username, title, raw, body).catch(error => console.log(error));

fs.writeJsonSync(`./database/matrix/member/${sender}.json`, memberJson, { spaces: '\t' });
fs.writeJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`), memberJson, { spaces: '\t' });

if (Private?.errors) {
for (let item of Private?.errors) {
Expand Down
8 changes: 5 additions & 3 deletions matrix/menu/discourse/discourse_2.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import fs from 'fs-extra';
import { database_matrix_member } from '../../../module/database_matrix.js';
import Translation from '../../../module/translation.js';
import path from 'path';

export default {
async exec({ meId, roomId, sender, name, checkRoom, roomIdOrAlias, body, replyBody, replySender, roomName, event_id, usersAdmin, RichReply, event, client }) {

let memberJson = fs.readJsonSync(`./database/matrix/member/${sender}.json`);
let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let memberJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`));
let translation = await Translation(`${process.env.LANGUAGE || config?.language}`);

if (memberJson?.verification_code === body) {
Expand All @@ -15,7 +17,7 @@ export default {
let reply = RichReply.createFor(roomId, event, message, message);
await client.sendMessage(roomId, reply).catch(error => console.log(error));
memberJson.access = true;
fs.writeJsonSync(`./database/matrix/member/${sender}.json`, memberJson, { spaces: '\t' });
fs.writeJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`), memberJson, { spaces: '\t' });
await database_matrix_member({ sender: sender, menu: 'main' }).catch(error => console.log(error));
}

Expand Down
10 changes: 6 additions & 4 deletions matrix/menu/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import get_latest_posts from '../../discourse/get_latest_posts.js';
import getCategories from '../../discourse/getCategories.js';
import { database_matrix_member } from '../../module/database_matrix.js';
import Translation from '../../module/translation.js';
import path from 'path';
moment.locale('en-EN');

export default {
async exec({ meId, roomId, sender, name, checkRoom, roomIdOrAlias, body, replyBody, replySender, roomName, event_id, usersAdmin, RichReply, event, client }) {

let memberJson = fs.readJsonSync(`./database/matrix/member/${sender}.json`);
let roomJson = fs.readJsonSync(`./database/matrix/${checkRoom}/${roomId}.json`);
let config = fs.readJsonSync('./config.json');
let __dirname = path.resolve();
let config = fs.readJsonSync(path.join(__dirname, '/config.json'));
let memberJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/member/${sender}.json`));
let roomJson = fs.readJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/${checkRoom}/${roomId}.json`));
let translation = await Translation(`${process.env.LANGUAGE || config?.language}`);

if (body === '1' || body === '١' || body === 'get_latest_posts') {
Expand Down Expand Up @@ -216,7 +218,7 @@ export default {
let reply = RichReply.createFor(roomId, event, message, message);
roomJson.evenPost = true;
roomJson.categories = 0;
fs.writeJsonSync(`./database/matrix/${checkRoom}/${roomId}.json`, roomJson, { spaces: '\t' });
fs.writeJsonSync(path.join(process.env.DATAPATH || config?.dataPath, `/database/matrix/${checkRoom}/${roomId}.json`), roomJson, { spaces: '\t' });
await client.sendMessage(roomId, reply).catch(error => console.log(error));
}

Expand Down
Loading

0 comments on commit 65237ae

Please sign in to comment.