Skip to content

Commit

Permalink
refactor: webhook
Browse files Browse the repository at this point in the history
Co-authored-by: codefactor-io <[email protected]>
  • Loading branch information
GiorgioBrux and code-factor authored Apr 4, 2024
1 parent 42f4edb commit fee4f82
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/webhooks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { WebhookClient, MessageEmbed } = require("discord.js-light");
const logging = require('./logging/logging');
const logging = require("./logging/logging");

class Webhook {
constructor(webhookUrl, type) {
Expand All @@ -12,20 +12,22 @@ class Webhook {
const webhooktoken = /[^/]*$/.exec(webhookUrl)[0];
const webhookid = webhookUrl.replace(
/^.*\/(?=[^/]*\/[^/]*$)|\/[^/]*$/g,
""
"",
);
if (
webhooktoken == null ||
webhookid == null ||
webhooktoken.length < webhookid.length ||
!/https:\/\/(ptb\.|canary\.|)(discordapp|discord)\.com\/api\/webhooks\/[0-9]+\/.+/g.test(
webhookUrl
webhookUrl,
)
) {
logging.error(`{red The ${type} webhook url is not valid. Skipping...}`);
return null;
}
logging.info(`{blueBright Using ${type} webhook with id: [${webhookid}] and token: [${webhooktoken}].}`);
logging.info(
`{blueBright Using ${type} webhook with id: [${webhookid}] and token: [${webhooktoken}].}`,
);
this.client = new WebhookClient(webhookid, webhooktoken);
this.a = 1;
}
Expand All @@ -42,7 +44,7 @@ class Webhook {
timetaken,
code,
msgurl,
webhookping_userid
webhookping_userid,
) {
if (!this.client) return;
const embed = new MessageEmbed()
Expand All @@ -63,7 +65,9 @@ class Webhook {
embeds: [embed],
})
.catch((err) => {
logging.error(`{red Tried to send nitro webhook embed but got error: ${err}.}`);
logging.error(
`{red Tried to send nitro webhook embed but got error: ${err}.}`,
);
});
}

Expand All @@ -87,7 +91,9 @@ class Webhook {
embeds: [embed],
})
.catch((err) => {
logging.error(`{red Tried to send notes webhook embed but got error: ${err}.}`);
logging.error(
`{red Tried to send notes webhook embed but got error: ${err}.}`,
);
});
}
}
Expand Down

0 comments on commit fee4f82

Please sign in to comment.