Search in sources :

Example 1 with RequestFuture

use of sx.blah.discord.util.RequestBuffer.RequestFuture in project Shadbot by Shadorc.

the class BotUtils method sendMessage.

public static RequestFuture<IMessage> sendMessage(MessageBuilder message, int retry) {
    IGuild guild = message.getChannel().isPrivate() ? null : message.getChannel().getGuild();
    long guildID = guild == null ? -1 : guild.getLongID();
    if (retry == 0) {
        LogUtils.infof("{Guild ID: %d} Abort attempt to send message (3 failed requests).", guildID);
        return null;
    }
    if (!message.getChannel().getShard().isReady()) {
        if (guild != null) {
            LogUtils.infof("{Guild ID: %d} A message couldn't be sent because shard isn't ready, adding it to queue.", guildID);
            ShardManager.getShadbotShard(guild.getShard()).queue(message);
        }
        return null;
    }
    return RequestBuffer.request(() -> {
        try {
            return message.send();
        } catch (MissingPermissionsException err) {
            BotUtils.sendMessage(TextUtils.missingPerm(err.getMissingPermissions()), message.getChannel());
            LogUtils.infof("{Guild ID: %d} %s", guildID, err.getMessage());
        } catch (DiscordException err) {
            if (err.getMessage().contains("Message was unable to be sent (Discord didn't return a response)")) {
                LogUtils.infof("{Guild ID: %d} A message could not be send because Discord didn't return a response, retrying.", guildID);
                RequestFuture<IMessage> msgRequest = BotUtils.sendMessage(message, retry - 1);
                if (msgRequest != null) {
                    return msgRequest.get();
                }
            } else if (err.getMessage().contains("Failed to make a 400 failed request after 5 tries!")) {
                LogUtils.infof("{Guild ID: %d} %s", guildID, err.getMessage());
            } else {
                LogUtils.error(err, "An error occurred while sending message.");
            }
        }
        return null;
    });
}
Also used : RequestFuture(sx.blah.discord.util.RequestBuffer.RequestFuture) DiscordException(sx.blah.discord.util.DiscordException) MissingPermissionsException(sx.blah.discord.util.MissingPermissionsException) IGuild(sx.blah.discord.handle.obj.IGuild)

Example 2 with RequestFuture

use of sx.blah.discord.util.RequestBuffer.RequestFuture in project Shadbot by Shadorc.

the class HangmanManager method show.

private void show() {
    List<String> missesList = charsTested.stream().filter(letter -> !word.contains(letter)).collect(Collectors.toList());
    EmbedBuilder embed = EmbedUtils.getDefaultEmbed().setLenient(true).withAuthorIcon(this.getAuthor().getAvatarURL()).withAuthorName("Hangman Game").withThumbnail("https://lh5.ggpht.com/nIoJylIWCj1gKv9dxtd4CFE2aeXvG7MbvP0BNFTtTFusYlxozJRQmHizsIDxydaa7DHT=w300").withDescription("Type letters or enter a word if you think you've guessed it.").appendField("Word", this.getRepresentation(word), false).appendField("Misses", FormatUtils.format(missesList, chr -> chr.toString().toUpperCase(), ", "), false);
    if (this.isTaskDone()) {
        embed.withFooterText("Finished.");
    } else {
        embed.withFooterText(String.format("Use %scancel to cancel this game (Automatically cancelled in %d min in case of inactivity)", this.getPrefix(), IDLE_MIN));
    }
    if (failsCount > 0) {
        embed.withImage(IMG_LIST.get(Math.min(IMG_LIST.size(), failsCount) - 1));
    }
    RequestFuture<IMessage> msgRequest = message.send(embed.build());
    if (msgRequest != null) {
        msgRequest.get();
    }
}
Also used : MoneyEnum(me.shadorc.shadbot.data.stats.MoneyStatsManager.MoneyEnum) BotUtils(me.shadorc.shadbot.utils.BotUtils) ArrayList(java.util.ArrayList) StringUtils(me.shadorc.shadbot.utils.StringUtils) IMessage(sx.blah.discord.handle.obj.IMessage) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) IUser(sx.blah.discord.handle.obj.IUser) AbstractGameManager(me.shadorc.shadbot.core.game.AbstractGameManager) UpdateableMessage(me.shadorc.shadbot.utils.object.UpdateableMessage) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) Collectors(java.util.stream.Collectors) MessageListener(me.shadorc.shadbot.message.MessageListener) TimeUnit(java.util.concurrent.TimeUnit) MoneyStatsManager(me.shadorc.shadbot.data.stats.MoneyStatsManager) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) RequestFuture(sx.blah.discord.util.RequestBuffer.RequestFuture) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) Database(me.shadorc.shadbot.data.db.Database) IChannel(sx.blah.discord.handle.obj.IChannel) Difficulty(me.shadorc.shadbot.command.game.hangman.HangmanCmd.Difficulty) RateLimiter(me.shadorc.shadbot.ratelimiter.RateLimiter) Emoji(me.shadorc.shadbot.utils.object.Emoji) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) MessageManager(me.shadorc.shadbot.message.MessageManager) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) IMessage(sx.blah.discord.handle.obj.IMessage)

Aggregations

RequestFuture (sx.blah.discord.util.RequestBuffer.RequestFuture)2 ChronoUnit (java.time.temporal.ChronoUnit)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Difficulty (me.shadorc.shadbot.command.game.hangman.HangmanCmd.Difficulty)1 AbstractCommand (me.shadorc.shadbot.core.command.AbstractCommand)1 AbstractGameManager (me.shadorc.shadbot.core.game.AbstractGameManager)1 Database (me.shadorc.shadbot.data.db.Database)1 MoneyStatsManager (me.shadorc.shadbot.data.stats.MoneyStatsManager)1 MoneyEnum (me.shadorc.shadbot.data.stats.MoneyStatsManager.MoneyEnum)1 MessageListener (me.shadorc.shadbot.message.MessageListener)1 MessageManager (me.shadorc.shadbot.message.MessageManager)1 RateLimiter (me.shadorc.shadbot.ratelimiter.RateLimiter)1 BotUtils (me.shadorc.shadbot.utils.BotUtils)1 FormatUtils (me.shadorc.shadbot.utils.FormatUtils)1 StringUtils (me.shadorc.shadbot.utils.StringUtils)1 EmbedUtils (me.shadorc.shadbot.utils.embed.EmbedUtils)1 Emoji (me.shadorc.shadbot.utils.object.Emoji)1