Можно ли улучшить мой код?
Здравствуйте возможно ли улучшить данный код? под словом улучшить я говорю о :читабельности, модульности, производительности. В фронте не силен,только чистый js,сам код:
const inviteRegex = () => {
const protocol = '(?:(?:http|https)://)?';
const subdomain = '(?:www.)?';
const domain = '(?:disco|discord|discordapp).(?:com|gg|io|li|me|net|org)';
const path = '(?:/(?:invite))?/([a-z0-9-.]+)';
const regex = `(${protocol}${subdomain}(${domain}${path}))`;
return new RegExp(regex, 'i');
};
async function inviteCheck(bot, message) {
if (!message.member.hasPermission('ADMINISTRATOR') && message.channel.permissionsFor(bot.user.id).has('MANAGE_MESSAGES')) {
const check = inviteRegex().text(message.content);
if (check) {
const invites = '';
const fetchInvite = await bot.fetchInvite(message.content).catch(null);
if (fetchInvite.guild.id === message.guild.id) return false;
if (message.channel.permissionsFor(bot.user.id).has('MANAGE_MESSAGES')) {
await message.delete().catch(null);
}
message.channel.send(`${fetchInvite.guild.name}(\`${fetchInvite.guild.id}\`) ОТ ${message.author}(\`${message.author.id}\`)`);
return true;
}
return false;
}
return false;
}
Ответы (1 шт):
Автор решения: nörbörnën
→ Ссылка
Можно немного улучшить стилистически, чтобы код был более читабельным
const inviteRegex = (() => {
const protocol = '(?:(?:http|https)://)?';
const subdomain = '(?:www.)?';
const domain = '(?:disco|discord|discordapp).(?:com|gg|io|li|me|net|org)';
const path = '(?:/(?:invite))?/([a-z0-9-.]+)';
return new RegExp(`(${protocol}${subdomain}(${domain}${path}))`, 'i');
})();
async function inviteCheck(bot, message) {
const isMemberAdministrator = message.member.hasPermission('ADMINISTRATOR');
const isBootManage = message.channel.permissionsFor(bot.user.id).has('MANAGE_MESSAGES');
if (!isMemberAdministrator && isBootManage && inviteRegex.test(message.content)) {
const fetchInvite = await bot.fetchInvite(message.content).catch(null);
if (fetchInvite && fetchInvite.guild.id !== message.guild.id) {
if (isBootManage) {
await message.delete().catch(null);
}
message.channel.send(`${fetchInvite.guild.name}(\`${fetchInvite.guild.id}\`) ОТ ${message.author}(\`${message.author.id}\`)`);
return true;
}
}
return false;
}