Skip to content

Instantly share code, notes, and snippets.

@Alex1304
Created April 7, 2020 14:08
Show Gist options
  • Save Alex1304/97633d70963a5c743e3802a627598c91 to your computer and use it in GitHub Desktop.
Save Alex1304/97633d70963a5c743e3802a627598c91 to your computer and use it in GitHub Desktop.
package com.github.alex1304.ultimategdbot.core;
import static reactor.function.TupleUtils.consumer;
import java.time.Duration;
import com.github.alex1304.ultimategdbot.api.command.Context;
import com.github.alex1304.ultimategdbot.api.command.PermissionLevel;
import com.github.alex1304.ultimategdbot.api.command.annotated.CommandAction;
import com.github.alex1304.ultimategdbot.api.command.annotated.CommandDescriptor;
import com.github.alex1304.ultimategdbot.api.command.annotated.CommandDoc;
import com.github.alex1304.ultimategdbot.api.command.annotated.CommandPermission;
import com.github.alex1304.ultimategdbot.api.util.BotUtils;
import discord4j.core.object.entity.Guild;
import discord4j.core.object.entity.channel.TextChannel;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.Logger;
import reactor.util.Loggers;
@CommandDescriptor(
aliases = "burst",
shortDescription = "Pings the bot to check if it is alive."
)
@CommandPermission(level = PermissionLevel.BOT_OWNER)
class BurstCommand {
private static final Logger LOGGER = Loggers.getLogger(BurstCommand.class);
@CommandAction
@CommandDoc("Bursts messages in test channels")
public Mono<Void> run(Context ctx) {
return ctx.event().getGuild()
.flatMapMany(Guild::getChannels)
.ofType(TextChannel.class)
.filter(channel -> channel.getName().startsWith("test"))
.flatMap(channel -> Flux.range(1, 5)
.flatMap(tick -> channel.createMessage("burst " + tick)
.log("burst#" + channel.getName() + "#" + tick)))
.count()
.elapsed()
.doOnNext(consumer((elapsed, count) -> LOGGER.debug(
"Sent " + count + " message in " + BotUtils.formatDuration(Duration.ofMillis(elapsed)))))
.switchIfEmpty(Mono.fromRunnable(() -> LOGGER.warn("Completed empty")))
.then();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment