Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Chorddb is a simple, lightweight package which is a database like MongoDB which uses Discord as storage with encryption. It works with JSON data.

**NEXT UPDATE**: Image Upload (Will only work with Buffer)
**NEXT UPDATE**: Docs for image upload.

# Docs

Expand Down
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "MIT",
"dependencies": {
"aes256": "^1.1.0",
"axios": "^1.11.0"
"axios": "^1.11.0",
"form-data": "^4.0.5"
}
}
8 changes: 8 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class InvalidChannelIdError extends Error {
}
}

class InvalidImageChannel extends Error {
constructor() {
super("Invalid Images channel Id");
this.name = "InvalidImageChannel";
}
}

class ChordDBNotStartedError extends Error {
constructor() {
super("ChordDB not started");
Expand All @@ -35,4 +42,5 @@ module.exports = {
InvalidChannelIdError,
InvalidTokenError,
ChordDBNotStartedError,
InvalidImageChannel,
};
84 changes: 69 additions & 15 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
// Main \\

const { encrypt, setup, decrypt, dc_call } = require("./functions");
const FormData = require("form-data");
const {
MessageTooLargeError,
InvalidTokenError,
InvalidChannelIdError,
ChordDBNotStartedError,
InvalidImageChannel,
} = require("./errors");

class UDB {
constructor(token, encryption_key, channel_id) {
constructor(
token,
encryption_key,
channel_id,
img_channel = null,
showChorddbMessage = true,
) {
this.token = token;
this.enc_key = encryption_key;
this.ch_id = channel_id;
this.isStarted = false;
this.images = img_channel;
this.debugShow = showChorddbMessage;
}

async start() {
Expand All @@ -23,24 +33,30 @@ class UDB {
const channel_info = await dc_call(`/channels/${this.ch_id}`);

if (user_info.id) {
console.log(
`\x1b[32m chorddb: connected as ${user_info.username}\x1b[0m`,
);
if (this.debugShow) {
console.log(
`\x1b[32m chorddb: connected as ${user_info.username}\x1b[0m`,
);
}
} else {
console.error(new InvalidTokenError());
process.exit();
}

if (channel_info.id) {
console.log(
`\x1b[32m chorddb: linked to channel ${channel_info.name}\x1b[0m`,
);
if (this.debugShow) {
console.log(
`\x1b[32m chorddb: linked to channel ${channel_info.name}\x1b[0m`,
);
}
} else {
console.error(new InvalidChannelIdError());
process.exit();
}

console.log("chorddb: ChordDB is Ready.");
if (this.debugShow) {
console.log("chorddb: ChordDB is Ready.");
}
this.isStarted = true;
}

Expand All @@ -58,8 +74,7 @@ class UDB {
const enc_data = encrypt(data);

if (enc_data.length > 2000) {
console.error(new MessageTooLargeError());
return true;
throw new MessageTooLargeError();
}

const r = await dc_call(`/channels/${this.ch_id}/messages`, "POST", {
Expand Down Expand Up @@ -147,17 +162,56 @@ class UDB {
msgs = await dc_call(`/channels/${this.ch_id}/messages`);

for (const msg of msgs) {
final.push(JSON.parse(decrypt(msg.content)));
final.push(JSON.parse(decrypt(msg.content.toString())));
}

if (final == []) {
if (final.length === 0) {
return null;
} else {
return final;
}
}

async sendImg(name, key, buffer) {
let Form = new FormData();
this._checkStarted();

if (!this.images) {
throw new InvalidImageChannel();
}

Form.append("file", buffer, name);
Form.append(
"payload_json",
JSON.stringify({
content: key,
}),
);

const res = await dc_call(`/channels/${this.ch_id}/messages`, "POST", Form);

return res;
}

async findImg(key) {
this._checkStarted();

if (!this.images) {
throw new InvalidImageChannel();
}

const res = await dc_call(`/channels/${this.ch_id}/messages`);

if (res) {
for (const msg of res) {
if (msg.content === key) {
return msg.attachments[0];
}
}
} else {
return null;
}
}
}

module.exports = {
UDB,
};
module.exports = UDB;
Binary file added tests/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 19 additions & 4 deletions tests/maintest.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { UDB } = require("../src/main");
const path = require("path");
const UDB = require("../src/main");

const db = new UDB("TOKEN", "ENCRYPTION_KEY", "CHANNEL_ID");

db.start();
const db = new UDB("TOKEN", "ENCRYPTION_KEY", "CHANNEL_ID", true);

async function test() {
await db.start();

console.log("\n--- TESTING WRITE ---");
const testData = { id: 1, name: "Test Item", value: 100 };
const writeResult = await db.write(testData);
Expand All @@ -25,6 +26,20 @@ async function test() {
console.log("\n--- VERIFY EDIT ---");
const verify = await db.find({ key: "id", value: 1 });
console.log("Updated item:", verify);

console.log("\n --- Test Image Write ---");
const imgWriteResult = await db.sendImg(
"verycoolimg.png",
"coolimage",
Buffer.from(path.join(__dirname, "/image.png")),
);

console.log(imgWriteResult);

console.log("\n --- Test Image Write ---");
const imgReadResult = await db.findImg("coolimage");

console.log(imgReadResult);
}

test();
Expand Down