Created
April 7, 2020 14:08
-
-
Save Alex1304/97633d70963a5c743e3802a627598c91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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