use of net.dv8tion.jda.api.sharding.ShardManager in project Sx4 by sx4-discord-bot.
the class TemporaryBanManager method removeBanAndGet.
public DeleteOneModel<Document> removeBanAndGet(long guildId, long userId, boolean automatic) {
ShardManager shardManager = this.bot.getShardManager();
Guild guild = shardManager.getGuildById(guildId);
if (guild == null) {
return null;
}
User user = shardManager.getUserById(userId);
Member member = user == null ? null : guild.getMember(user);
if (automatic && member == null) {
guild.unban(User.fromId(userId)).reason("Ban length served").queue();
}
if (automatic) {
Reason reason = new Reason("Ban length served");
UnbanEvent event = new UnbanEvent(guild.getSelfMember(), user == null ? UserReference.fromId(userId) : user, reason);
this.bot.getModActionManager().onModAction(event);
}
this.deleteExecutor(guildId, userId);
return new DeleteOneModel<>(Filters.and(Filters.eq("guildId", guildId), Filters.eq("userId", userId)));
}
use of net.dv8tion.jda.api.sharding.ShardManager in project SkyBot by duncte123.
the class AirUtils method handleExpiredReminders.
public static void handleExpiredReminders(List<Reminder> reminders, DatabaseAdapter adapter) {
// Get the shardManager and a list of ints to purge the ids for
final ShardManager shardManager = SkyBot.getInstance().getShardManager();
final List<Integer> toPurge = new ArrayList<>();
for (final Reminder reminder : reminders) {
// The reminder message template
final String message = String.format("%s you asked me to remind you about \"%s\"", TimeFormat.RELATIVE.format(reminder.getCreate_date()), reminder.getReminder().trim());
// If we have a channel send the message to that
if (reminder.getIn_channel()) {
final long channelId = reminder.getChannel_id();
final TextChannel channel = shardManager.getTextChannelById(channelId);
// skipping the continue statement makes sure that we roll into the dm part of this
if (channel != null) {
// Add the reminder to the list of the reminders to purge
toPurge.add(reminder.getId());
sendMsg(new MessageConfig.Builder().setChannel(channel).setMessage(String.format("<@%s>, %s", reminder.getUser_id(), message)).replyTo(reminder.getMessage_id()).build());
// go to the next one and don't run the user code if a channel was found
continue;
}
}
try {
Objects.requireNonNull(shardManager.getShardById(0)).openPrivateChannelById(reminder.getUser_id()).flatMap((c) -> c.sendMessage(message + "\n" + reminder.getJumpUrl())).complete();
toPurge.add(reminder.getId());
} catch (ErrorResponseException errorResponseEx) {
final ErrorResponse errorResponse = errorResponseEx.getErrorResponse();
if (// The account probably got deleted or something
errorResponse == ErrorResponse.UNKNOWN_USER || // we cannot dm this user (has dms blocked?)
errorResponse == ErrorResponse.CANNOT_SEND_TO_USER) {
toPurge.add(reminder.getId());
}
} catch (Exception e) {
Sentry.captureException(e);
}
}
// get a date that is 2 days in the future
final OffsetDateTime plusTwoDays = OffsetDateTime.now(ZoneOffset.UTC).plus(2L, ChronoUnit.DAYS);
// Remove any reminders that have not been removed after 2 days
final List<Integer> extraRemoval = reminders.stream().filter((reminder) -> reminder.getReminder_date().isAfter(plusTwoDays)).map(Reminder::getId).collect(Collectors.toList());
toPurge.addAll(extraRemoval);
if (!toPurge.isEmpty()) {
adapter.purgeRemindersSync(toPurge);
}
}
use of net.dv8tion.jda.api.sharding.ShardManager in project SkyBot by duncte123.
the class CommandUtils method getPatronGuildIds.
public static List<Long> getPatronGuildIds(long userId, ShardManager shardManager) {
if (ONEGUILD_PATRONS.containsKey(userId)) {
return List.of(ONEGUILD_PATRONS.get(userId));
}
final List<Long> guildIds = new ArrayList<>();
shardManager.getGuildCache().acceptStream((stream) -> stream.filter((guild) -> {
final Member member = guild.getMemberById(userId);
return member != null && member.hasPermission(Permission.ADMINISTRATOR);
}).forEach((guild) -> guildIds.add(guild.getIdLong())));
return guildIds;
}
use of net.dv8tion.jda.api.sharding.ShardManager in project Sx4 by sx4-discord-bot.
the class GiveawayCommand method list.
@Command(value = "list", description = "Lists all the giveaways which have happened in the server")
@CommandId(52)
@Examples({ "giveaway list" })
public void list(Sx4CommandEvent event) {
List<Document> giveaways = event.getMongo().getGiveaways(Filters.eq("guildId", event.getGuild().getIdLong())).into(new ArrayList<>());
if (giveaways.isEmpty()) {
event.replyFailure("No giveaways have been setup in this server").queue();
return;
}
PagedResult<Document> paged = new PagedResult<>(event.getBot(), giveaways).setAuthor("Giveaways", null, event.getGuild().getIconUrl()).setDisplayFunction(data -> {
long endAt = data.getLong("endAt"), timeNow = Clock.systemUTC().instant().getEpochSecond();
return data.getLong("messageId") + " - " + (endAt - timeNow < 0 ? "Ended" : TimeUtility.LONG_TIME_FORMATTER.parse(endAt - timeNow));
});
paged.onSelect(select -> {
ShardManager shardManager = event.getShardManager();
Document data = select.getSelected();
List<Long> winners = data.getList("winners", Long.class, Collections.emptyList());
String winnersString = winners.isEmpty() ? "None" : winners.stream().map(shardManager::getUserById).filter(Objects::nonNull).map(User::getAsMention).collect(Collectors.joining(", "));
event.replyFormat("**Giveaway %d**\nItem: %s\nWinner%s: %s\nDuration: %s", data.getLong("messageId"), data.getString("item"), winners.size() == 1 ? "" : "s", winnersString, TimeUtility.LONG_TIME_FORMATTER.parse(data.getLong("duration"))).queue();
});
paged.execute(event);
}
use of net.dv8tion.jda.api.sharding.ShardManager in project Sx4 by sx4-discord-bot.
the class GuessTheNumberGame method endGame.
public void endGame(InteractionHook hook) {
this.bot.getGuessTheNumberManager().removeGame(this);
ShardManager manager = this.bot.getShardManager();
ObjectId gameId = ObjectId.get();
int randomNumber = this.random.nextInt(this.max) + 1;
Document authorData = new Document("userId", this.userId).append("gameId", gameId).append("type", GameType.GUESS_THE_NUMBER.getId()).append("choice", this.userGuess).append("answer", randomNumber);
Document opponentData = new Document("userId", this.opponentId).append("gameId", gameId).append("type", GameType.GUESS_THE_NUMBER.getId()).append("choice", this.opponentGuess).append("answer", randomNumber);
int authorDifference = Math.abs(this.userGuess - randomNumber), opponentDifference = Math.abs(this.opponentGuess - randomNumber);
User user = manager.getUserById(this.userId), opponent = manager.getUserById(this.opponentId);
StringBuilder content = new StringBuilder("The random number was **" + randomNumber + "**\n" + (opponent == null ? this.opponentId : opponent.getName()) + "'s number was **" + this.opponentGuess + "**\n" + (user == null ? this.userId : user.getName()) + "'s number was **" + this.userGuess + "**\n\n");
if (authorDifference == opponentDifference) {
content.append("You both guessed the same number, It was a draw!");
authorData.append("state", GameState.DRAW.getId());
opponentData.append("state", GameState.DRAW.getId());
} else if (authorDifference > opponentDifference) {
content.append(opponent == null ? this.opponentId : opponent.getName()).append(" won! They were the closest to ").append(randomNumber);
authorData.append("state", GameState.LOSS.getId());
opponentData.append("state", GameState.WIN.getId());
} else {
content.append(user == null ? this.userId : user.getName()).append(" won! They were the closest to ").append(randomNumber);
authorData.append("state", GameState.WIN.getId());
opponentData.append("state", GameState.LOSS.getId());
}
hook.sendMessage(content.toString()).queue();
hook.editMessageComponentsById(this.messageId, ActionRow.of(Button.primary("a", "Guess the number").asDisabled())).queue();
this.bot.getMongo().insertManyGames(List.of(authorData, opponentData)).whenComplete(MongoDatabase.exceptionally());
}
Aggregations