Permissions package, the following + canRun command
parent
38ccb6e9f6
commit
5a3ac763eb
@ -1,49 +1,62 @@
|
||||
package net.pingex.dcf.permissions;
|
||||
|
||||
import net.pingex.dcf.core.Configuration;
|
||||
import sx.blah.discord.handle.obj.IMessage;
|
||||
import sx.blah.discord.handle.obj.IGuild;
|
||||
import sx.blah.discord.handle.obj.IUser;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Default behavior when a permissions provider doesn't return `true` or `false`
|
||||
*/
|
||||
public enum DefaultPermission implements Predicate<IMessage>
|
||||
public enum DefaultPermission implements Predicate<DefaultPermission.Tuple>
|
||||
{
|
||||
/**
|
||||
* Everyone is allowed to run the command.
|
||||
*/
|
||||
EVERYONE(iMessage -> true),
|
||||
EVERYONE(tuple -> true),
|
||||
|
||||
/**
|
||||
* Only the guild owner is allowed to run the command.
|
||||
*/
|
||||
GUILD_OWNER(iMessage -> iMessage.getAuthor().getID().equals(iMessage.getGuild().getOwnerID())),
|
||||
GUILD_OWNER(tuple -> tuple.guild != null && tuple.user.getID().equals(tuple.guild.getOwnerID())),
|
||||
|
||||
/**
|
||||
* Only the bot owner is allowed to run the command.
|
||||
*/
|
||||
BOT_OWNER(iMessage -> iMessage.getAuthor().getID().equals(Configuration.BOT_OWNER)),
|
||||
BOT_OWNER(tuple -> tuple.user.getID().equals(Configuration.BOT_OWNER)),
|
||||
|
||||
/**
|
||||
* Guild Owner x Bot Owner
|
||||
*/
|
||||
ANY_OWNER(iMessage -> GUILD_OWNER.test(iMessage) || BOT_OWNER.test(iMessage)),
|
||||
ANY_OWNER(tuple -> GUILD_OWNER.test(tuple) || BOT_OWNER.test(tuple)),
|
||||
|
||||
/**
|
||||
* Nobody is allowed to run the command
|
||||
*/
|
||||
NONE(iMessage -> false);
|
||||
NONE(tuple -> false);
|
||||
|
||||
DefaultPermission(Predicate<IMessage> predicate)
|
||||
DefaultPermission(Predicate<Tuple> predicate)
|
||||
{
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
private Predicate<IMessage> predicate;
|
||||
private Predicate<Tuple> predicate;
|
||||
|
||||
@Override
|
||||
public boolean test(IMessage o)
|
||||
public boolean test(Tuple o)
|
||||
{
|
||||
return predicate.test(o);
|
||||
}
|
||||
|
||||
public static class Tuple
|
||||
{
|
||||
IUser user;
|
||||
IGuild guild;
|
||||
|
||||
public Tuple(IUser user, IGuild guild)
|
||||
{
|
||||
this.user = user;
|
||||
this.guild = guild;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue