publicstaticfinalintDISABLED=0x80000;//even if i use 0x80000000 and it overflows it should still work //DONE 0x200 is a new permission; JDA 3 will probably not recognize it so i have to circumvent over it manually //changed to 0x80000
//PATCHED? SQL INJECTION PRONE (eg node: games/**/WHERE1--.",*," - returns numberformatexception; games/**/WHERE1--.osu",*,"osu returns N/A; can even drop tables)
registerSubCommand("perms",Arrays.asList("permissions","perm","permission"),(channel,user,msg,words)->{//DONE disable commands; use negative? //TODO list all edited permission nodes, clear command?
if(!words[1].matches("[a-zA-Z0-9.]+"))returnnewCommandResult(CommandResultType.INVALIDARGS,"Invalid node specified.");//prevent SQL injection; might be restricting though, but then again all command names are alphanumeric
words[0]=msg.getMentions().getChannels().get(0).getId();//assuming its to order from left to right; but tbh if they try to trick the command with 2 channels it wont work anyways with the range check and node check
if(sOrig.equals(node))returnnewCommandResult(CommandResultType.INVALIDARGS,"Invalid node specified.");//SQL returns the actual string if the table doesnt have the column
if(rs.next()){//considerations: rs.next(); guild vs channel; allows; add or remove;
lines=rs.getString(1).split("\n");
if(lines[0].equals(node))returnnewCommandResult(CommandResultType.INVALIDARGS,"Invalid node specified.");//SQL returns the actual string if the table doesnt have the column
inti=0;
longorig=0;
if(opType==2){
orig=Long.parseLong(lines[0]);
}else{
try{
for(i=0;i<=lines.length;i++){
if(lines[i].startsWith(words[0]+":")){
orig=Long.parseLong(lines[i].split(":")[1]);
break;
}
}
}catch(ArrayIndexOutOfBoundsExceptione){
lines=Arrays.copyOf(lines,lines.length+1);//to extend it; should be the same method as using arraylist
//orig = Permission.getRaw(def); //use default //actually should already have imposed global default if rs has next; adding default to this will only give channel the global perm, which makes no sense
type=split[0];lines[i]=split[1];//at this stage theres only 2 options: guild and a valid channel id
}else{
if(opType==2){
String[]split=imposePerms(null,Permission.getRaw(def),perm,allows);//initiate guild by imposing perm to default; since allow is in the lower 32 bit, i can just put def as the base
type=split[0];lines[0]=split[1];
}else{
lines[0]=String.valueOf(Permission.getRaw(def));//initiate guild with the default permission of the command
lines=Arrays.copyOf(lines,lines.length+1);
String[]split=imposePerms(words[0],0,perm,allows);//impose with perm base = 0
type=split[0];lines[1]=split[1];
}
}
Stringfin=String.join("\n",lines)+"\n";//last line must have \n
s.execute("INSERT INTO perms_"+cat+"(id, \""+node+"\") VALUES ("+channel.getGuild().getId()+", '"+fin+"') ON CONFLICT(id) DO UPDATE SET \""+node+"\" = '"+fin+"';");//no need blank update check, because it will never be the same
channel.sendMessage("Successfully "+type+" "+(words[0].equalsIgnoreCase("guild")?"guild-wide":"channel-specific (<#"+words[0]+">)")+" permission `"+(allows?"":"-")+(perm==null?"DISABLED":perm.name())+"` for `"+words[1].toLowerCase()+"`.").queue();
},"<channel|guild|list|wipe|wipeall> <command node> [-][permission]",Arrays.asList("list admin","list games.osu.pp","wipeall admin","wipe games.osu","419464253574086656 games MESSAGE_EMBED_LINKS","guild games.osu -MESSAGE_EMBED_LINKS","#general music VOICE_CONNECT","guild admin ADMINISTRATOR"),
"Edit default perms required for commands for this guild!",Arrays.asList(
"Adding a `-` sign before a permission overrides the parent permission set,",
"Where `cat(guild > channel) > cmd(guild > channel) > subcmd(guild > channel)` (parent > child, where subcmd channel is of highest priority).",
"*For example, specifying `admin.settings.prefix` with `-MANAGE_SERVER` will override the requirement of `admin.settings`, and allow those without `MANAGE_SERVER` to use `admin.settings.prefix`*.\n",
"The permission nodes are as such: `<cat>[.cmd][.subcmd]`.",
"For permission names, please refer to https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/core/Permission.html,",
"Along with a special permission `DISABLED` for disabling the node.",
"Specifying the permission twice will remove it.\n",
"Use `wipe` if you want to reset the permissions for the node, and `wipeall` for resetting all perms in the whole category.",
"`list` and `wipe`/`wipeall` only accepts the first 2 parameters."),EnumSet.of(Permission.ADMINISTRATOR),-BotUserLevel.BOT_OWNER.ordinal());
registerSubCommand("prefix",Arrays.asList("pre"),(channel,user,msg,words)->{//dont allow prefixes with markdown for now
Stringprefix;
if(words.length<1){
prefix=DiscordBot.prefix;
channel.sendMessage("No prefix entered. Using default prefix...").queue();
}else{
prefix=msg.getContentStripped().substring(msg.getContentDisplay().lastIndexOf(String.join(" ",words))).replaceAll("\\\\","");//if prefix/shortcut contains markdown it breaks
}
if(prefix.length()>30)returnnewCommandResult(CommandResultType.INVALIDARGS,"Please do not enter prefixes that are too long.");//arbitrarily set; can change anytime
try(Connectioncon=DiscordBot.db.getConnection()){
PreparedStatements=con.prepareStatement("INSERT INTO settings(id, prefix) VALUES ("+channel.getGuild().getId()+", ?) ON CONFLICT(id) DO UPDATE SET prefix = ? WHERE prefix <> ?");
s.setString(1,prefix);s.setString(2,prefix);s.setString(3,prefix);//using prep statement to prevent injection
intupdate=s.executeUpdate();
s.close();
if(update!=0){//should ever only be 1 or 0
channel.sendMessage("Successfully changed prefix to `"+prefix+"`. Do "+prefix+"help to see the new command syntaxes.").queue();
}else{
returnnewCommandResult(CommandResultType.INVALIDARGS,"You are setting the same prefix!");
"Change the prefix for this guild!",Arrays.asList("*You can also include spaces with the use of `code block`s.*","Leave blank to use the default (`"+DiscordBot.prefix+"`)."));