Sunar

Owner only

Learn how to restrict command execution to only the bot owner using middleware in Sunar. This guide provides examples to enforce owner-only access.

Usage

protect(builder, [ownerOnly])

Logic

import { Message } from 'discord.js';
import { Protector, execute } from 'sunar';
 
const OWNERS = ['123', '456', '789'];
 
const ownerOnly = new Protector({
	commands: ['autocomplete', 'contextMenu', 'slash'],
	components: ['button', 'modal', 'selectMenu'],
	signals: ['interactionCreate', 'messageCreate'],
});
 
const content = 'Only the bot owners can use this.';
 
execute(ownerOnly, (arg, next) => {
	const entry = Array.isArray(arg) ? arg[0] : arg;
 
	if (entry instanceof Message) {
		const isOwner = OWNERS.includes(entry.author.id);
		if (isOwner) return next();
		return entry.reply({ content });
	}
 
	const isOwner = OWNERS.includes(entry.user.id);
 
	if (entry.isAutocomplete() && !isOwner) return entry.respond([]);
	if (entry.isRepliable() && !isOwner) return entry.reply({ content, ephemeral: true });
 
	return isOwner && next();
});
 
export { ownerOnly };

Last updated on

On this page

GitHubEdit on Github ↗