Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit 7843478

Browse files
author
Lucas Malandrino
authored
Merge pull request #5 from JesusCrie/dev
Release 2.1.0
2 parents 5743b13 + cf203b7 commit 7843478

20 files changed

Lines changed: 604 additions & 61 deletions

File tree

.idea/compiler.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ModularBot-Base/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dependencies {
2-
compile "com.jesus-crie:modularbot-core:$version"
3-
compile "com.jesus-crie:modularbot-logger:$version"
4-
compile "com.jesus-crie:modularbot-command:$version"
2+
compile project(':modularbot-core')
3+
compile project(':modularbot-logger')
4+
compile project(':modularbot-command')
55
}

ModularBot-Command/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dependencies {
22
compile project(':modularbot-core')
33

4-
testCompile project(':modularbot-logger')
4+
testImplementation project(':modularbot-logger')
55
}

ModularBot-Command/src/main/java/com/jesus_crie/modularbot_command/AccessLevel.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,55 @@ public class AccessLevel {
1717
* Literally everyone, everywhere.
1818
*/
1919
public static final AccessLevel EVERYONE = new AccessLevel(
20-
EnumSet.noneOf(Permission.class), false, false, 0);
20+
EnumSet.noneOf(Permission.class), false, false);
2121

2222
/**
2323
* Only available in guilds.
2424
*/
2525
public static final AccessLevel GUILD_ONLY = new AccessLevel(
26-
EnumSet.noneOf(Permission.class), true, false, 0);
26+
EnumSet.noneOf(Permission.class), true, false);
2727

2828
/**
2929
* Only available in private channels.
3030
*/
3131
public static final AccessLevel PRIVATE_ONLY = new AccessLevel(
32-
EnumSet.noneOf(Permission.class), false, true, 0);
32+
EnumSet.noneOf(Permission.class), false, true);
3333

3434
/**
3535
* In guilds, peoples who can delete messages.
3636
*/
3737
public static final AccessLevel GUILD_MODERATOR = new AccessLevel(
38-
EnumSet.of(Permission.MESSAGE_MANAGE), true, false, 0);
38+
EnumSet.of(Permission.MESSAGE_MANAGE), true, false);
3939

4040
/**
4141
* In guilds, those who have all of the permissions.
4242
*/
4343
public static final AccessLevel GUILD_ADMINISTRATOR = new AccessLevel(
44-
EnumSet.of(Permission.ADMINISTRATOR), true, false, 0);
44+
EnumSet.of(Permission.ADMINISTRATOR), true, false);
4545

4646
/**
4747
* Only the creator of the bot.
4848
*/
49-
public static final AccessLevel CREATOR = new AccessLevel(EnumSet.noneOf(Permission.class), false, false, CREATOR_ID);
49+
public static final AccessLevel CREATOR = new AccessLevel(EnumSet.noneOf(Permission.class), false, false) {
50+
@Override
51+
public boolean check(@Nonnull CommandEvent event) {
52+
return event.getAuthor().getIdLong() == CREATOR_ID;
53+
}
54+
};
5055

5156
private final EnumSet<Permission> requiredPermissions;
5257
private final boolean onlyGuild;
5358
private final boolean onlyPrivate;
54-
private final long user;
5559

5660
/**
5761
* @param requiredPermissions The permissions required if in a guild.
5862
* @param onlyGuild Flag that indicate that this can only occur in a guild.
5963
* @param onlyPrivate Flag that indicate that this can only occur in a private channel.
60-
* @param user Flag that indicate that only this user can use this, /!\ overwrite every other flag /!\.
6164
*/
62-
public AccessLevel(@Nonnull EnumSet<Permission> requiredPermissions, boolean onlyGuild, boolean onlyPrivate, long user) {
65+
public AccessLevel(@Nonnull EnumSet<Permission> requiredPermissions, boolean onlyGuild, boolean onlyPrivate) {
6366
this.requiredPermissions = requiredPermissions;
6467
this.onlyGuild = onlyGuild;
6568
this.onlyPrivate = onlyPrivate;
66-
this.user = user;
6769
}
6870

6971
/**
@@ -72,10 +74,6 @@ public AccessLevel(@Nonnull EnumSet<Permission> requiredPermissions, boolean onl
7274
* @return
7375
*/
7476
public boolean check(@Nonnull CommandEvent event) {
75-
// Check user
76-
if (user != 0)
77-
return user == event.getAuthor().getIdLong();
78-
7977
// If in a guild
8078
if (event.isFromType(ChannelType.TEXT)) {
8179
// Only private, eliminatory

ModularBot-Command/src/main/java/com/jesus_crie/modularbot_command/CommandModule.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import com.jesus_crie.modularbot.ModularBotBuilder;
44
import com.jesus_crie.modularbot.module.BaseModule;
5-
import com.jesus_crie.modularbot_command.annotations.CommandInfo;
5+
import com.jesus_crie.modularbot.module.ModuleManager;
66
import com.jesus_crie.modularbot_command.listener.CommandListener;
77
import com.jesus_crie.modularbot_command.listener.DiscordCommandListener;
8-
import com.jesus_crie.modularbot_command.listener.NopCommandListener;
98
import com.jesus_crie.modularbot_command.processing.CommandProcessor;
109

1110
import javax.annotation.Nonnull;
@@ -20,7 +19,7 @@ public class CommandModule extends BaseModule {
2019

2120
// Prefix stuff
2221
private String defaultPrefix = "!";
23-
private Map<Long, String> customPrefix = new HashMap<>();
22+
private Map<Long, String> customPrefix = Collections.emptyMap();
2423

2524
// Command storing
2625
private List<Command> commandStorage = new ArrayList<>();
@@ -35,7 +34,7 @@ public CommandModule() {
3534
}
3635

3736
@Override
38-
public void onLoad(@Nonnull final ModularBotBuilder builder) {
37+
public void onLoad(@Nonnull final ModuleManager moduleManager, @Nonnull final ModularBotBuilder builder) {
3938
builder.addListeners(new DiscordCommandListener(this));
4039
}
4140

@@ -65,15 +64,45 @@ public void setCommandProcessorFlags(int... flags) {
6564

6665
/**
6766
* Set the id of the owner of the bot used in {@link AccessLevel#CREATOR}.
67+
* Will be automatically called if you use the config module.
6868
*
6969
* @param owner The discord id of the account of the creator.
7070
*/
71-
public void setOwnerId(final long owner) {
71+
public void setCreatorId(final long owner) {
7272
AccessLevel.CREATOR_ID = owner;
7373
}
7474

75+
/**
76+
* Register a new prefix for a guild and override the old one if present.
77+
* Used by the config module.
78+
* <p>
79+
* You can also delete the prefix by letting the prefix {@code null}.
80+
*
81+
* @param guildId The id of the targeted guild.
82+
* @param prefix The prefix for this guild.
83+
*/
84+
public void addCustomPrefixForGuild(final long guildId, @Nullable final String prefix) {
85+
if (customPrefix.size() == 0)
86+
customPrefix = new HashMap<>();
87+
88+
if (prefix == null || prefix.length() == 0 || defaultPrefix.equals(prefix))
89+
customPrefix.remove(guildId);
90+
91+
customPrefix.put(guildId, prefix);
92+
}
93+
94+
/**
95+
* Get an unmodifiable view of the custom prefixes.
96+
* Used by the config module.
97+
*
98+
* @return A view of the custom prefixes.
99+
*/
100+
public Map<Long, String> getCustomPrefixes() {
101+
return Collections.unmodifiableMap(customPrefix);
102+
}
103+
75104
@Nonnull
76-
public String getPrefixForGuild(long guildId) {
105+
public String getPrefixForGuild(final long guildId) {
77106
return customPrefix.getOrDefault(guildId, defaultPrefix);
78107
}
79108

ModularBot-Command/src/test/java/com/jesus_crie/modularbot_command/CommandTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jesus_crie.modularbot_command;
22

3+
import com.jesus_crie.modularbot.module.ModuleManager;
34
import com.jesus_crie.modularbot_command.annotations.CommandInfo;
45
import com.jesus_crie.modularbot_command.annotations.RegisterPattern;
56
import com.jesus_crie.modularbot_command.exception.InvalidCommandPatternMethodException;
@@ -10,13 +11,7 @@
1011
import net.dv8tion.jda.core.entities.User;
1112
import org.junit.jupiter.api.BeforeAll;
1213
import org.junit.jupiter.api.Test;
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
15-
import org.slf4j.impl.ModularLog;
16-
import org.slf4j.impl.ModularLogger;
1714

18-
import javax.annotation.Nonnull;
19-
import javax.annotation.RegEx;
2015
import java.net.URL;
2116
import java.util.List;
2217

@@ -28,7 +23,7 @@ public class CommandTest {
2823

2924
@BeforeAll
3025
static void beforeAll() {
31-
new ConsoleLoggerModule().onLoad(null);
26+
new ConsoleLoggerModule().onLoad(new ModuleManager(), null);
3227
}
3328

3429
@Test

ModularBot-Core/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
dependencies {
22
compile 'net.dv8tion:JDA:3.6.0_375'
33

4-
testCompile project(':modularbot-command')
5-
testCompile project(':modularbot-logger')
4+
testImplementation project(':modularbot-command')
5+
testImplementation project(':modularbot-logger')
6+
testImplementation project(':modularbot-night-config-wrapper')
67
}

ModularBot-Core/src/main/java/com/jesus_crie/modularbot/ModularBotBuilder.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.jesus_crie.modularbot.utils.IStateProvider;
66
import net.dv8tion.jda.core.JDA;
77
import net.dv8tion.jda.core.audio.factory.IAudioSendFactory;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
810

911
import javax.annotation.Nonnull;
1012
import java.util.ArrayList;
@@ -13,6 +15,8 @@
1315

1416
public class ModularBotBuilder {
1517

18+
private static final Logger LOG = LoggerFactory.getLogger("ModularBotBuilder");
19+
1620
protected final String token;
1721
protected int shards = -1;
1822
protected IStateProvider stateProvider = null;
@@ -171,15 +175,23 @@ public ModularBotBuilder autoLoadBaseModules() {
171175
Class<? extends BaseModule> loggerModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_logger.ConsoleLoggerModule");
172176
moduleManager.registerModules(this, loggerModule);
173177
} catch (ClassNotFoundException e) {
174-
System.out.println("[Warn] Failed to autoload logger module.");
178+
LOG.debug("Failed to autoload logger module.");
175179
}
176180

177181
// Try command module
178182
try {
179183
Class<? extends BaseModule> commandModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_command.CommandModule");
180184
moduleManager.registerModules(this, commandModule);
181185
} catch (ClassNotFoundException e) {
182-
System.out.println("[Warn] Failed to autoload command module.");
186+
LOG.debug("Failed to autoload command module.");
187+
}
188+
189+
// Try night config module
190+
try {
191+
Class<? extends BaseModule> nightConfigModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_nightconfigwrapper.NightConfigWrapperModule");
192+
moduleManager.registerModules(this, nightConfigModule);
193+
} catch (ClassNotFoundException e) {
194+
LOG.debug("Failed to autoload night config module.");
183195
}
184196

185197
// TODO 16/06/18 renember to complete with new modules

ModularBot-Core/src/main/java/com/jesus_crie/modularbot/module/Lifecycle.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ public interface Lifecycle {
1111
/**
1212
* Called when the module is loaded in the first place, occurs when the module is registered.
1313
*
14-
* @param builder The {@link ModularBotBuilder ModularBotBuilder} which is associated.
14+
* @param moduleManager The current {@link ModuleManager ModuleManager}
15+
* @param builder The {@link ModularBotBuilder ModularBotBuilder} which is associated.
1516
*/
16-
default void onLoad(@Nonnull final ModularBotBuilder builder) {}
17+
default void onLoad(@Nonnull final ModuleManager moduleManager, @Nonnull final ModularBotBuilder builder) {}
1718

1819
/**
1920
* Called when the instance of {@link ModularBot ModularBot} is almost created and the {@link ModuleManager ModuleManager}
@@ -41,7 +42,7 @@ default void onShardsCreated() {}
4142
* Called right after all of the shards have received the {@link net.dv8tion.jda.core.events.ReadyEvent ReadyEvent}.
4243
* If you need to do things the the bot come online, put it here.
4344
*
44-
* This method MUST call the {@link super}.
45+
* This method MUST call the {@code super}.
4546
*
4647
* @param bot The associated instance of {@link ModularBot ModularBot}.
4748
*/

ModularBot-Core/src/main/java/com/jesus_crie/modularbot/module/ModuleManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void registerModule(@Nonnull final ModularBotBuilder builder, @Nonnull fi
2727
throw new ModuleAlreadyLoadedException("Found another module " + m.getClass() + " !");
2828

2929
modules.put(module.getClass(), module);
30-
module.onLoad(builder);
30+
module.onLoad(this, builder);
3131
module.state = Lifecycle.State.LOADED;
3232
}
3333

0 commit comments

Comments
 (0)