103 lines
1.5 KiB
JavaScript
103 lines
1.5 KiB
JavaScript
const {
|
|
PermissionFlagsBits,
|
|
MessageFlags
|
|
} = require("discord.js");
|
|
|
|
|
|
function getBotAuthorId() {
|
|
return process.env.BOT_AUTHOR_ID ?? null;
|
|
}
|
|
|
|
|
|
function isBotAuthor(
|
|
userId
|
|
) {
|
|
const authorId =
|
|
getBotAuthorId();
|
|
|
|
return Boolean(
|
|
authorId &&
|
|
userId &&
|
|
String(userId) === String(authorId)
|
|
);
|
|
}
|
|
|
|
|
|
function isAdministrator(
|
|
interaction
|
|
) {
|
|
return (
|
|
interaction
|
|
?.memberPermissions
|
|
?.has(
|
|
PermissionFlagsBits.Administrator
|
|
)
|
|
??
|
|
false
|
|
);
|
|
}
|
|
|
|
|
|
function canUseAdminCommands(
|
|
interaction
|
|
) {
|
|
return (
|
|
isBotAuthor(
|
|
interaction.user.id
|
|
)
|
|
||
|
|
isAdministrator(
|
|
interaction
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
async function requireAdminAccess(
|
|
interaction
|
|
) {
|
|
if (
|
|
canUseAdminCommands(
|
|
interaction
|
|
)
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
|
|
const payload = {
|
|
content:
|
|
"You don't have permission to use this command.",
|
|
|
|
flags:
|
|
MessageFlags.Ephemeral
|
|
};
|
|
|
|
|
|
if (
|
|
interaction.replied ||
|
|
interaction.deferred
|
|
) {
|
|
await interaction.followUp(
|
|
payload
|
|
);
|
|
}
|
|
|
|
else {
|
|
await interaction.reply(
|
|
payload
|
|
);
|
|
}
|
|
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
getBotAuthorId,
|
|
isBotAuthor,
|
|
isAdministrator,
|
|
canUseAdminCommands,
|
|
requireAdminAccess
|
|
}; |