Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text cleanup #57

Merged
merged 3 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,14 @@
import com.locibot.locibot.core.command.CommandPermission;
import com.locibot.locibot.core.command.Context;
import com.locibot.locibot.database.DatabaseManager;
import com.locibot.locibot.database.events_db.entity.DBEvent;
import com.locibot.locibot.database.events_db.entity.DBEventMember;
import discord4j.core.object.command.ApplicationCommandOption;
import discord4j.core.object.component.ActionRow;
import discord4j.core.object.component.Button;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.User;
import discord4j.core.spec.EmbedCreateFields;
import discord4j.core.spec.EmbedCreateSpec;
import discord4j.core.spec.InteractionFollowupCreateSpec;
import discord4j.core.spec.MessageCreateSpec;
import discord4j.rest.util.Color;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class AddUserEventCmd extends BaseCmd {
protected AddUserEventCmd() {
Expand All @@ -40,24 +28,24 @@ protected AddUserEventCmd() {
public Mono<?> execute(Context context) {
String eventTitle = context.getOptionAsString("title").orElseThrow();
if (!DatabaseManager.getEvents().containsEvent(eventTitle)) {
return context.createFollowupMessage("Only the event-owner is allowed to add users!");
return context.createFollowupMessage(context.localize("event.add.restriction"));
}
List<Mono<User>> users = new ArrayList<>();
for (int i = 0; i < 10; i++) {
if (context.getOptionAsUser("member_" + i) != null)
users.add(context.getOptionAsUser("member_" + i));
}

return context.createFollowupMessage("Users added")
return context.createFollowupMessage(context.localize("event.add.success"))
.then(Flux.fromIterable(users).flatMap(userMono -> userMono.map(user -> user)).collectList().flatMap(usersNoMono ->
Flux.fromIterable(usersNoMono).flatMap(user -> DatabaseManager.getEvents().getDBEvent(eventTitle).flatMap(dbEvent ->
DatabaseManager.getGuilds().getDBMember(context.getGuildId(), user.getId()).flatMap(dbMember -> {
if (user.isBot()) {
return context.getChannel().flatMap(textChannel -> textChannel.getGuild().flatMap(guild -> guild.getMemberById(user.getId()).flatMap(member ->
context.createFollowupMessageEphemeral(member.getNickname().orElse(user.getUsername()) + " is a bot and can not be messaged."))));
context.createFollowupMessageEphemeral(member.getNickname().orElse(user.getUsername()) + context.localize("event.add.warning")))));
} else if (dbMember.getBotRegister() && dbEvent.isScheduled())
return EventUtil.privateInvite(context, dbEvent, user);
else if (dbEvent.isScheduled()){
else if (dbEvent.isScheduled()) {
return EventUtil.publicInvite(context, dbEvent, new ArrayList<>(Collections.singleton(user.getUsername())));
}
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ protected CreateEventCmd() {
public Mono<?> execute(Context context) {

if (DatabaseManager.getGuilds().getDBMember(context.getGuildId(), context.getAuthorId()) == null)
return context.createFollowupMessage("You can only create an event if you have registered with the bot.\n" +
"If your server does not have a register button, contact a moderator.");
return context.createFollowupMessage(context.localize("event.create.restriction"));

String eventName = context.getOptionAsString("title").orElse("error");
if (DatabaseManager.getEvents().containsEvent(eventName))
return context.createFollowupMessage("This group name is already taken. Pls try another one.");
DBEvent event = new DBEvent(eventName, context.getOptionAsString("description"). orElse(null), context.getOptionAsString("icon").orElse(null));
return context.createFollowupMessage(context.localize("event.create.group"));
DBEvent event = new DBEvent(eventName, context.getOptionAsString("description").orElse(null), context.getOptionAsString("icon").orElse(null));
List<Mono<User>> users = new ArrayList<>();

for (int i = 1; i < 11; i++) {
Expand All @@ -48,6 +47,6 @@ public Mono<?> execute(Context context) {
.then(new DBEventMember(context.getEvent().getInteraction().getUser().getId().asLong(), eventName, 1, true).insert())
//add Members
.thenMany(Flux.concat(users).flatMap(user -> new DBEventMember(user.getId().asLong(), eventName, 0, false).insert()))
.then(context.createFollowupMessage("Created"));
.then(context.createFollowupMessage(context.localize("event.create.success")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public Mono<?> execute(Context context) {
{
//check if author is owner
if (event.getOwner().getId().equals(context.getAuthorId())) {
return event.delete().then(context.createFollowupMessage("Event **" + eventName + "** has been deleted!"));
return event.delete().then(context.createFollowupMessage(context.localize("event.delete.success").formatted(eventName)));
}
return context.createFollowupMessage("You are not the owner of " + eventName + "!");
return context.createFollowupMessage(context.localize("event.delete.restriction").formatted(eventName));
});
}
return context.createFollowupMessage(eventName + " does not exist!");
return context.createFollowupMessage(context.localize("event.delete.empty").formatted(eventName));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ static Mono<Message> publicInvite(Context context, DBEvent dbEvent, List<String>
.title("Event invitation")
.thumbnail(dbEvent.getIcon() == null ? owner.getAvatarUrl() : dbEvent.getIcon())
.description("Invitation to **" + dbEvent.getEventName() + "**")
.footer(EmbedCreateFields.Footer.of("Only users who do not want to receive direct messages from the bot are listed above.\nAuthor: " + owner.getUsername(), owner.getAvatarUrl()))
.addField(EmbedCreateFields.Field.of("Description", dbEvent.getEventDescription() + "\n", false))
.addField(EmbedCreateFields.Field.of("Date & Time",
"%s Uhr".formatted(ZonedDateTime.ofInstant(
.footer(EmbedCreateFields.Footer.of(context.localize("event.util.footer").formatted(owner.getUsername()), owner.getAvatarUrl()))
.addField(EmbedCreateFields.Field.of(context.localize("event.description"), dbEvent.getEventDescription() + "\n", false))
.addField(EmbedCreateFields.Field.of(context.localize("event.util.date"),
context.localize("event.time").formatted(ZonedDateTime.ofInstant(
Instant.ofEpochSecond(dbEvent.getBean().getScheduledDate()),
ZoneId.of("Europe/Berlin")).format(DateTimeFormatter.ofPattern("dd.MM.yyyy, hh:mm"))),
false))
.addField(EmbedCreateFields.Field.of("Users", usersString.stream().map(String::toString).collect(Collectors.joining(",")) + "\n", false))
.addField(EmbedCreateFields.Field.of(context.localize("event.util.users"), usersString.stream().map(String::toString).collect(Collectors.joining(",")) + "\n", false))
.build())
.content(usersString.stream().map(String::toString).collect(Collectors.joining(",")))
.addComponent(createButtons(dbEvent))
.addComponent(createButtons(dbEvent, context))
.build()));
}

Expand All @@ -52,24 +52,24 @@ static Mono<Message> privateInvite(Context context, DBEvent dbEvent, User user)
privateChannel.createMessage(MessageCreateSpec.builder()
.addEmbed(EmbedCreateSpec.builder()
.color(Color.BLUE)
.title("Event invitation")
.title(context.localize("event.util.invitation"))
.thumbnail(dbEvent.getIcon() == null ? owner.getAvatarUrl() : dbEvent.getIcon())
.description("You got invited to **" + dbEvent.getEventName() + "**")
.addField(EmbedCreateFields.Field.of("Date & Time",
"%s Uhr".formatted(ZonedDateTime.ofInstant(
.addField(EmbedCreateFields.Field.of(context.localize("event.util.date"),
context.localize("event.time").formatted(ZonedDateTime.ofInstant(
Instant.ofEpochSecond(dbEvent.getBean().getScheduledDate()),
ZoneId.of("Europe/Berlin")).format(DateTimeFormatter.ofPattern("dd.MM.yyyy, hh:mm"))),
false))
.footer(EmbedCreateFields.Footer.of("Author: " + owner.getUsername(), owner.getAvatarUrl()))
.addField(EmbedCreateFields.Field.of("Description", dbEvent.getEventDescription() + "\n", false))
.footer(EmbedCreateFields.Footer.of(context.localize("event.author").formatted(owner.getUsername()), owner.getAvatarUrl()))
.addField(EmbedCreateFields.Field.of(context.localize("event.description"), dbEvent.getEventDescription() + "\n", false))
.build())
.addComponent(createButtons(dbEvent)).build())));
.addComponent(createButtons(dbEvent, context)).build())));
}

@NotNull
private static ActionRow createButtons(DBEvent dbEvent) {
private static ActionRow createButtons(DBEvent dbEvent, Context context) {
return ActionRow.of(
Button.success("acceptInviteButton_" + dbEvent.getEventName(), "Accept"),
Button.danger("declineInviteButton_" + dbEvent.getEventName(), "Decline"));
Button.success("acceptInviteButton_" + dbEvent.getEventName(), context.localize("event.util.button.accept")),
Button.danger("declineInviteButton_" + dbEvent.getEventName(), context.localize("event.util.button.decline")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,34 @@ public Mono<?> execute(Context context) {
String title = context.getOptionAsString("event_title").orElseThrow();

if (!DatabaseManager.getEvents().containsEvent(title))
return context.createFollowupMessage("No event found with the title: " + title);
return context.createFollowupMessage(context.localize("event.publish.title.error").formatted(title));

return DatabaseManager.getEvents().getDBEvent(title).flatMap(dbEvent -> {
if (!dbEvent.isScheduled())
return context.createFollowupMessage("The event must first be scheduled to be published!");
return context.createFollowupMessage(context.localize("event.publish.schedule"));
if (dbEvent.getOwner().getId().asLong() == context.getAuthor().getId().asLong())
//noinspection ConstantConditions
return context.getGuild().flatMap(guild -> guild.getMemberById(dbEvent.getOwner().getId())).flatMap(owner ->
context.createFollowupMessage(InteractionFollowupCreateSpec.builder()
.content("@everyone")
.addEmbed(EmbedCreateSpec.builder()
.color(Color.BLUE)
.title("Event")
.title(context.localize("event.publish.title"))
.thumbnail(dbEvent.getIcon() == null ? owner.getAvatarUrl() : dbEvent.getIcon())
.description("Join the event **%s**".formatted(dbEvent.getEventName()))
.footer(EmbedCreateFields.Footer.of("Author: %s".formatted(owner.getUsername()), owner.getAvatarUrl()))
.addField(EmbedCreateFields.Field.of("Description", dbEvent.getEventDescription() + "\n", false))
.addField(EmbedCreateFields.Field.of("Date",
"%s Uhr".formatted(ZonedDateTime.ofInstant(
.description(context.localize("event.publish.join").formatted(dbEvent.getEventName()))
.footer(EmbedCreateFields.Footer.of(context.localize("event.author").formatted(owner.getUsername()), owner.getAvatarUrl()))
.addField(EmbedCreateFields.Field.of(context.localize("event.description"), dbEvent.getEventDescription() + "\n", false))
.addField(EmbedCreateFields.Field.of(context.localize("event.publish.date"),
context.localize("event.time").formatted(ZonedDateTime.ofInstant(
Instant.ofEpochSecond(dbEvent.getBean().getScheduledDate()),
ZoneId.of("Europe/Berlin")).format(DateTimeFormatter.ofPattern("dd.MM.yyyy, hh:mm"))),
false))
.build())
.addComponent(ActionRow.of(
Button.success("joinButton_" + title, "Join"),
Button.danger("leaveButton_" + title, "Leave")))
Button.success("joinButton_" + title, context.localize("event.publish.button.join")),
Button.danger("leaveButton_" + title, context.localize("event.publish.button.leave"))))
.build()));
return context.createFollowupMessage("You are not the event owner, so you can not publish it.");
return context.createFollowupMessage(context.localize("event.publish.restriction"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@
import com.locibot.locibot.core.command.CommandPermission;
import com.locibot.locibot.core.command.Context;
import com.locibot.locibot.database.DatabaseManager;
import com.locibot.locibot.database.events_db.entity.DBEvent;
import com.locibot.locibot.database.events_db.entity.DBEventMember;
import discord4j.core.object.command.ApplicationCommandOption;
import discord4j.core.object.component.ActionRow;
import discord4j.core.object.component.Button;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.User;
import discord4j.core.spec.EmbedCreateFields;
import discord4j.core.spec.EmbedCreateSpec;
import discord4j.core.spec.InteractionFollowupCreateSpec;
import discord4j.core.spec.MessageCreateSpec;
import discord4j.rest.util.Color;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -46,14 +37,14 @@ public Mono<?> execute(Context context) {
return context.createFollowupMessage("Event scheduled!").then(
DatabaseManager.getEvents().getDBEvent(context.getOptionAsString("event_title").orElseThrow()).flatMap(dbEvent -> {
if (!dbEvent.getOwner().getBean().getId().equals(context.getAuthor().getId().asLong())) {
return context.createFollowupMessage("Only the event owner is allowed to create a schedule!");
return context.createFollowupMessage(context.localize("event.schedule.owner"));
}
return dbEvent.updateSchedules(ZonedDateTime.of(LocalDateTime.of(newDate, newTime), ZoneId.of("Europe/Berlin")))
.delayElement(Duration.ofSeconds(1)).then(Flux.fromIterable(dbEvent.getMembers()).concatMap(dbEventMember ->
context.getClient().getUserById(dbEventMember.getId()).flatMap(user -> DatabaseManager.getGuilds().getDBMember(context.getGuildId(), user.getId()).flatMap(dbMember -> {
if (user.isBot()) {
return context.getChannel().flatMap(textChannel -> textChannel.getGuild().flatMap(guild -> guild.getMemberById(user.getId()).flatMap(member ->
context.createFollowupMessageEphemeral(member.getNickname().orElse(user.getUsername()) + " is a bot and can not be messaged."))));
context.createFollowupMessageEphemeral(context.localize("event.schedule.bot").formatted(member.getNickname().orElse(user.getUsername()))))));
} else if (dbMember.getBotRegister())
return EventUtil.privateInvite(context, dbEvent, user);
else {
Expand Down
Loading