Skip to content

Commit

Permalink
Update NBot 2.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
NeutronStars committed Mar 18, 2018
1 parent 6801e07 commit 6881029
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/main/java/fr/neutronstars/nbot/NBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public final class NBot
{
private static final String NAME = "NBot", VERSION = "2.0.3", AUTHOR = "NeutronStars";
private static final String NAME = "NBot", VERSION = "2.0.4", AUTHOR = "NeutronStars";
private static final NBotLogger logger = StaticLoggerBinder.getSingleton().getLoggerFactory().getLogger("NBot");
private static NBotServer server;

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/fr/neutronstars/nbot/NBotServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ protected NBotServer(Configuration configuration, PluginManager pluginManager) t
.addEventListener(new NBotListener(pluginManager)).buildAsync();
}

public NBotLogger getLogger()
{
return logger;
}

public JDA getJDA()
{
return jda;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/fr/neutronstars/nbot/NBotStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public static void main(String... args)
{
System.setProperty("file.encoding", "UTF-8");

logger.info(String.format("Starting %1$s v%2$s by %3$s...", NBot.getName(), NBot.getVersion(), NBot.getAuthor()));

loadFolders("guilds", "plugins", "config");

Configuration configuration = loadConfiguration();
setDefaultConfiguration(configuration);

NBotLogger.load(configuration);

logger.info(String.format("Starting %1$s v%2$s by %3$s...", NBot.getName(), NBot.getVersion(), NBot.getAuthor()));

PluginManager pluginManager = new PluginManager(configuration.getString("loadedFormat"), configuration.getString("enabledFormat"), configuration.getString("disabledFormat"));
CommandManager.registerCommand(new DefaultCommand(), null);
pluginManager.registerCommands();
Expand Down Expand Up @@ -96,6 +98,9 @@ private static void setDefaultConfiguration(Configuration configuration)
if(!configuration.has("enabledFormat")) configuration.set("enabledFormat", "%1$s v%2$s by %3$s is enabled.");
if(!configuration.has("disabledFormat")) configuration.set("disabledFormat", "%1$s v%2$s by %3$s is disabled.");

if(!configuration.has("loggerTimeFormat")) configuration.set("loggerTimeFormat", "HH:mm:ss");
if(!configuration.has("loggerLineFormat")) configuration.set("loggerLineFormat", "[{DATE}] [{LEVEL}-{NAME}] {MESSAGE}");

configuration.save();
}

Expand Down
41 changes: 38 additions & 3 deletions src/main/java/fr/neutronstars/nbot/command/CommandMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fr.neutronstars.nbot.entity.Guild;
import fr.neutronstars.nbot.entity.Message;
import fr.neutronstars.nbot.entity.User;
import fr.neutronstars.nbot.plugin.NBotPlugin;
import fr.neutronstars.nbot.util.Configuration;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.*;
Expand All @@ -14,7 +15,9 @@

import java.io.File;
import java.lang.reflect.Parameter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -128,15 +131,21 @@ public boolean onCommand(User user, String command, Message message)
Object[] objects = getCommands(command.split(" "));
if(objects[0] == null) return false;

NBot.getLogger().info("[Command] "+user.getName() + " -> "+command);

SimpleCommand simpleCommand = (SimpleCommand)objects[0];
if(!guild.hasPermission(user, simpleCommand.getPower())) return false;

if(simpleCommand.getPlugin() != null)
simpleCommand.getPlugin().getLogger().info("[Command] "+user.getName() + " -> "+command);
else
NBot.getLogger().info("[Command] "+user.getName() + " -> "+command);

try {
execute(simpleCommand, command, (String[]) objects[1], message, user);
}catch(Exception e) {
NBot.getLogger().error(e.getMessage(), e);
if(simpleCommand.getPlugin() != null)
simpleCommand.getPlugin().getLogger().error(e.getMessage(), e);
else
NBot.getLogger().error(e.getMessage(), e);
}
return true;
}
Expand All @@ -155,6 +164,32 @@ public List<SimpleCommand> getCommands()
return new ArrayList<>(commands);
}

public List<SimpleCommand> getDefaultCommands()
{
List<SimpleCommand> defaultCommands = new ArrayList<>();
List<SimpleCommand> commands = getCommands();

for(SimpleCommand command : commands)
if(command.getPlugin() == null) defaultCommands.add(command);

return defaultCommands;
}

public Map<NBotPlugin, List<SimpleCommand>> getPkuginCommands()
{
Map<NBotPlugin, List<SimpleCommand>> pluginCommandMap = new HashMap<>();
List<SimpleCommand> commands = getCommands();

for(SimpleCommand command : commands)
{
if(command.getPlugin() == null) continue;
if(!pluginCommandMap.containsKey(command.getPlugin()))
pluginCommandMap.put(command.getPlugin(), new ArrayList<>());
pluginCommandMap.get(command.getPlugin()).add(command);
}
return pluginCommandMap;
}

private void execute(SimpleCommand simpleCommand, String command, String[] args, Message message, User user) throws Exception{
Parameter[] parameters = simpleCommand.getMethod().getParameters();
Object[] objects = new Object[parameters.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,32 @@
import java.awt.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
* Created by NeutronStars on 21/09/2017
*/
public class DefaultCommand
{
@Command(name = "help")
@Command(name = "help", description = "Send the commands list.")
private void onHelp(User user, Channel channel, Guild guild)
{
Collection<SimpleCommand> commands = guild.getCommands();
channel.sendMessageToChannel(user.getAsMention()+" check your private message !");

sendHelp("Default Commands", user, guild, guild.getDefaultCommands());

Map<NBotPlugin, List<SimpleCommand>> pluginCommandsMap = guild.getPluginCommands();
for(Map.Entry<NBotPlugin, List<SimpleCommand>> entry : pluginCommandsMap.entrySet())
sendHelp(entry.getKey().getName()+" Commands", user, guild, entry.getValue());
}

private void sendHelp(String title, User user, Guild guild, Collection<SimpleCommand> commands)
{
EmbedBuilder builder = new EmbedBuilder();
builder.setTitle("Commands List");
builder.setTitle(title);
builder.setDescription("Commands for the guild "+guild.getName()+"\n -> Prefix : "+guild.getPrefix());
builder.setColor(Color.MAGENTA);
builder.setFooter("API "+NBot.getName()+" v"+NBot.getVersion()+" by "+NBot.getAuthor(), null);
builder.setFooter(NBot.getName()+" API v"+NBot.getVersion()+" by "+NBot.getAuthor(), NBot.getJDA().getSelfUser().getAvatarUrl());

for(SimpleCommand command : commands)
{
Expand All @@ -40,10 +51,9 @@ private void onHelp(User user, Channel channel, Guild guild)
}

user.sendMessageToChannel(builder.build());
channel.sendMessageToChannel(user.getAsMention()+" check your private message !");
}

@Command(name="plugins")
@Command(name="plugins", description = "Show the list of plugins.")
private void onPlugins(Channel channel)
{
Collection<NBotPlugin> plugins = NBot.getPluginManager().getPlugins();
Expand All @@ -64,15 +74,15 @@ private void onPlugins(Channel channel)
channel.sendMessageToChannel(builder.build());
}

@Command(name = "prefix", powers = 100)
@Command(name = "prefix", powers = 100, description = "Change the prefix for the commands of the guild.")
private void onPrefix(Guild guild, Channel channel, String[] args)
{
String prefix = args.length == 0 || args[0].equalsIgnoreCase("null") ? null : args[0];
guild.setPrefix(prefix);
channel.sendMessageToChannel("Prefix modified -> "+prefix);
}

@Command(name="power", powers = 100)
@Command(name="power", powers = 100, description = "Change the permissions of commands, users or role of the guild.")
private void onPower(SimpleCommand command, User user1, Message message, String[] args, Guild guild)
{
if(args.length < 2)
Expand Down Expand Up @@ -121,7 +131,7 @@ private void onPower(SimpleCommand command, User user1, Message message, String[
message.getMessageChannel().sendMessageToChannel(builder.toString());
}

@Command(name = "commandname", powers = 100)
@Command(name = "commandname", powers = 100, description = "change the commands name of the guild.")
private void onCommandName(SimpleCommand simpleCommand, User user, String[] args, Guild guild, Channel channel)
{
if(args.length < 2)
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/fr/neutronstars/nbot/entity/Guild.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.neutronstars.nbot.NBot;
import fr.neutronstars.nbot.command.CommandMap;
import fr.neutronstars.nbot.command.SimpleCommand;
import fr.neutronstars.nbot.plugin.NBotPlugin;
import fr.neutronstars.nbot.util.Configuration;
import fr.neutronstars.nbot.util.JSONReader;
import fr.neutronstars.nbot.util.JSONWriter;
Expand Down Expand Up @@ -510,6 +511,16 @@ public Collection<SimpleCommand> getCommands()
return commandMap.getCommands();
}

public Collection<SimpleCommand> getDefaultCommands()
{
return commandMap.getDefaultCommands();
}

public Map<NBotPlugin, List<SimpleCommand>> getPluginCommands()
{
return commandMap.getPkuginCommands();
}

public void setPrefix(String newPrefix)
{
commandMap.setPrefix(newPrefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void onReady(ReadyEvent event)

String playing = NBot.getConfiguration().getString("playing");
if(playing != null && !playing.equalsIgnoreCase("null"))
NBot.getJDA().getPresence().setGame(Game.listening(playing));
NBot.getJDA().getPresence().setGame(Game.playing(playing));

logger.info(builder.toString());

Expand Down
Loading

0 comments on commit 6881029

Please sign in to comment.