Search in sources :

Example 26 with IUser

use of sx.blah.discord.handle.obj.IUser in project Shadbot by Shadorc.

the class BotUtils method getUsersFrom.

public static List<IUser> getUsersFrom(IMessage message) {
    List<IUser> users = new ArrayList<>(message.getMentions());
    for (IRole role : message.getRoleMentions()) {
        users.addAll(message.getGuild().getUsersByRole(role));
    }
    users = users.stream().distinct().collect(Collectors.toList());
    return users;
}
Also used : IRole(sx.blah.discord.handle.obj.IRole) ArrayList(java.util.ArrayList) IUser(sx.blah.discord.handle.obj.IUser)

Example 27 with IUser

use of sx.blah.discord.handle.obj.IUser in project Shadbot by Shadorc.

the class RolelistCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    List<IRole> roles = context.getMessage().getRoleMentions();
    if (roles.isEmpty()) {
        throw new MissingArgumentException();
    }
    List<IUser> users = roles.stream().flatMap(role -> context.getGuild().getUsersByRole(role).stream()).distinct().collect(Collectors.toList());
    // Only keep elements common to all users list
    roles.stream().forEach(role -> users.retainAll(context.getGuild().getUsersByRole(role)));
    EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Role list").withDescription(String.format("Members with role(s) **%s**", FormatUtils.format(roles, IRole::getName, ", ")));
    if (users.isEmpty()) {
        embed.appendDescription(String.format("There is nobody with %s.", roles.size() == 1 ? "this role" : "these roles"));
    } else {
        FormatUtils.createColumns(users.stream().map(IUser::getName).collect(Collectors.toList()), 25).stream().forEach(embed::appendField);
    }
    BotUtils.sendMessage(embed.build(), context.getChannel());
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) IRole(sx.blah.discord.handle.obj.IRole) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IUser(sx.blah.discord.handle.obj.IUser)

Example 28 with IUser

use of sx.blah.discord.handle.obj.IUser in project Shadbot by Shadorc.

the class LottoCmd method draw.

public static void draw() {
    LogUtils.infof("Lottery draw started...");
    int winningNum = ThreadLocalRandom.current().nextInt(MIN_NUM, MAX_NUM + 1);
    List<LottoPlayer> winners = LottoManager.getPlayers().stream().filter(player -> player.getNum() == winningNum && Shadbot.getClient().getGuildByID(player.getGuildID()) != null && Shadbot.getClient().getUserByID(player.getUserID()) != null).collect(Collectors.toList());
    for (LottoPlayer winner : winners) {
        IGuild guild = Shadbot.getClient().getGuildByID(winner.getGuildID());
        IUser user = Shadbot.getClient().getUserByID(winner.getUserID());
        int coins = (int) Math.ceil((double) LottoManager.getPool() / winners.size());
        Database.getDBUser(guild, user).addCoins(coins);
        BotUtils.sendMessage(String.format("Congratulations, you have the winning Lotto number! You earn %s.", FormatUtils.formatCoins(coins)), user.getOrCreatePMChannel());
    }
    LogUtils.infof("Lottery draw done (Winning number: %d | %d winner(s) | Prize pool: %d)", winningNum, winners.size(), LottoManager.getPool());
    LottoManager.setHistoric(winners.size(), LottoManager.getPool(), winningNum);
    LottoManager.resetUsers();
    if (!winners.isEmpty()) {
        LottoManager.resetPool();
    }
}
Also used : HelpBuilder(me.shadorc.shadbot.utils.embed.HelpBuilder) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) LottoManager(me.shadorc.shadbot.data.lotto.LottoManager) ZonedDateTime(java.time.ZonedDateTime) LogUtils(me.shadorc.shadbot.utils.LogUtils) DBUser(me.shadorc.shadbot.data.db.DBUser) LottoPlayer(me.shadorc.shadbot.data.lotto.LottoPlayer) BotUtils(me.shadorc.shadbot.utils.BotUtils) TimeUtils(me.shadorc.shadbot.utils.TimeUtils) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) Command(me.shadorc.shadbot.core.command.annotation.Command) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) IUser(sx.blah.discord.handle.obj.IUser) TextUtils(me.shadorc.shadbot.utils.TextUtils) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RateLimited(me.shadorc.shadbot.core.command.annotation.RateLimited) LottoHistoric(me.shadorc.shadbot.data.lotto.LottoHistoric) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) Collectors(java.util.stream.Collectors) Shadbot(me.shadorc.shadbot.Shadbot) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) CastUtils(me.shadorc.shadbot.utils.CastUtils) IGuild(sx.blah.discord.handle.obj.IGuild) List(java.util.List) Context(me.shadorc.shadbot.core.command.Context) DayOfWeek(java.time.DayOfWeek) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) Database(me.shadorc.shadbot.data.db.Database) Emoji(me.shadorc.shadbot.utils.object.Emoji) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) LottoPlayer(me.shadorc.shadbot.data.lotto.LottoPlayer) IUser(sx.blah.discord.handle.obj.IUser) IGuild(sx.blah.discord.handle.obj.IGuild)

Example 29 with IUser

use of sx.blah.discord.handle.obj.IUser in project Shadbot by Shadorc.

the class BanCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    List<IUser> mentionedUsers = context.getMessage().getMentions();
    if (mentionedUsers.isEmpty()) {
        throw new MissingArgumentException();
    }
    if (!PermissionUtils.hasPermissions(context.getChannel(), context.getAuthor(), Permissions.BAN)) {
        throw new IllegalArgumentException("You don't have permission to ban.");
    }
    if (!BotUtils.hasPermissions(context.getChannel(), Permissions.BAN)) {
        BotUtils.sendMessage(TextUtils.missingPerm(Permissions.BAN), context.getChannel());
        return;
    }
    if (mentionedUsers.contains(context.getAuthor())) {
        throw new IllegalCmdArgumentException("You cannot ban yourself.");
    }
    for (IUser mentionedUser : mentionedUsers) {
        if (!PermissionUtils.isUserHigher(context.getGuild(), context.getAuthor(), mentionedUser)) {
            throw new IllegalCmdArgumentException(String.format("You can't ban **%s** because he has the same or a higher role " + "position than you in the role hierarchy.", mentionedUser.getName()));
        }
        if (!BotUtils.canInteract(context.getGuild(), mentionedUser)) {
            throw new IllegalCmdArgumentException(String.format("I cannot ban **%s** because he has the same or a higher role " + "position than me in the role hierarchy.", mentionedUser.getName()));
        }
    }
    StringBuilder reason = new StringBuilder();
    reason.append(StringUtils.remove(context.getArg(), FormatUtils.format(mentionedUsers, user -> user.mention(false), " ")).trim());
    if (reason.length() > Ban.MAX_REASON_LENGTH) {
        throw new IllegalCmdArgumentException(String.format("Reason cannot exceed **%d characters**.", Ban.MAX_REASON_LENGTH));
    }
    if (reason.length() == 0) {
        reason.append("Reason not specified.");
    }
    for (IUser user : mentionedUsers) {
        if (!user.isBot()) {
            BotUtils.sendMessage(String.format(Emoji.INFO + " You were banned from the server **%s** by **%s**. Reason: `%s`", context.getGuild().getName(), context.getAuthorName(), reason), user.getOrCreatePMChannel());
        }
        RequestBuffer.request(() -> {
            context.getGuild().banUser(user, reason.toString(), 7);
        }).get();
    }
    BotUtils.sendMessage(String.format(Emoji.INFO + " (Requested by **%s**) **%s** got banned. Reason: `%s`", context.getAuthorName(), FormatUtils.format(mentionedUsers, IUser::getName, ", "), reason), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) Ban(sx.blah.discord.util.Ban) RequestBuffer(sx.blah.discord.util.RequestBuffer) HelpBuilder(me.shadorc.shadbot.utils.embed.HelpBuilder) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) PermissionUtils(sx.blah.discord.util.PermissionUtils) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) BotUtils(me.shadorc.shadbot.utils.BotUtils) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) StringUtils(me.shadorc.shadbot.utils.StringUtils) CommandPermission(me.shadorc.shadbot.core.command.CommandPermission) Command(me.shadorc.shadbot.core.command.annotation.Command) List(java.util.List) IUser(sx.blah.discord.handle.obj.IUser) Context(me.shadorc.shadbot.core.command.Context) TextUtils(me.shadorc.shadbot.utils.TextUtils) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) Emoji(me.shadorc.shadbot.utils.object.Emoji) Permissions(sx.blah.discord.handle.obj.Permissions) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IUser(sx.blah.discord.handle.obj.IUser)

Example 30 with IUser

use of sx.blah.discord.handle.obj.IUser in project Shadbot by Shadorc.

the class DiceManager method stop.

@Override
public void stop() {
    this.cancelScheduledTask();
    int winningNum = ThreadLocalRandom.current().nextInt(1, 7);
    List<String> list = new ArrayList<>();
    for (int num : numsPlayers.keySet()) {
        IUser user = numsPlayers.get(num);
        int gains = bet;
        if (num == winningNum) {
            gains *= numsPlayers.size() + DiceCmd.MULTIPLIER;
            MoneyStatsManager.log(MoneyEnum.MONEY_GAINED, this.getCmdName(), gains);
        } else {
            gains *= -1;
            MoneyStatsManager.log(MoneyEnum.MONEY_LOST, this.getCmdName(), Math.abs(gains));
        }
        list.add(gains > 0 ? 0 : list.size(), String.format("%s (**%s**)", user.getName(), FormatUtils.formatCoins(gains)));
        Database.getDBUser(this.getGuild(), user).addCoins(gains);
    }
    RequestFuture<IMessage> msgRequest = BotUtils.sendMessage(String.format(Emoji.DICE + " The dice is rolling... **%s** !", winningNum), this.getChannel());
    if (msgRequest != null) {
        msgRequest.get();
    }
    this.results = FormatUtils.format(list, Object::toString, "\n");
    this.show();
    numsPlayers.clear();
    DiceCmd.MANAGERS.remove(this.getChannel().getLongID());
}
Also used : IMessage(sx.blah.discord.handle.obj.IMessage) ArrayList(java.util.ArrayList) IUser(sx.blah.discord.handle.obj.IUser)

Aggregations

IUser (sx.blah.discord.handle.obj.IUser)67 ArrayList (java.util.ArrayList)15 IGuild (sx.blah.discord.handle.obj.IGuild)13 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)13 XEmbedBuilder (com.github.vaerys.objects.XEmbedBuilder)9 IMessage (sx.blah.discord.handle.obj.IMessage)9 IRole (sx.blah.discord.handle.obj.IRole)8 List (java.util.List)7 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)7 IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)6 IChannel (sx.blah.discord.handle.obj.IChannel)6 HashMap (java.util.HashMap)5 CCommandObject (com.github.vaerys.objects.CCommandObject)4 AbstractCommand (me.shadorc.shadbot.core.command.AbstractCommand)4 CommandCategory (me.shadorc.shadbot.core.command.CommandCategory)4 Context (me.shadorc.shadbot.core.command.Context)4 Command (me.shadorc.shadbot.core.command.annotation.Command)4 BotUtils (me.shadorc.shadbot.utils.BotUtils)4 FormatUtils (me.shadorc.shadbot.utils.FormatUtils)4 TextUtils (me.shadorc.shadbot.utils.TextUtils)4