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;
}
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());
}
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();
}
}
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());
}
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());
}
Aggregations