Compare commits

..

2 Commits

Author SHA1 Message Date
4dc8fb19fb datastore, url reset 2021-09-29 12:38:37 -07:00
bcfd056790 t 2021-09-29 11:55:07 -07:00
6 changed files with 53 additions and 15 deletions

View File

@ -18,7 +18,7 @@ module.exports = {
announcement_channel: "876010629490683955", announcement_channel: "876010629490683955",
archive_category: "887838689533771776", archive_category: "887838689533771776",
data_dir: process.cwd() + "/data/", data_dir: process.cwd() + "/data/",
web_hostname: "ldb.owo69.me", base_uri: "https://ldb.owo69.me",
world_clock: [ world_clock: [
{ {
timezone: "America/Los_Angeles", timezone: "America/Los_Angeles",

31
datastore.js Normal file
View File

@ -0,0 +1,31 @@
var fs = require("fs");
var config = require("./config");
module.exports = class DataStore {
constructor(id) {
if (id.includes('/')) throw new Error("NO");
this.dir = `${config.data_dir}/${id}/`;
if (!fs.existsSync(this.dir)) fs.mkdirSync(this.dir);
}
get(key) {
if (key.includes('/')) throw new Error("NO");
try {
return fs.readFileSync(this.dir + key, "utf8") || true;
} catch (error) {
if (error.code == "ENOENT") return false;
else throw error;
}
}
put(key, value) {
if (key.includes('/')) throw new Error("NO");
return fs.writeFileSync(this.dir + key, value || '');
}
del(key) {
if (key.includes('/')) throw new Error("NO");
try {
return fs.unlinkSync(this.dir + key);
} catch (error) {
if (error.code != "ENOENT") throw error;
}
}
}

View File

@ -1,9 +1,10 @@
var client = require("./client"); var client = require("./client");
var config = require("./config"); var config = require("./config");
var app = require("./www"); var app = require("./www");
var fs = require("fs");
var {EventEmitter} = require("events"); var {EventEmitter} = require("events");
let p = u => `${config.data_dir}/magic-channels/${u}` var DataStore = require("./datastore");
var ds = new DataStore("magic_channel");
module.exports = class MagicChannel extends EventEmitter{ module.exports = class MagicChannel extends EventEmitter{
constructor(guild, user, channelName) { constructor(guild, user, channelName) {
@ -19,10 +20,8 @@ module.exports = class MagicChannel extends EventEmitter{
}); });
if (fs.existsSync(p(this.user.id))) { let channel_id = ds.get(this.user.id + "channel");
let channel_id = fs.readFileSync(p(this.user.id)); if (channel_id) this.channel = client.channels.resolve(channel_id);
this.channel = client.channels.resolve(channel_id);
}
if (!this.channel) { if (!this.channel) {
this.guild.channels.create(channelName || this.user.username, { this.guild.channels.create(channelName || this.user.username, {
@ -42,14 +41,19 @@ module.exports = class MagicChannel extends EventEmitter{
] ]
}).then(channel => { }).then(channel => {
this.channel = channel; this.channel = channel;
fs.writeFileSync(p(this.user.id), channel.id); ds.put(this.user.id + "channel", channel.id);
channel.send(`https://${config.web_hostname}/${this.user.id}`); channel.send(`${config.base_uri}/${this.user.id}`).then(message => {
ds.put(this.user.id + "message", message.id);
});
}); });
} }
this.once("trigger", () => this.hide()); this.once("trigger", () => this.hide());
} }
getMessage() {
return this.channel.messages.fetch(ds.get(this.user.id + "message"));
}
show() { show() {
return this.channel.permissionOverwrites.edit(this.user, { return this.channel.permissionOverwrites.edit(this.user, {
@ -63,14 +67,17 @@ module.exports = class MagicChannel extends EventEmitter{
}); });
} }
reset() { async reset() {
this.show() var message = await this.getMessage();
await message.edit(`${config.base_uri}/${this.user.id}?${Math.random()}`);
this.show();
this.once("trigger", () => this.hide()); this.once("trigger", () => this.hide());
} }
destroy() { destroy() {
this.channel.delete(); ds.del(this.user.id + "channel");
fs.unlinkSync(p(this.user.id)); ds.del(this.user.id + "message");
return this.channel.delete();
} }
} }

View File

@ -65,7 +65,7 @@ async function doThing(message, user) {
console.error("avatar download", error.message); console.error("avatar download", error.message);
} }
} }
avatarURL = `https://${config.web_hostname}/avatars/${afn}` avatarURL = `${config.base_uri}/avatars/${afn}`
} else avatarURL = message.author.defaultAvatarURL; } else avatarURL = message.author.defaultAvatarURL;
let imageCandidate = message.attachments.find(a => [".png",".jpg",".jpeg",".webp",".gif"].some(e => a.url.toLowerCase().endsWith(e))); let imageCandidate = message.attachments.find(a => [".png",".jpg",".jpeg",".webp",".gif"].some(e => a.url.toLowerCase().endsWith(e)));

View File

@ -73,7 +73,7 @@ async function embedPixiv(/*message*/ channel, /*array of*/ links, send /* chann
function fallback() { function fallback() {
// save the data already downloaded // save the data already downloaded
for (let image of images) if (image.data) fs.writeFileSync(config.data_dir + "pixiv-cache/" + image.url.split('?')[0].replace(/\//g, '\\'), image.data); for (let image of images) if (image.data) fs.writeFileSync(config.data_dir + "pixiv-cache/" + image.url.split('?')[0].replace(/\//g, '\\'), image.data);
let urls = images.map(image => image.url.replace("https://i.pximg.net/", `https://${config.web_hostname}/pixiv-proxy/`)); let urls = images.map(image => image.url.replace("https://i.pximg.net/", `${config.base_uri}/pixiv-proxy/`));
if (illust.xRestrict && !channel.nsfw) urls = urls.map(url => `||${url} ||`); if (illust.xRestrict && !channel.nsfw) urls = urls.map(url => `||${url} ||`);
urls = urls.join('\n'); urls = urls.join('\n');
if (urls.length > 2000) { if (urls.length > 2000) {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 40 KiB