Skip to content

Commit

Permalink
refactor: pass only reply obj to command
Browse files Browse the repository at this point in the history
So command only needs to update it when needed.

This is the first step to make commands portable.
  • Loading branch information
sntran committed Feb 12, 2021
1 parent 933655b commit 6f91522
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
8 changes: 3 additions & 5 deletions commands/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ module.exports = {
usage: "<url> [destpath]",
/**
* Downloads a link to a Google Drive folder
* @param {Discord.Message} message - The incoming chat message.
* @param {Discord.Message} reply - The reply message.
* @param {URL} url The URL to download
* @param {string} [destpath] Path to Google Drive to save file to.
*/
async execute(message, url, destpath = basename(url.pathname)) {
async execute(reply, url, destpath = basename(url.pathname)) {
let remote = "target";

// If `destpath` is a folder, append the filename from URL.
Expand All @@ -49,7 +49,7 @@ module.exports = {

let header = `**File**: ${ destpath }`;

const reply = await message.reply(`${ header }\n**Status**: Pending`);
reply.edit(`${ header }\n**Status**: Pending`);

const response = await fetch(url, {
method: "get",
Expand Down Expand Up @@ -81,7 +81,5 @@ module.exports = {

const fileId = await rcat(response.body, `${ remote }:${ destpath }`);
reply.edit(`${ header }\nhttps://drive.google.com/file/d/${ fileId }`);

return reply;
},
};
8 changes: 4 additions & 4 deletions commands/fshare.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ module.exports = {
usage: "<folder/file link> [destpath]",
/**
* Downloads an FShare link
* @param {Discord.Message} message - The incoming chat message.
* @param {Discord.Message} reply - The reply message.
* @param {URL} url The URL to download
* @param {string} [destpath] Path to Google Drive to save file to.
*/
async execute(message, url, destpath = "") {
async execute(reply, url, destpath = "") {
// Removes any search params.
url.search = "";

Expand All @@ -104,7 +104,7 @@ module.exports = {
});

debug(`Downloading ${ location }`);
const commands = /** @type { Discord.Collection } */(message.client.commands);
return commands.get("download").execute(message, new URL(location), destpath);
const commands = /** @type { Discord.Collection } */(reply.client.commands);
return commands.get("download").execute(reply, new URL(location), destpath);
},
};
12 changes: 6 additions & 6 deletions commands/mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ module.exports = {
usage: "<url>",
/**
* Handles the mirror request
* @param {Discord.Message} message - The incoming chat message.
* @param {Discord.Message} reply - The reply message.
* @param {URL} url The URL to download
*/
async execute(message, url, ...args) {
const commands = /** @type { Discord.Collection } */(message.client.commands);
async execute(reply, url, ...args) {
const commands = /** @type { Discord.Collection } */(reply.client.commands);
const { host } = url;

if (/(www\.)?fshare\.vn/.test(host)) {
return commands.get("fshare").execute(message, url, ...args);
return commands.get("fshare").execute(reply, url, ...args);
} else if (/(www\.)?(youtu\.be|youtube\.com)/.test(host)) {
return commands.get("ytdl").execute(message, url, ...args);
return commands.get("ytdl").execute(reply, url, ...args);
} else {
// Direct links.
return commands.get("download").execute(message, url, ...args);
return commands.get("download").execute(reply, url, ...args);
}
},
};
8 changes: 3 additions & 5 deletions commands/ytdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ module.exports = {
usage: "<url>",
/**
* Handles the mirror request
* @param {Discord.Message} message - The incoming chat message.
* @param {Discord.Message} reply - The reply message.
* @param {URL} url The URL to download
*/
async execute(message, url, filename = "") {
async execute(reply, url, filename = "") {
let header = `**File**: ${ filename }`;
const reply = await message.reply(`${ header }\n**Status**: Pending`);
reply.edit(`${ header }\n**Status**: Pending`);

debug(`Retrieving infomation for ${ url }`);
const info = await getInfo(url);
Expand Down Expand Up @@ -193,7 +193,5 @@ module.exports = {
const fileId = await promise;
debug(`Finished uploading ${ filename }`);
reply.edit(`${ header }\nhttps://drive.google.com/file/d/${ fileId }`);

return reply;
},
};
13 changes: 8 additions & 5 deletions discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ client.on("message", async message => {

// Acknowledge request received.
const reaction = await message.react("🕐");
const reply = await message.reply("Status: In Queued");

// Push request into our job queue.
jobs.push({ message, command, args, reaction, }, (error, reply) => {
// We only push the reply object so each command can update it if need to.
jobs.push({ reply, command, args, reaction, }, (error, reply) => {
if (error) {
message.reply(error.message);
reply.edit(`Status: Error - ${ error.message }`);
return;
}
});
Expand All @@ -118,11 +120,12 @@ client.on("message", async message => {
client.login(DISCORD_TOKEN);

// A simple worker that is run for each job.
async function worker({ message, command, args, reaction }, cb) {
async function worker({ reply, command, args, reaction }, cb) {
try {
// Removes the reaction to indicate the request being started.
await reaction.remove();
const reply = await command.execute(message, ...args);
reply.edit("Status: Starting");
reaction.remove();
await command.execute(reply, ...args);
cb(null, reply);
} catch(error) {
cb(error);
Expand Down

0 comments on commit 6f91522

Please sign in to comment.