implement BotAdmin privilege

This commit is contained in:
Sebastien 2019-06-16 19:27:16 +02:00
parent b05401777c
commit f4b99c0061
25 changed files with 281 additions and 18 deletions

View File

@ -2,7 +2,9 @@ package net.Broken;
import net.Broken.Commands.Move; import net.Broken.Commands.Move;
import net.Broken.DB.Entity.GuildPreferenceEntity; import net.Broken.DB.Entity.GuildPreferenceEntity;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.GuildPreferenceRepository; import net.Broken.DB.Repository.GuildPreferenceRepository;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.Tools.AntiSpam; import net.Broken.Tools.AntiSpam;
import net.Broken.Tools.Command.CommandParser; import net.Broken.Tools.Command.CommandParser;
import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.EmbedMessageUtils;
@ -38,6 +40,7 @@ public class BotListener extends ListenerAdapter {
private Moderateur modo = new Moderateur(); private Moderateur modo = new Moderateur();
private GuildPreferenceRepository guildPreferenceRepository; private GuildPreferenceRepository guildPreferenceRepository;
private UserRepository userRepository;
private Logger logger = LogManager.getLogger(); private Logger logger = LogManager.getLogger();
@ -45,6 +48,7 @@ public class BotListener extends ListenerAdapter {
ApplicationContext context = SpringContext.getAppContext(); ApplicationContext context = SpringContext.getAppContext();
guildPreferenceRepository = (GuildPreferenceRepository) context.getBean("guildPreferenceRepository"); guildPreferenceRepository = (GuildPreferenceRepository) context.getBean("guildPreferenceRepository");
userRepository = (UserRepository) context.getBean("userRepository");
} }
@ -157,7 +161,9 @@ public class BotListener extends ListenerAdapter {
if (event.getMessage().getContentRaw().startsWith("//") && !event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) { if (event.getMessage().getContentRaw().startsWith("//") && !event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) {
//On a detecter que c'etait une commande //On a detecter que c'etait une commande
//System.out.println(event.getMessage().getContent()); //System.out.println(event.getMessage().getContent());
MainBot.handleCommand(new CommandParser().parse(event.getMessage().getContentRaw(), event)); List<UserEntity> users = userRepository.findByJdaId(event.getAuthor().getId());
UserEntity user = users.size() == 0 ? null : users.get(0);
MainBot.handleCommand(new CommandParser().parse(event.getMessage().getContentRaw(), event), user);
} }
else if (!event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) else if (!event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId()))

View File

@ -26,6 +26,12 @@ public interface Commande {
*/ */
boolean isAdminCmd(); boolean isAdminCmd();
/**
* Determines if the command is usable only by bot level admin user
* @return boolean
*/
boolean isBotAdminCmd();
/** /**
* Determines if the command is only usable on NSFW channels * Determines if the command is only usable on NSFW channels
* @return boolean * @return boolean

View File

@ -56,6 +56,16 @@ public class Cat implements Commande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -3,14 +3,41 @@ package net.Broken.Commands;
import groovy.lang.Binding; import groovy.lang.Binding;
import groovy.lang.GroovyShell; import groovy.lang.GroovyShell;
import net.Broken.Commande; import net.Broken.Commande;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.GuildPreferenceRepository;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.SpringContext;
import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.MessageTimeOut;
import net.Broken.Tools.PrivateMessage;
import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import java.awt.*; import java.awt.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
public class Code implements Commande { public class Code implements Commande {
private UserRepository userRepository;
private Logger logger = LogManager.getLogger();
public Code (){
ApplicationContext context = SpringContext.getAppContext();
userRepository = (UserRepository) context.getBean("userRepository");
}
@Override @Override
public void action(String[] args, MessageReceivedEvent event) { public void action(String[] args, MessageReceivedEvent event) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for(String arg : args){ for(String arg : args){
stringBuilder.append(arg); stringBuilder.append(arg);
@ -61,6 +88,16 @@ public class Code implements Commande {
return true; return true;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return true;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -31,6 +31,16 @@ public class DayTrigger implements Commande{
return true; return true;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return true;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -59,6 +59,16 @@ public class Flush implements Commande{
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -160,6 +160,16 @@ public class Help implements Commande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -26,6 +26,16 @@ public class Invite implements Commande{
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -31,6 +31,16 @@ public class ListRoles implements Commande {
return true; return true;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -190,6 +190,16 @@ public class Move implements Commande {
return true; return true;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -165,6 +165,16 @@ public class Music implements Commande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -30,6 +30,16 @@ public class Ass extends NumberedCommande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return true; return true;

View File

@ -28,6 +28,16 @@ public class Boobs extends NumberedCommande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return true; return true;

View File

@ -38,6 +38,16 @@ public class Madame extends NumberedCommande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return true; return true;

View File

@ -28,6 +28,16 @@ public class Pipe extends NumberedCommande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return true; return true;

View File

@ -40,6 +40,16 @@ public class SM implements Commande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return true; return true;

View File

@ -69,6 +69,16 @@ public class Suicide implements Commande{
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return true; return true;

View File

@ -37,6 +37,16 @@ public class Ping implements Commande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -23,6 +23,16 @@ public class Rank implements Commande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -56,6 +56,16 @@ public class Settings implements Commande {
return true; return true;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -81,6 +81,16 @@ public class Spam implements Commande {
return true; return true;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -101,6 +101,16 @@ public class SpamInfo implements Commande{
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -58,6 +58,16 @@ public class YtSearch implements Commande {
return false; return false;
} }
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override @Override
public boolean isNSFW() { public boolean isNSFW() {
return false; return false;

View File

@ -25,6 +25,8 @@ public class UserEntity {
private String apiToken; private String apiToken;
private boolean isBotAdmin = false;
@JsonIgnore @JsonIgnore
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user") @OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
private List<UserStats> userStats; private List<UserStats> userStats;
@ -124,4 +126,12 @@ public class UserEntity {
public void setUserStats(List<UserStats> userStats) { public void setUserStats(List<UserStats> userStats) {
this.userStats = userStats; this.userStats = userStats;
} }
public boolean isBotAdmin() {
return isBotAdmin;
}
public void setBotAdmin(boolean botAdmin) {
isBotAdmin = botAdmin;
}
} }

View File

@ -1,5 +1,6 @@
package net.Broken; package net.Broken;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.Tools.Command.CommandParser; import net.Broken.Tools.Command.CommandParser;
import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.MessageTimeOut; import net.Broken.Tools.MessageTimeOut;
@ -85,7 +86,7 @@ public class MainBot {
* Perform test (admin, NSFW and private usable or not) and execute command or not * Perform test (admin, NSFW and private usable or not) and execute command or not
* @param cmd Container whit all command info * @param cmd Container whit all command info
*/ */
public static void handleCommand(CommandParser.CommandContainer cmd) public static void handleCommand(CommandParser.CommandContainer cmd, UserEntity user)
{ {
if(!ready) if(!ready)
@ -98,13 +99,14 @@ public class MainBot {
{ {
Commande cmdObj = commandes.get(cmd.commande); Commande cmdObj = commandes.get(cmd.commande);
boolean isAdmin; boolean isAdmin;
boolean isBotAdmin = user != null && user.isBotAdmin();
if(cmd.event.isFromType(ChannelType.PRIVATE)){ if(cmd.event.isFromType(ChannelType.PRIVATE)){
isAdmin = false; isAdmin = false;
} }
else else
isAdmin = cmd.event.getMember().hasPermission(Permission.ADMINISTRATOR); isAdmin = cmd.event.getMember().hasPermission(Permission.ADMINISTRATOR);
if(!cmdObj.isAdminCmd() || isAdmin){ if((!cmdObj.isAdminCmd() || isAdmin) && (!cmdObj.isBotAdminCmd() || isBotAdmin)){
if(cmd.event.isFromType(ChannelType.PRIVATE) && commandes.get(cmd.commande).isPrivateUsable()) if(cmd.event.isFromType(ChannelType.PRIVATE) && commandes.get(cmd.commande).isPrivateUsable())
{ {
@ -123,14 +125,16 @@ public class MainBot {
} }
else else
cmd.event.getPrivateChannel().sendMessage(EmbedMessageUtils.getNoPrivate()).queue(); cmd.event.getPrivateChannel().sendMessage(EmbedMessageUtils.getNoPrivate()).queue();
} }
else{ else{
if(cmd.event.isFromType(ChannelType.PRIVATE)){ if(cmd.event.isFromType(ChannelType.PRIVATE)){
PrivateMessage.send(cmd.event.getAuthor(),EmbedMessageUtils.getUnautorized(), logger); PrivateMessage.send(cmd.event.getAuthor(),EmbedMessageUtils.getUnautorized(), logger);
} }
else{ else{
Message msg = cmd.event.getTextChannel().sendMessage(EmbedMessageUtils.getUnautorized()).complete(); cmd.event.getTextChannel().sendMessage(EmbedMessageUtils.getUnautorized()).complete();
new MessageTimeOut(gifMessageTimeOut, msg, cmd.event.getMessage()).start();
} }
} }
} }