Skip to content

Commit a35cf1e

Browse files
authored
Move TheListener to JDA (#185)
* Move half of the events to JDA * Move RoleEvents * Finish move to JDA * Few tweaks * Add timeout events * Re-add List argument check in the JDBI arg factory
1 parent 67b8442 commit a35cf1e

24 files changed

+866
-960
lines changed

build.gradle

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ configurations {
4343
shade
4444
implementation.extendsFrom(shade)
4545

46-
d4j
47-
listenerImplementation.extendsFrom(d4j)
48-
4946
global
5047
coreImplementation.extendsFrom(global)
5148
implementation.extendsFrom(global)
@@ -58,6 +55,7 @@ configurations {
5855
implementation.extendsFrom(jda)
5956
commanderImplementation.extendsFrom(jda)
6057
watcherImplementation.extendsFrom(jda)
58+
listenerImplementation.extendsFrom(jda)
6159

6260
globalCompile
6361
coreCompileOnly.extendsFrom(globalCompile)
@@ -107,8 +105,6 @@ dependencies {
107105
}
108106
jda libs.chewtils
109107

110-
d4j libs.discord4j
111-
112108
global libs.guava
113109
global libs.gson
114110
global libs.logback
@@ -133,9 +129,6 @@ dependencies {
133129
globalCompile libs.annotations
134130
globalCompile libs.lombok
135131

136-
listenerCompileOnly libs.immutables.value
137-
listenerCompileOnly libs.immutables.builder
138-
139132
global libs.jdbi.core
140133
global libs.jdbi.sqlobject
141134

@@ -156,7 +149,7 @@ tasks.named('shadowJar', ShadowJar).configure {
156149
sourceSets.forEach({
157150
from it.output
158151
})
159-
configurations = [project.configurations.jda, project.configurations.global, project.configurations.d4j]
152+
configurations = [project.configurations.jda, project.configurations.global]
160153
manifest.attributes(makeManifestAttributes())
161154
classifier("all")
162155
group("build")
@@ -172,7 +165,7 @@ tasks.named(JavaPlugin.JAR_TASK_NAME, Jar).configure {
172165
dependsOn(tasks.getByName("updateLicenses"))
173166
}
174167

175-
configureBotJar("theListener", sourceSets.listener, Arrays.asList("d4j"))
168+
configureBotJar("theListener", sourceSets.listener, Arrays.asList("jda"))
176169
configureBotJar("theWatcher", sourceSets.watcher, Arrays.asList("jda"))
177170
configureBotJar("theCommander", sourceSets.commander, Arrays.asList("jda"))
178171

src/core/java/com/mcmoddev/mmdbot/core/RunBots.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.slf4j.LoggerFactory;
3434

3535
import javax.annotation.Nonnull;
36+
import javax.security.auth.login.LoginException;
3637
import java.io.BufferedWriter;
3738
import java.io.FileReader;
3839
import java.io.FileWriter;
@@ -110,7 +111,11 @@ public static void main(String[] a) {
110111
}, 5, TimeUnit.SECONDS); // Give the bot 5 seconds to startup.. it
111112
// should add its listeners till then
112113
}
113-
bot.start();
114+
try {
115+
bot.start();
116+
} catch (LoginException e) {
117+
bot.getLogger().error("Exception logging in: ", e);
118+
}
114119
botsAmount.incrementAndGet();
115120
bot.getLogger().warn("Bot {} has been found, and it has been launched!", botEntry.name());
116121
}).exceptionally(t -> {

src/core/java/com/mcmoddev/mmdbot/core/bot/Bot.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222

2323
import org.slf4j.Logger;
2424

25+
import javax.security.auth.login.LoginException;
2526
import java.io.IOException;
2627

2728
public interface Bot {
2829

29-
void start();
30+
void start() throws LoginException;
3031

3132
BotType<?> getType();
3233

src/core/java/com/mcmoddev/mmdbot/core/commands/component/storage/SQLComponentStorage.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class SQLComponentStorage implements ComponentStorage {
5353

5454
// Install the SQL Objects and Guava plugins
5555
jdbi.installPlugin(new SqlObjectPlugin());
56-
jdbi.registerArgument(new JdbiFactories.JdbiArgumentFactory());
56+
jdbi.registerArgument(JdbiFactories.JdbiArgumentFactory.FACTORY);
5757
jdbi.registerRowMapper(Component.class, (rs, ctx) ->
5858
new Component(rs.getString(FEATURE_ROW_NAME),
5959
UUID.fromString(rs.getString(ID_ROW_NAME)),
@@ -70,7 +70,7 @@ public void insertComponent(Component component) {
7070
))
7171
.bind(FEATURE_ROW_NAME, component.featureId())
7272
.bind(ID_ROW_NAME, component.uuid())
73-
.bind(ARGUMENTS_ROW_NAME, listToString(component.arguments()))
73+
.bind(ARGUMENTS_ROW_NAME, component.arguments())
7474
.bind(LIFESPAN_ROW_NAME, component.lifespan().toString())
7575
.bind(LAST_USED_ROW_NAME, Instant.now())
7676
.execute());
@@ -93,7 +93,7 @@ public void updateArguments(UUID id, List<String> newArguments) {
9393
jdbi.useHandle(handle -> handle.createUpdate("update %s set %s = :args, %s = :last_used where %s = :id".formatted(
9494
tableName, ARGUMENTS_ROW_NAME, LAST_USED_ROW_NAME, ID_ROW_NAME
9595
))
96-
.bind("args", listToString(newArguments))
96+
.bind("args", newArguments)
9797
.bind("id", id.toString())
9898
.bind("last_used", Instant.now())
9999
.execute());
@@ -131,10 +131,6 @@ public void removeComponentsLastUsedBefore(Instant before) {
131131
private static final Type STRING_LIST_TYPE = new TypeToken<List<String>>() {
132132
}.getType();
133133

134-
private static String listToString(final List<String> list) {
135-
return Constants.Gsons.NO_PRETTY_PRINTING.toJson(list, STRING_LIST_TYPE);
136-
}
137-
138134
private static List<String> listFromString(final String string) {
139135
return Constants.Gsons.NO_PRETTY_PRINTING.fromJson(string, STRING_LIST_TYPE);
140136
}

src/core/java/com/mcmoddev/mmdbot/core/database/jdbi/JdbiFactories.java

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,34 @@
2020
*/
2121
package com.mcmoddev.mmdbot.core.database.jdbi;
2222

23-
import org.jdbi.v3.core.argument.AbstractArgumentFactory;
23+
import com.mcmoddev.mmdbot.core.util.Constants;
2424
import org.jdbi.v3.core.argument.Argument;
25-
import org.jdbi.v3.core.argument.ArgumentFactory;
25+
import org.jdbi.v3.core.argument.NullArgument;
2626
import org.jdbi.v3.core.config.ConfigRegistry;
27+
import org.jdbi.v3.core.generic.GenericTypes;
2728
import org.jdbi.v3.core.statement.StatementContext;
29+
import org.jdbi.v3.core.statement.UnableToCreateStatementException;
2830

2931
import java.lang.reflect.Type;
3032
import java.sql.PreparedStatement;
3133
import java.sql.SQLException;
3234
import java.sql.Types;
3335
import java.util.HashMap;
36+
import java.util.List;
3437
import java.util.Map;
3538
import java.util.Optional;
3639
import java.util.UUID;
40+
import java.util.function.Function;
3741

38-
public abstract class JdbiFactories<T> extends AbstractArgumentFactory<T> {
39-
private static final Map<Type, AbstractArgumentFactory<?>> BY_TYPE_FACTORIES = new HashMap<>();
42+
@SuppressWarnings("rawtypes")
43+
public final class JdbiFactories {
44+
private static final Map<Type, ArgumentFactory<?>> BY_TYPE_FACTORIES = new HashMap<>();
4045

41-
public static final AbstractArgumentFactory<UUID> UUID = create(Types.JAVA_OBJECT, java.util.UUID.class, ((position, statement, ctx, value) -> statement.setString(position, value.toString())));
46+
public static final ArgumentFactory<UUID> UUID = create(Types.JAVA_OBJECT, java.util.UUID.class, ((position, statement, ctx, value) -> statement.setString(position, value.toString())));
47+
public static final ArgumentFactory<List> LIST = create(Types.JAVA_OBJECT, List.class, ((position, statement, ctx, value) -> statement.setString(position, Constants.Gsons.NO_PRETTY_PRINTING.toJson(value))));
4248

43-
JdbiFactories(final int sqlType) {
44-
super(sqlType);
45-
}
46-
47-
public static <T> JdbiFactories<T> create(int sqlType, Type type, Factory<T> factory) {
48-
final var fct = new JdbiFactories<T>(sqlType) {
49+
public static <T> ArgumentFactory<T> create(int sqlType, Type type, Factory<T> factory) {
50+
final var fct = new ArgumentFactory<T>(sqlType, type) {
4951
@Override
5052
protected Argument build(final T value, final ConfigRegistry config) {
5153
return (position, statement, ctx) -> factory.accept(position, statement, ctx, value);
@@ -56,18 +58,85 @@ protected Argument build(final T value, final ConfigRegistry config) {
5658
}
5759

5860
@FunctionalInterface
59-
interface Factory<T> {
61+
private interface Factory<T> {
6062
void accept(int position, PreparedStatement statement, StatementContext ctx, T value) throws SQLException;
6163
}
6264

63-
public static final class JdbiArgumentFactory implements ArgumentFactory {
65+
/**
66+
* A factory for argument factories defined in {@link JdbiFactories}.
67+
*/
68+
public static final class JdbiArgumentFactory implements org.jdbi.v3.core.argument.ArgumentFactory {
69+
70+
public static final JdbiArgumentFactory FACTORY = new JdbiArgumentFactory();
71+
72+
private JdbiArgumentFactory() {}
6473

6574
@Override
6675
public Optional<Argument> build(final Type type, final Object value, final ConfigRegistry config) {
76+
if (List.class.isAssignableFrom(GenericTypes.getErasedType(type))) {
77+
return LIST.build(type, value, config);
78+
}
6779
if (BY_TYPE_FACTORIES.containsKey(type)) {
6880
return BY_TYPE_FACTORIES.get(type).build(type, value, config);
6981
}
7082
return Optional.empty();
7183
}
7284
}
85+
86+
public static abstract class ArgumentFactory<T> implements org.jdbi.v3.core.argument.ArgumentFactory.Preparable {
87+
private final int sqlType;
88+
private final ArgumentPredicate isInstance;
89+
90+
/**
91+
* Constructs an {@link ArgumentFactory} for type {@code T}.
92+
*
93+
* @param sqlType the {@link java.sql.Types} constant to use when the argument value is {@code null}.
94+
*/
95+
protected ArgumentFactory(int sqlType, final Type argumentType) {
96+
this.sqlType = sqlType;
97+
98+
if (argumentType instanceof Class<?> argumentClass) {
99+
this.isInstance = (type, value) ->
100+
argumentClass.isAssignableFrom(GenericTypes.getErasedType(type)) || argumentClass.isInstance(value);
101+
} else {
102+
this.isInstance = (type, value) -> argumentType.equals(type);
103+
}
104+
}
105+
106+
@Override
107+
public Optional<Function<Object, Argument>> prepare(Type type, ConfigRegistry config) {
108+
return isInstance.test(type, null)
109+
? Optional.of(value -> innerBuild(value, config)
110+
.orElseThrow(() -> new UnableToCreateStatementException("Prepared argument " + value + " of type " + type + " failed to bind")))
111+
: Optional.empty();
112+
}
113+
114+
@Override
115+
public final Optional<Argument> build(Type type, Object value, ConfigRegistry config) {
116+
if (!isInstance.test(type, value)) {
117+
return Optional.empty();
118+
}
119+
return innerBuild(value, config);
120+
}
121+
122+
@SuppressWarnings("unchecked")
123+
private Optional<Argument> innerBuild(Object value, ConfigRegistry config) {
124+
return Optional.of(value == null
125+
? new NullArgument(sqlType)
126+
: build((T) value, config));
127+
}
128+
129+
/**
130+
* Produce an argument object for the given value.
131+
*
132+
* @param value the value to convert to an argument
133+
* @param config the config registry
134+
* @return an {@link Argument} for the given {@code value}.
135+
*/
136+
protected abstract Argument build(T value, ConfigRegistry config);
137+
138+
private interface ArgumentPredicate {
139+
boolean test(Type type, Object value);
140+
}
141+
}
73142
}

src/core/java/com/mcmoddev/mmdbot/core/util/event/ThreadedEventListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.awt.Color;
3434
import java.util.ArrayList;
3535
import java.util.Arrays;
36+
import java.util.LinkedList;
3637
import java.util.List;
3738
import java.util.concurrent.Executor;
3839
import java.util.concurrent.ExecutorService;

src/listener/java/com/mcmoddev/mmdbot/thelistener/util/EventListener.java renamed to src/core/java/com/mcmoddev/mmdbot/core/util/jda/caching/InteractionData.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818
* USA
1919
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
2020
*/
21-
package com.mcmoddev.mmdbot.thelistener.util;
21+
package com.mcmoddev.mmdbot.core.util.jda.caching;
2222

23-
import discord4j.core.event.domain.Event;
23+
public interface InteractionData {
2424

25-
@FunctionalInterface
26-
public interface EventListener {
27-
28-
void onEvent(Event event);
25+
long getAuthorId();
2926

3027
}

src/core/java/com/mcmoddev/mmdbot/core/util/jda/caching/JdaMessageCache.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation;
8+
* Specifically version 2.1 of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.core.util.jda.caching;
222

323
import org.jetbrains.annotations.Nullable;

src/core/java/com/mcmoddev/mmdbot/core/util/jda/caching/JdaMessageCacheImpl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation;
8+
* Specifically version 2.1 of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.core.util.jda.caching;
222

323
import com.github.benmanes.caffeine.cache.Cache;

0 commit comments

Comments
 (0)