Skip to content

Commit

Permalink
Merge pull request #30 from PolestarLabs/add-await-statements
Browse files Browse the repository at this point in the history
fix(TimedUsage,commend) add await
  • Loading branch information
Flicksie authored Mar 5, 2024
2 parents a067029 + 0047e46 commit 4072f68
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 83 deletions.
120 changes: 84 additions & 36 deletions core/commands/social/commend.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
/* eslint-disable consistent-return */
const Timed = require("../../structures/TimedUsage");


const init = async function (msg, args) {
async function init(msg, args) {
const P = { lngs: msg.lang, prefix: msg.prefix };

const Target = await PLX.resolveMember(msg.guild.id, args[0], { enforceDB: true }).catch(() => {});
const Target = await PLX.resolveMember(msg.guild.id, args[0], {
enforceDB: true,
}).catch(() => {});
if (!Target) return msg.reply($t("responses.errors.kin404", P));

const userData = await DB.users.findOne({ id: msg.author.id });
const targetData = await DB.commends.parseFull({ id: Target.id }) || { id: Target.id, whoIn: [], whoOut: [] };
if (!userData || !targetData) return "Error, one of the users are not present in Database";


const targetData = (await DB.commends.parseFull({ id: Target.id })) || {
id: Target.id,
whoIn: [],
whoOut: [],
};
if (!userData || !targetData) {
return "Error, one of the users are not present in Database";
}
/*
if (!userData.personal?.ip) {
return {embed: {description: `You have to Log-in at least once in our [Dashboard](https://pollux.gg) in order to give commends!` } };
Expand All @@ -23,14 +29,24 @@ const init = async function (msg, args) {
return msg.channel.send(_emoji("nope") + $t("responses.commend.noSelf", P));
}
*/
//const hasVoted = await (Promise.promisify(PLX.topGG.hasVoted))( msg.author.id ).timeout(2000).catch(err=> true);
const hasVoted = await PLX.topGG.hasVoted( msg.author.id ).catch(err=> true);
if (!hasVoted && !userData.prime?.active){
return {embeds: [{description: `To commend globally you must have upvoted us in the last 12 hours!\n[Vote for Pollux here!](https://top.gg/bot/271394014358405121/vote)` }] };
// const hasVoted = await (Promise.promisify(PLX.topGG.hasVoted))( msg.author.id ).timeout(2000).catch(err=> true);
const hasVoted = await PLX.topGG.hasVoted(msg.author.id).catch(() => true);
if (!hasVoted && !userData.prime?.active) {
return {
embeds: [
{
description: `To commend globally you must have upvoted us in the last 12 hours!
[Vote for Pollux here!](https://top.gg/bot/271394014358405121/vote)`,
},
],
};
}

const preafter = async function preafter(M, D) {
if (userData.modules.inventory.find((itm) => itm.id === "commendtoken")?.count >= 1) {
if (
userData.modules.inventory.find((itm) => itm.id === "commendtoken")
?.count >= 1
) {
if (Target.id === msg.author.id) {
msg.channel.send(_emoji("nope") + $t("responses.commend.noSelf", P));
return false;
Expand All @@ -42,8 +58,6 @@ const init = async function (msg, args) {
return true;
};



const after = async function after(msg, Dly) {
await Promise.all([
userData.removeItem("commendtoken"),
Expand All @@ -52,20 +66,19 @@ const init = async function (msg, args) {

P.target = Target.nick || (Target.user || Target).username;
P.author = msg.member.nick || msg.author.username;
P.cmcount = (targetData.totalIn + 1) || 0;
P.cmcount = targetData.totalIn + 1 || 0;
P.pplcount = targetData.whoIn.length + 1;

const embed = new Embed()
.thumbnail(`${paths.DASH}/build/rank.png`)
.color("#3b9ea5")
.timestamp(new Date())
.description(`
.timestamp(new Date()).description(`
${$t("responses.commend.give", P)}
${$t("responses.commend.totals", P)}
`);

Progression.emit("command.commend.commit", { msg, userID: msg.author.id });
msg.channel.send({ embed });
await msg.channel.send({ embed });
};

const reject = function (msg, Daily, r) {
Expand All @@ -83,36 +96,73 @@ const init = async function (msg, args) {
P.remaining = `<t:${~~((userDaily.last + Daily.day) / 1000)}:R>`;
const embed = new Embed();
embed.setColor("#3b9ea5");
embed.description(`${_emoji("future")} ${dailyAvailable ? _emoji("online") + $t("responses.commend.check_yes", P) : _emoji("dnd") + $t("responses.commend.check_no", P)}\
\n\n:reminder_ribbon: × **${userData.modules.inventory.find((i) => i.id === "commendtoken")?.count || 0}**`);
embed.description(`${_emoji("future")} ${
dailyAvailable
? _emoji("online") + $t("responses.commend.check_yes", P)
: _emoji("dnd") + $t("responses.commend.check_no", P)
}\
\n\n:reminder_ribbon: × **${
userData.modules.inventory.find((i) => i.id === "commendtoken")
?.count || 0
}**`);
return msg.channel.send({ embed });
};
// TODO[epic=Constants Module] Replace
Timed.init(msg, "commend", { day: 3.6e+6 * 4 }, after, reject, status, preafter);
};
await Timed.init(
msg,
"commend",
{ day: 3.6e6 * 4 },
after,
reject,
status,
preafter
);
}

const info = async (msg, args) => {
const Target = await PLX.getTarget(args[0] || msg.author, msg.guild);
if (!Target) return msg.reply($t("responses.errors.kin404", P));

const targetData = await DB.commends.parseFull({ id: Target.id }) || { id: Target.id, whoIn: [], whoOut: [] };
const targetData = (await DB.commends.parseFull({ id: Target.id })) || {
id: Target.id,
whoIn: [],
whoOut: [],
};

const metas = await DB.users.find({ id: { $in: targetData.whoIn.map((u) => u.id) } }, { id: 1, meta: 1 }).sort({ amt: -1 })
const metas = await DB.users
.find(
{ id: { $in: targetData.whoIn.map((u) => u.id) } },
{ id: 1, meta: 1 }
)
.sort({ amt: -1 })
.lean()
.exec();
const commendT3 = targetData.whoIn.map((u) => ({ name: metas.find((x) => x.id === u.id)?.meta?.tag || `<@${u.id}>`, amt: u.count })).sort((c1, c2) => c2.amt - c1.amt);
const commendT3 = targetData.whoIn
.map((u) => ({
name: metas.find((x) => x.id === u.id)?.meta?.tag || `<@${u.id}>`,
amt: u.count,
}))
.sort((c1, c2) => c2.amt - c1.amt);
const embed = new Embed()
.color("#3b9ea5")
.thumbnail(`${paths.CDN}/build/rank.png`)
.description(`__**Commend Info for ${Target.mention}**__\
\n\u2003 Total Commends Received: **${targetData.totalIn || 0}**\
\n\u2003 Total Commends Given: **${targetData.totalOut || 0}**\
${commendT3.length == 0 ? ""
: `\n\n__**Top Commenders**__\
\n\u2003 ${commendT3[0] ? `**${commendT3[0].name}** > ${commendT3[0].amt}` : ""}\
\n\u2003 ${commendT3[1] ? `**${commendT3[1].name}** > ${commendT3[1].amt}` : ""}\
\n\u2003 ${commendT3[2] ? `**${commendT3[2].name}** > ${commendT3[2].amt}` : ""}`
}`);
${
commendT3.length == 0
? ""
: `\n\n__**Top Commenders**__\
\n\u2003 ${
commendT3[0] ? `**${commendT3[0].name}** > ${commendT3[0].amt}` : ""
}\
\n\u2003 ${
commendT3[1] ? `**${commendT3[1].name}** > ${commendT3[1].amt}` : ""
}\
\n\u2003 ${
commendT3[2] ? `**${commendT3[2].name}** > ${commendT3[2].amt}` : ""
}`
}`);

return { embed };
};
Expand All @@ -124,9 +174,7 @@ module.exports = {
cmd: "commend",
perms: 3,
cat: "social",
botPerms: [ "attachFiles", "embedLinks" ],
aliases: [ "com", "rec", "rep" ],
autoSubs: [
{ label: "info", gen: info },
],
botPerms: ["attachFiles", "embedLinks"],
aliases: ["com", "rec", "rep"],
autoSubs: [{ label: "info", gen: info }],
};
Loading

0 comments on commit 4072f68

Please sign in to comment.