xiyuhook
Version:
Simple Auto-Webhook for discord.js
73 lines (64 loc) • 2.58 kB
JavaScript
module.exports = async function (channel, message, options) {
const init = new Promise(async resolve => {
async function sendHook(hook, message, options) {
if (typeof message !== 'string' && ['RichEmbed', 'MessageEmbed'].includes(message.constructor.name)) {
options.embeds = [message];
message = null;
}
if ((options.mentions || true) !== false) {
let callback = await hook.send(message, {
username: options.name,
avatarURL: options.icon,
embeds: options.embeds
});
} else {
let callback = await hook.send(message, {
username: options.name,
avatarURL: options.icon,
embeds: options.embeds,
allowedMentions: { parse: [] }
});
}
resolve(callback);
}
async function fallback(channel, message, timer) {
channel = channel.channel || channel;
let callback = await channel.send(message)
if (timer) callback.delete({
timeout: timer
})
resolve(callback);
}
if (!channel) return console.log('XIYU_HOOK_ERROR: Invalid usage, please read the NPM page!')
channel = channel.channel || channel;
if (!channel.send || !channel.fetchWebhooks) return console.log('XIYU_HOOK_ERROR: Channel Invalid.');
if (!message) return console.log('XIYU_HOOK_ERROR: Message Invalid.');
if (!options) options = {};
options = {
delete: options.delete || false,
color: options.color || null,
name: options.name || 'Message',
icon: options.icon || undefined
}
if (isNaN(options.delete)) options.delete = false;
let sended = false;
let webhooks = await channel.fetchWebhooks().catch(err => {
sended = true;
fallback(channel, message, options.delete)
});
if(sended) return;
let hook = webhooks.find(w => w.name === 'https://discord.gg/Ysj2XRAmGm')
if (!hook) {
try {
hook = await channel.createWebhook('https://discord.gg/Ysj2XRAmGm', {
avatar: 'https://imgur.com/a/t7EQEee'
});
} catch (e) {
hook = await channel.createWebhook('https://discord.gg/Ysj2XRAmGm', 'https://imgur.com/a/t7EQEee');
}
return sendHook(hook, message, options);
}
sendHook(hook, message, options);
})
return init;
}