Added IGuild to Context

master
Pingex aka Raphaël 9 years ago
parent 34af04a7b0
commit b1a59cbb15

@ -2,6 +2,7 @@ package net.pingex.dcf.commands;
import sx.blah.discord.api.IDiscordClient; import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.handle.obj.IChannel; import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IUser; import sx.blah.discord.handle.obj.IUser;
import java.util.List; import java.util.List;
@ -31,6 +32,11 @@ public class Context
*/ */
private IChannel channel; private IChannel channel;
/**
* Guild to audit against, null if not relevant.
*/
private IGuild guild;
/** /**
* The originating reference message * The originating reference message
*/ */
@ -54,26 +60,28 @@ public class Context
this.arguments = arguments; this.arguments = arguments;
this.user = originatingMessage.getAuthor(); this.user = originatingMessage.getAuthor();
this.channel = originatingMessage.getChannel(); this.channel = originatingMessage.getChannel();
this.guild = originatingMessage.getChannel().isPrivate() ? null : originatingMessage.getChannel().getGuild();
this.originatingMessage = originatingMessage; this.originatingMessage = originatingMessage;
this.client = client; this.client = client;
} }
/** /**
* Construct a context using explicit user and channel. The provided message is here for reference. * Construct a context without an explicit IMessage. Used to provide a mock context for auditing purposes.
* @param command Command being executed. * @param command Command being executed.
* @param arguments Its arguments. * @param arguments Its arguments.
* @param user User invoking the command. * @param user User invoking the command.
* @param channel Channel used to invoke the command. * @param channel Channel used to invoke the command.
* @param originatingMessage The messaged used to invoke the command. * @param guild Originating guild.
* @param client Discord client. * @param client Discord client.
*/ */
public Context(Command command, List<String> arguments, IUser user, IChannel channel, IMessage originatingMessage, IDiscordClient client) public Context(Command command, List<String> arguments, IUser user, IChannel channel, IGuild guild, IDiscordClient client)
{ {
this.command = command; this.command = command;
this.arguments = arguments; this.arguments = arguments;
this.user = user; this.user = user;
this.channel = channel; this.channel = channel;
this.originatingMessage = originatingMessage; this.guild = guild;
this.originatingMessage = null;
this.client = client; this.client = client;
} }
@ -97,6 +105,11 @@ public class Context
return channel; return channel;
} }
public IGuild getGuild()
{
return guild;
}
public IMessage getOriginatingMessage() public IMessage getOriginatingMessage()
{ {
return originatingMessage; return originatingMessage;

@ -28,10 +28,10 @@ public class UserGuildCheck implements IAuditComponentProvider
public AuditResult doAudit(Context context) public AuditResult doAudit(Context context)
{ {
// Check for guild // Check for guild
if(context.getChannel().getGuild() == null) if(context.getGuild() == null)
return new AuditResult(AuditResult.ResultCode.NOOP, "This channel is not part of a guild."); return new AuditResult(AuditResult.ResultCode.NOOP, "This channel is not part of a guild.");
Boolean returnedValue = provider.validateUser(context.getChannel().getGuild(), context.getUser(), context.getCommand()); Boolean returnedValue = provider.validateUser(context.getGuild(), context.getUser(), context.getCommand());
if(returnedValue == null) // No rule for user in this guild if(returnedValue == null) // No rule for user in this guild
return new AuditResult(AuditResult.ResultCode.NOOP, "No guild rule for this user."); return new AuditResult(AuditResult.ResultCode.NOOP, "No guild rule for this user.");

@ -29,11 +29,11 @@ public class UserGuildRoleCheck implements IAuditComponentProvider
public AuditResult doAudit(Context context) public AuditResult doAudit(Context context)
{ {
// Check for guild // Check for guild
if(context.getChannel().getGuild() == null) if(context.getGuild() == null)
return new AuditResult(AuditResult.ResultCode.NOOP, "This channel is not part of a guild."); return new AuditResult(AuditResult.ResultCode.NOOP, "This channel is not part of a guild.");
if(context.getChannel().getGuild().getRolesForUser(context.getUser()) != null) // User has roles if(context.getGuild().getRolesForUser(context.getUser()) != null) // User has roles
for(IRole i : context.getChannel().getGuild().getRolesForUser(context.getUser())) for(IRole i : context.getGuild().getRolesForUser(context.getUser()))
{ {
Boolean returnedQuery = provider.validateGroup(i, context.getCommand()); Boolean returnedQuery = provider.validateGroup(i, context.getCommand());
if(returnedQuery != null) if(returnedQuery != null)