Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/xdnw/locutus
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Jun 26, 2024
2 parents 21c7257 + c0f4202 commit 91b1669
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 40 deletions.
6 changes: 5 additions & 1 deletion src/main/java/link/locutus/discord/Locutus.java
Original file line number Diff line number Diff line change
Expand Up @@ -1140,11 +1140,15 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event) {
public void onButtonInteraction(@NotNull ButtonInteractionEvent event) {
try {
Message message = event.getMessage();
// Only process locutus buttons

Button button = event.getButton();
System.out.println("Button press " + button.getId() + " | " + button.getLabel());

if (button.getId().equalsIgnoreCase("")) {
RateLimitUtil.queue(message.delete());
return;
}

if (message.getAuthor().getIdLong() != Settings.INSTANCE.APPLICATION_ID) {
System.out.println("Author not application");
return;
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/link/locutus/discord/commands/account/CheckMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,13 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m
channel.sendMessage("Please wait...");

GuildDB db = Locutus.imp().getGuildDB(guild);
SpreadSheet sheet = SpreadSheet.create(db, SheetKey.MAIL_RESPONSES_SHEET);

List<String> header = new ArrayList<>(Arrays.asList(
"nation",
"alliance",
"mail-id",
"subject",
"response"
));

sheet.setHeader(header);

boolean checkUnread = Boolean.parseBoolean(args.get(1));
boolean checkRead = Boolean.parseBoolean(args.get(2));
boolean readContent = Boolean.parseBoolean(args.get(3));
boolean group = flags.contains('g');
boolean count = flags.contains('c');

Map<DBNation, Map<Mail, List<String>>> results = new LinkedHashMap<>();

Expand All @@ -77,8 +69,17 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m
});
task.call();

boolean group = flags.contains('g');
boolean count = flags.contains('g');
SpreadSheet sheet = SpreadSheet.create(db, SheetKey.MAIL_RESPONSES_SHEET);

List<String> header = new ArrayList<>(Arrays.asList(
"nation",
"alliance",
"mail-id",
"subject",
"response"
));

sheet.setHeader(header);

List<Object> row = new ArrayList<>();
for (Map.Entry<DBNation, Map<Mail, List<String>>> entry : results.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ default IMessageBuilder cancelButton() {
return commandButton(CommandBehavior.DELETE_MESSAGE, " ", "Cancel");
}

@CheckReturnValue
default IMessageBuilder cancelButton(String label) {
return commandButton(CommandBehavior.DELETE_MESSAGE, " ", label);
}

@CheckReturnValue
default IMessageBuilder file(String name, String data) {
return file(name, data.getBytes(StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public IMessageBuilder commandButton(String command, String message) {
command = id + "";
remapLongCommands.put(command, cmdLong);
}
if (message.equalsIgnoreCase("cancel")) {
if (message.equalsIgnoreCase("cancel") || message.equalsIgnoreCase("dismiss")) {
buttons.add(Button.danger(command, message));
} else {
buttons.add(Button.primary(command, message));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ public CommandManager2 registerDefaults() {
this.commands.registerMethod(new BankCommands(), List.of("escrow"), "escrowSheetCmd", "view_sheet");

this.commands.registerMethod(new IACommands(), List.of("nation", "list"), "viewBans", "bans");
this.commands.registerMethod(new IACommands(), List.of("mail"), "readMail", "read");
this.commands.registerMethod(new IACommands(), List.of("mail"), "searchMail", "search");

this.commands.registerMethod(new AdminCommands(), List.of("admin", "sync"), "importLinkedBans", "multi_bans");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public String title(@Me User user, @Me IMessageIO io, @Me Guild guild, Message d
message.clearEmbeds();
message.embed(builder.build());
message.send();
io.create().embed("Set Title", "Done! See: " + discMessage.getJumpUrl()).cancelButton().send();
io.create().embed("Set Title", "Done! See: " + discMessage.getJumpUrl()).cancelButton("Dismiss").send();
return null;
}

Expand All @@ -118,7 +118,7 @@ public String description(@Me User user, @Me IMessageIO io, @Me Guild guild, Mes
message.clearEmbeds();
message.embed(builder.build());
message.send();
io.create().embed("Set Description", "Done! See: " + discMessage.getJumpUrl()).cancelButton().send();
io.create().embed("Set Description", "Done! See: " + discMessage.getJumpUrl()).cancelButton("Dismiss").send();
return null;
}

Expand Down Expand Up @@ -152,12 +152,20 @@ public String renameButton(@Me User user, @Me IMessageIO io, @Me Guild guild, @M

DiscordMessageBuilder msg = new DiscordMessageBuilder(message.getChannel(), message);
msg.clearButtons();
for (Map.Entry<String, String> entry : reactions.entrySet()) {
msg.commandButton(entry.getValue(), entry.getKey());

if (reactions != null) {
for (Map.Entry<String, String> entry : reactions.entrySet()) {
msg.commandButton(entry.getValue(), entry.getKey().equalsIgnoreCase(label) ? rename_to : entry.getKey());
}
}
for (Button button : message.getButtons()) {
if (!button.getId().equalsIgnoreCase(button.getLabel())) {
msg.commandButton(button.getId(), button.getLabel().equalsIgnoreCase(label) ? rename_to : button.getLabel());
}
}
msg.send();
io.create().embed("Deleted Button", "Done! Renamed button `" + label + "` to " + rename_to + "\n" +
"Remove it using: " + CM.embed.remove.button.cmd.toSlashMention()).cancelButton().send();
io.create().embed("Renamed Button", "Done! Renamed button `" + label + "` to " + rename_to + "\n" +
"Remove it using: " + CM.embed.remove.button.cmd.toSlashMention()).cancelButton("Dismiss").send();
return null;
}

Expand Down Expand Up @@ -194,7 +202,7 @@ public String removeButton(@Me User user, @Me IMessageIO io, @Me Guild guild, Me
}

RateLimitUtil.queue(message.editMessageComponents(rows));
io.create().embed("Deleted Button", "Done! Deleted " + labels.size() + " buttons").cancelButton().send();
io.create().embed("Deleted Button", "Done! Deleted " + labels.size() + " buttons").cancelButton("Dismiss").send();
return null;
}

Expand Down Expand Up @@ -235,7 +243,7 @@ public String addButtonRaw(@Me User user, @Me IMessageIO io, @Me JSONObject cmdJ
.send();
io.create().embed("Added Button", "Added button `" + label + "` to " + message.getJumpUrl() + "\n" +
"Remove it using: " + CM.embed.remove.button.cmd.toSlashMention() + "\n" +
"Rename using TODO CM REF").cancelButton().send();// + CM.embed.rename.button.cmd.toSlashMention();
"Rename using TODO CM REF").cancelButton("Dismiss").send();// + CM.embed.rename.button.cmd.toSlashMention();
return null;
}

Expand Down Expand Up @@ -327,7 +335,7 @@ public String addModal(@Me User user, @Me IMessageIO io, @Me Guild guild, Messag
new DiscordMessageBuilder(message.getChannel(), message)
.modal(behavior, channelId, command, full, label)
.send();
io.create().embed("Added Modal", "Added modal button `" + label + "` to " + message.getJumpUrl()).cancelButton().send();
io.create().embed("Added Modal", "Added modal button `" + label + "` to " + message.getJumpUrl()).cancelButton("Dismiss").send();
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
import link.locutus.discord.util.sheet.SpreadSheet;
import link.locutus.discord.util.task.MailRespondTask;
import link.locutus.discord.util.task.ia.IACheckup;
import link.locutus.discord.util.task.mail.Mail;
import link.locutus.discord.util.task.mail.ReadMailTask;
import link.locutus.discord.util.task.mail.SearchMailTask;
import link.locutus.discord.web.jooby.handler.CommandResult;
import com.google.gson.JsonObject;
import link.locutus.discord.apiv1.enums.Rank;
Expand Down Expand Up @@ -1212,12 +1215,130 @@ public String reply(@Me GuildDB db, @Me DBNation me, @Me User author, @Me IMessa
return "Mail: " + result;
}

// @Command(desc = "Read a message for an id")
// @RolePermission(Roles.MAIL)
// @IsAlliance
// public String viewMail(@Me )
@Command(desc = "Read and display an in-game message for an id from a nation's account\n" +
"The nation must be your own or in the same alliance\n" +
"Login details via `{prefix}credentials login`")
@RolePermission(Roles.MAIL)
@IsAlliance
public String readMail(@Me DBNation me, @Me GuildDB db, int message_id, @Default DBNation account) throws Exception {
if (account == null) account = me;
if (me.getId() != account.getId()) {
if (!db.isAllianceId(account.getAlliance_id())) return "Nation is not a member of the alliance";
}
Auth auth = account.getAuth(true);

List<ReadMailTask.MailMessage> msgs = new ReadMailTask(auth, message_id).call();
if (msgs.isEmpty()) {
return "No messages found for " + account.getMarkdownUrl() + " with id `" + message_id + "`. Ensure the `account` is correct and the message is not more than 30 days old";
}
StringBuilder response = new StringBuilder();
for (ReadMailTask.MailMessage msg : msgs) {
response.append("**__" + PW.getMarkdownUrl(msg.getNationId(), false) + "__**" + DiscordUtil.timestamp(msg.getDate(), null) + "\n");
response.append(msg.getContent()).append("\n\n");
}
return response.toString();
}

@Command(desc = "Show ingame mail and optionally the responses\n" +
"Results are in a spreadsheet\n" +
"A search term can be specified to only show messages containing that in the subject line\n" +
"By default only unread messages are checked")
@IsAlliance
public String searchMail(@Me GuildDB db, @Me IMessageIO io,
@Me DBNation me,
@Arg("The account to check the mail of\n" +
"Defaults to your own nation")
@Default DBNation account,
@Arg("The term to search for\n" +
"Defaults to show all messages")
@Switch("q") String search_for,
@Arg("Don't search unread messages\n" +
"Defaults to false")
@Switch("u") boolean skip_unread,
@Switch("r") boolean check_read,
@Switch("c") boolean read_content,
@Arg("If responses are grouped by nation")
@Switch("g") boolean group_by_nation,
@Arg("If responses are counted")
@Switch("n") boolean count_replies
) throws Exception {
if (account == null) account = me;
if (me.getId() != account.getId()) {
if (!db.isAllianceId(account.getAlliance_id())) return "Nation is not a member of the alliance";
}
Auth auth = account.getAuth(true);

if (skip_unread) {
check_read = true;
}
Map<DBNation, Map<Mail, List<String>>> results = new LinkedHashMap<>();
SearchMailTask task = new SearchMailTask(auth, search_for, !skip_unread, check_read, read_content, (mail, strings) -> {
DBNation nation = Locutus.imp().getNationDB().getNation(mail.nationId);
if (nation != null) {
results.computeIfAbsent(nation, f -> new LinkedHashMap<>()).put(mail, strings);
}
});
task.call();

SpreadSheet sheet = SpreadSheet.create(db, SheetKey.MAIL_RESPONSES_SHEET);

// todo add mail search SearchMailTask
List<String> header = new ArrayList<>(Arrays.asList(
"nation",
"alliance",
"mail-id",
"subject",
"response"
));

sheet.setHeader(header);

List<Object> row = new ArrayList<>();
for (Map.Entry<DBNation, Map<Mail, List<String>>> entry : results.entrySet()) {
row.clear();
DBNation nation = entry.getKey();

int countVal = 0;
for (Map.Entry<Mail, List<String>> mailListEntry : entry.getValue().entrySet()) {
if (!group_by_nation) row.clear();

Mail mail = mailListEntry.getKey();
List<String> strings = mailListEntry.getValue();

if (row.isEmpty()) {
int allianceId = nation != null ? nation.getAlliance_id() : 0;
row.add(MarkupUtil.sheetUrl(PW.getName(mail.nationId, false), PW.getUrl(mail.nationId, false)));
row.add(MarkupUtil.sheetUrl(PW.getName(allianceId, true), PW.getUrl(allianceId, true)));
row.add(mail.id + "");
row.add(mail.subject);
}

if (count_replies) {
strings.removeIf(f -> f.equalsIgnoreCase("false"));
if (group_by_nation) {
countVal += strings.size();
} else {
row.add(strings.size());
}
} else {
row.addAll(strings);
}

if (!group_by_nation) sheet.addRow(row);
}
if (group_by_nation) {
if (count_replies) {
row.add(countVal);
}
sheet.addRow(row);
}
}

sheet.updateClearCurrentTab();
sheet.updateWrite();

sheet.attach(io.create(), "mail", null, false, 0).send();
return null;
}

@Command(desc = "Generate a list of nations and their expected raid loot\n" +
"e.g. `{prefix}sheets_milcom lootvaluesheet #cities<10,#position>1,#active_m<2880,someAlliance`")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3264,6 +3264,24 @@ public button labels(String value) {

}
}
public static class rename{
@AutoRegister(clazz=link.locutus.discord.commands.manager.v2.impl.pw.commands.EmbedCommands.class,method="renameButton")
public static class button extends CommandRef {
public static final button cmd = new button();
public button message(String value) {
return set("message", value);
}

public button label(String value) {
return set("label", value);
}

public button rename_to(String value) {
return set("rename_to", value);
}

}
}
public static class template{
@AutoRegister(clazz=link.locutus.discord.commands.manager.v2.impl.pw.commands.EmbedCommands.class,method="allyEnemySheets")
public static class ally_enemy_sheets extends CommandRef {
Expand Down Expand Up @@ -4805,6 +4823,18 @@ public command skipMail(String value) {
return set("skipMail", value);
}

}
@AutoRegister(clazz=link.locutus.discord.commands.manager.v2.impl.pw.commands.IACommands.class,method="readMail")
public static class read extends CommandRef {
public static final read cmd = new read();
public read messageId(String value) {
return set("messageId", value);
}

public read account(String value) {
return set("account", value);
}

}
@AutoRegister(clazz=link.locutus.discord.commands.manager.v2.impl.pw.commands.AdminCommands.class,method="testRecruitMessage")
public static class recruit extends CommandRef {
Expand All @@ -4830,6 +4860,38 @@ public reply sender(String value) {
return set("sender", value);
}

}
@AutoRegister(clazz=link.locutus.discord.commands.manager.v2.impl.pw.commands.IACommands.class,method="searchMail")
public static class search extends CommandRef {
public static final search cmd = new search();
public search account(String value) {
return set("account", value);
}

public search search_for(String value) {
return set("search_for", value);
}

public search skip_unread(String value) {
return set("skip_unread", value);
}

public search check_read(String value) {
return set("check_read", value);
}

public search read_content(String value) {
return set("read_content", value);
}

public search group_by_nation(String value) {
return set("group_by_nation", value);
}

public search count_replies(String value) {
return set("count_replies", value);
}

}
@AutoRegister(clazz=link.locutus.discord.commands.manager.v2.impl.pw.commands.IACommands.class,method="mail")
public static class send extends CommandRef {
Expand Down
Loading

0 comments on commit 91b1669

Please sign in to comment.