discord4j.store.api.mapping.MappingStoreService Java Examples

The following examples show how to use discord4j.store.api.mapping.MappingStoreService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DisCalClient.java    From DisCal-Discord-Bot with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates the DisCal bot client.
 *
 * @return The client if successful, otherwise <code>null</code>.
 */
private static DiscordClient createClient() {
	DiscordClientBuilder clientBuilder = new DiscordClientBuilder(BotSettings.TOKEN.get());
	//Handle shard count and index for multiple java instances
	clientBuilder.setShardIndex(Integer.valueOf(BotSettings.SHARD_INDEX.get()));
	clientBuilder.setShardCount(Integer.valueOf(BotSettings.SHARD_COUNT.get()));
	clientBuilder.setInitialPresence(Presence.online(Activity.playing("Booting Up!")));


	//Redis info + store service for caching
	if (BotSettings.USE_REDIS_STORES.get().equalsIgnoreCase("true")) {
		RedisURI uri = RedisURI.Builder
			.redis(BotSettings.REDIS_HOSTNAME.get(), Integer.valueOf(BotSettings.REDIS_PORT.get()))
			.withPassword(BotSettings.REDIS_PASSWORD.get())
			.build();

		RedisStoreService rss = new RedisStoreService(RedisClient.create(uri));

		MappingStoreService mss = MappingStoreService.create()
			.setMappings(rss, GuildBean.class, MessageBean.class)
			.setFallback(new JdkStoreService());

		clientBuilder.setStoreService(mss);
	} else {
		clientBuilder.setStoreService(new JdkStoreService());
	}

	return clientBuilder.build();
}
 
Example #2
Source File: ExampleStore.java    From Discord4J with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    JacksonResources jackson = JacksonResources.create();
    Map<String, AtomicLong> counts = new ConcurrentHashMap<>();
    DiscordClientBuilder.create(System.getenv("token"))
            .setJacksonResources(jackson)
            .build()
            .gateway()
            .setStoreService(MappingStoreService.create()
                    .setMapping(new NoOpStoreService(), MessageData.class)
                    .setFallback(new JdkStoreService()))
            .withGateway(gateway -> {
                log.info("Start!");

                Mono<Void> server = startHttpServer(gateway, counts, jackson.getObjectMapper()).then();

                Mono<Void> listener = gateway.on(GatewayLifecycleEvent.class)
                        .filter(e -> !e.getClass().equals(ReadyEvent.class))
                        .doOnNext(e -> log.info("[shard={}] {}", e.getShardInfo().format(), e.toString()))
                        .then();

                Mono<Void> eventCounter = Flux.fromIterable(reflections.getSubTypesOf(Event.class))
                        .filter(cls -> reflections.getSubTypesOf(cls).isEmpty())
                        .flatMap(type -> gateway.on(type).doOnNext(event -> {
                            String key = event.getClass().getSimpleName();
                            counts.computeIfAbsent(key, k -> new AtomicLong()).addAndGet(1);
                        }))
                        .then();

                return Mono.when(server, listener, eventCounter);
            })
            .block();
}
 
Example #3
Source File: DisCalClient.java    From DisCal-Discord-Bot with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates the DisCal bot client.
 *
 * @return The client if successful, otherwise <code>null</code>.
 */
private static DiscordClient createClient() {
	DiscordClientBuilder clientBuilder = new DiscordClientBuilder(BotSettings.TOKEN.get());
	//Handle shard count and index for multiple java instances
	clientBuilder.setShardIndex(Integer.valueOf(BotSettings.SHARD_INDEX.get()));
	clientBuilder.setShardCount(Integer.valueOf(BotSettings.SHARD_COUNT.get()));
	clientBuilder.setInitialPresence(Presence.online(Activity.playing("Booting Up!")));


	//Redis info + store service for caching
	if (BotSettings.USE_REDIS_STORES.get().equalsIgnoreCase("true")) {
		RedisURI uri = RedisURI.Builder
			.redis(BotSettings.REDIS_HOSTNAME.get(), Integer.valueOf(BotSettings.REDIS_PORT.get()))
			.withPassword(BotSettings.REDIS_PASSWORD.get())
			.build();

		RedisStoreService rss = new RedisStoreService(RedisClient.create(uri));

		MappingStoreService mss = MappingStoreService.create()
			.setMappings(rss, GuildBean.class, MessageBean.class)
			.setFallback(new JdkStoreService());

		clientBuilder.setStoreService(mss);
	} else {
		clientBuilder.setStoreService(new JdkStoreService());
	}

	return clientBuilder.build();
}