Skip to content

Commit

Permalink
Various
Browse files Browse the repository at this point in the history
moderation for subject
login dont require guild locals
fidx interview question formatting
conflict import feedback messages
  • Loading branch information
xdnw committed May 14, 2024
1 parent 9b3e0d2 commit cc5f337
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package link.locutus.discord.commands.manager.v2.impl.discord.permission;

import link.locutus.discord.apiv1.enums.Rank;
import link.locutus.discord.apiv3.enums.AlliancePermission;
import link.locutus.discord.commands.manager.v2.binding.annotation.Command;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.METHOD})
public @interface CmdAlliancePermission {
@Command(desc = "The alliance permissions required")
AlliancePermission[] value() default {};

boolean any() default false;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
package link.locutus.discord.commands.manager.v2.impl.pw.binding;

import link.locutus.discord.Locutus;
import link.locutus.discord.apiv1.enums.Rank;
import link.locutus.discord.apiv3.enums.AlliancePermission;
import link.locutus.discord.commands.manager.v2.binding.BindingHelper;
import link.locutus.discord.commands.manager.v2.binding.annotation.Binding;
import link.locutus.discord.commands.manager.v2.binding.annotation.Me;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.ClassPermission;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.CoalitionPermission;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.DenyPermission;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.HasApi;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.HasKey;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.HasOffshore;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.IsAlliance;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.IsGuild;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.NotGuild;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.RankPermission;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.RolePermission;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.WhitelistPermission;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.*;
import link.locutus.discord.commands.manager.v2.impl.pw.refs.CM;
import link.locutus.discord.config.Settings;
import link.locutus.discord.db.GuildDB;
Expand Down Expand Up @@ -178,13 +169,32 @@ public boolean checkWhitelistPermission(@Me GuildDB db, CoalitionPermission perm
@Binding("Must be registered to a nation with an in-game rank equal to or above the provided rank\n" +
"Default: MEMBER")
@RankPermission
public boolean checkRank(@Me Guild guild, RankPermission perm, @Me DBNation me) {
public boolean checkRank(RankPermission perm, @Me DBNation me) {
if (me.getPosition() < perm.value().id) {
throw new IllegalCallerException("Your ingame alliance positions is below " + perm.value());
}
return true;
}

@Binding("Must have the permissions ingame with their alliance")
@CmdAlliancePermission
public boolean checkRank(CmdAlliancePermission perm, @Me DBNation me) {
if (me.getPositionEnum().id >= Rank.HEIR.id) return true;
if (me.getPositionEnum().id <= Rank.APPLICANT.id) return false;
boolean hasAny = false;
for (AlliancePermission requiredPermission : perm.value()) {
if (!me.hasPermission(requiredPermission)) {
if (!perm.any()) throw new IllegalCallerException("You do not have " + requiredPermission + " in your alliance");
} else {
hasAny = true;
}
}
if (!hasAny) {
throw new IllegalCallerException("You do not have any of " + Arrays.toString(perm.value()) + " in your alliance");
}
return true;
}

@Binding("Deny all use")
@DenyPermission
public boolean deny(DenyPermission perm, @Me DBNation nation, @Me User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import link.locutus.discord.commands.manager.v2.binding.annotation.*;
import link.locutus.discord.commands.manager.v2.binding.annotation.Timestamp;
import link.locutus.discord.commands.manager.v2.command.IMessageBuilder;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.CoalitionPermission;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.IsGuild;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.WhitelistPermission;
Expand Down Expand Up @@ -54,6 +55,7 @@
import java.text.Normalizer;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -115,15 +117,18 @@ public String setWiki(@Me GuildDB db, ConflictManager manager, Conflict conflict
url = url.substring(url.lastIndexOf("/") + 1);
}
conflict.setWiki(url);
String importResult = importWikiPage(db, manager, conflict.getName(), url, false, true);
conflict.push(manager, null, false, false);
return "Set wiki to: `" + url + "`. Attempting to load additional wiki information...\n\n" +
importWikiPage(db, manager, conflict.getName(), url, false);
importResult;
}

@Command(desc = "Sets the wiki page for a conflict")
@RolePermission(Roles.MILCOM)
@CoalitionPermission(Coalition.MANAGE_CONFLICTS)
public String setStatus(ConflictManager manager, Conflict conflict, String status) throws IOException {
conflict.setStatus(status);
conflict.push(manager, null, false, false);
return "Done! Set the status of `" + conflict.getName() + "` to `" + status + "`";
}

Expand All @@ -132,6 +137,7 @@ public String setStatus(ConflictManager manager, Conflict conflict, String statu
@CoalitionPermission(Coalition.MANAGE_CONFLICTS)
public String setCB(ConflictManager manager, Conflict conflict, String casus_belli) throws IOException {
conflict.setCasusBelli(casus_belli);
conflict.push(manager, null, false, false);
return "Done! Set the casus belli of `" + conflict.getName() + "` to `" + casus_belli + "`";
}

Expand All @@ -140,30 +146,39 @@ public String setCB(ConflictManager manager, Conflict conflict, String casus_bel
@CoalitionPermission(Coalition.MANAGE_CONFLICTS)
public String setCategory(ConflictManager manager, Conflict conflict, ConflictCategory category) throws IOException {
conflict.setCategory(category);
conflict.push(manager, null, false, true);
return "Done! Set the category of `" + conflict.getName() + "` to `" + category + "`";
}

@Command(desc = "Pushes conflict data to the AWS bucket for the website")
@RolePermission(Roles.MILCOM)
@CoalitionPermission(Coalition.MANAGE_CONFLICTS)
public String syncConflictData(ConflictManager manager, @Default Set<Conflict> conflicts, @Switch("u") boolean upload_graph, @Switch("w") boolean reinitialize_wars, @Switch("g") boolean reinitialize_graphs) throws IOException, ParseException {
public String syncConflictData(@Me IMessageIO io, ConflictManager manager, @Default Set<Conflict> conflicts, @Switch("u") boolean upload_graph, @Switch("w") boolean reinitialize_wars, @Switch("g") boolean reinitialize_graphs) throws IOException, ParseException {
AwsManager aws = manager.getAws();
if (aws == null) {
throw new IllegalArgumentException("AWS is not configured in `config.yaml`");
}
CompletableFuture<IMessageBuilder> msgFuture;
if (reinitialize_wars) {
msgFuture = io.send("Initializing wars...");
manager.loadConflictWars(conflicts, true);
} else {
msgFuture = io.send("Please wait...");
}
if (reinitialize_graphs) {
for (Conflict conflict : conflicts) {
io.updateOptionally(msgFuture, "Initializing graphs for " + conflict.getName() + "...");
conflict.updateGraphsLegacy(manager);
}
}
if (conflicts != null) {
List<String> urls = new ArrayList<>();
for (Conflict conflict : conflicts) {
urls.addAll(conflict.push(manager, null, upload_graph));
io.updateOptionally(msgFuture, "Pushing " + conflict.getName() + "...");
urls.addAll(conflict.push(manager, null, upload_graph, false));
}
io.updateOptionally(msgFuture, "Pushing index...");
urls.add(manager.pushIndex());
return "Done! See:\n- <" + StringMan.join(urls, ">\n- <") + ">";
} else {
if (!manager.pushDirtyConflicts()) {
Expand All @@ -190,6 +205,7 @@ public String deleteConflict(ConflictManager manager, @Me IMessageIO io, @Me JSO
return null;
}
manager.deleteConflict(conflict);
manager.pushIndex();
return "Deleted conflict: #" + conflict.getId() + " - `" + conflict.getName() + "`" +
"\nNote: this does not push the data to the site";
}
Expand Down Expand Up @@ -255,8 +271,9 @@ public String setConflictEnd(ConflictManager manager, @Me JSONObject command, Co
return "Set `" + conflict.getName() + "` end to " + TimeUtil.DD_MM_YYYY.format(time) + " for " + alliance.getMarkdownUrl();
}
conflict.setEnd(time);
conflict.push(manager, null, false, true);
return "Set `" + conflict.getName() + "` end to " + TimeUtil.DD_MM_YYYY.format(time) +
"\nNote: this does not push the data to the site";
"\nNote: this does not recalculate conflict data";
}

@Command(desc = "Set the start date for a conflict\n" +
Expand All @@ -281,8 +298,9 @@ public String setConflictStart(ConflictManager manager, @Me JSONObject command,
return "Set `" + conflict.getName() + "` start to " + TimeUtil.DD_MM_YYYY.format(time) + " for " + alliance.getMarkdownUrl();
}
conflict.setStart(time);
conflict.push(manager, null, false, true);
return "Set `" + conflict.getName() + "` start to " + TimeUtil.DD_MM_YYYY.format(time) +
"\nNote: this does not push the data to the site";
"\nNote: this does not recalculate conflict data";
}

@Command(desc = "Set the name of a conflict")
Expand Down Expand Up @@ -310,8 +328,8 @@ public String setConflictName(ConflictManager manager, Conflict conflict, String
sideName = "conflict";
conflict.setName(name);
}
return "Changed " + sideName + " name `" + previousName + "` => `" + name + "`" +
"\nNote: this does not push the data to the site";
conflict.push(manager, null, false, true);
return "Changed " + sideName + " name `" + previousName + "` => `" + name + "`";
}

private int getFighting(DBAlliance alliance, Set<DBAlliance> enemyCoalition) {
Expand Down Expand Up @@ -540,7 +558,8 @@ public String importAllianceNames(ConflictManager manager) throws IOException, P
public String importWikiPage(@Me GuildDB db, ConflictManager manager,
String name,
@Default String url,
@Default("true") boolean useCache) throws IOException {
@Default("true") boolean useCache,
@Switch("p") boolean skipPushToSite) throws IOException {
if (name.contains("http")) return "Please specify the name of the wiki page, not the URL for `name`";
if (url == null) {
url = Normalizer.normalize(name.replace(" ", "_"), Normalizer.Form.NFKC);
Expand Down Expand Up @@ -635,8 +654,14 @@ public String recalculateTables(ConflictManager manager, Set<Conflict> conflicts
"This does not push the data to the site")
@RolePermission(Roles.MILCOM)
@CoalitionPermission(Coalition.MANAGE_CONFLICTS)
public String recalculateGraphs(ConflictManager manager, Set<Conflict> conflicts) throws IOException, ParseException {
public String recalculateGraphs(@Me IMessageIO io, ConflictManager manager, Set<Conflict> conflicts) throws IOException, ParseException {
CompletableFuture<IMessageBuilder> future = io.send("Please wait...");
return recalculateGraphs2(io, future, manager, conflicts);
}

private String recalculateGraphs2(@Me IMessageIO io, CompletableFuture<IMessageBuilder> updateMsg, ConflictManager manager, Set<Conflict> conflicts) throws IOException, ParseException {
for (Conflict conflict : conflicts) {
io.updateOptionally(updateMsg, "Updating " + conflict.getName());
conflict.updateGraphsLegacy(manager);
}
return "Done!\nNote: this does not push the data to the site";
Expand All @@ -647,12 +672,14 @@ public String recalculateGraphs(ConflictManager manager, Set<Conflict> conflicts
"This does not push the data to the site")
@RolePermission(Roles.MILCOM)
@CoalitionPermission(Coalition.MANAGE_CONFLICTS)
public String importConflictData(@Me GuildDB db, ConflictManager manager,
public String importConflictData(@Me IMessageIO io, @Me GuildDB db, ConflictManager manager,
@Switch("c") boolean ctowned,
@Switch("g") Set<Conflict> graphData,
@Switch("a") boolean allianceNames,
@Switch("w") boolean wiki,
@Switch("s") boolean all) throws IOException, SQLException, ClassNotFoundException, ParseException {
CompletableFuture<IMessageBuilder> msgFuture = io.send("Please wait...");

boolean loadGraphData = false;
if (all) {
Locutus.imp().getWarDb().loadWarCityCountsLegacy();
Expand All @@ -662,22 +689,37 @@ public String importConflictData(@Me GuildDB db, ConflictManager manager,
loadGraphData = true;
}
if (allianceNames) {
io.updateOptionally(msgFuture, "Importing alliance names");
importAllianceNames(manager);
}
if (ctowned) {
io.updateOptionally(msgFuture, "Importing from ctowned.net");
importCtowned(db, manager, true);
}
if (wiki) {
io.updateOptionally(msgFuture, "Importing from wiki");
importWikiAll(db, manager, true);
}
if (loadGraphData && graphData == null) {
io.updateOptionally(msgFuture, "Recaculating tables");
graphData = new LinkedHashSet<>(manager.getConflictMap().values());
recalculateTables(manager, graphData);
}
if (graphData != null) {
recalculateGraphs(manager, graphData);
io.updateOptionally(msgFuture, "Recaculating graphs");
recalculateGraphs2(io, msgFuture, manager, graphData);
if (all && !graphData.isEmpty()) {
io.updateOptionally(msgFuture, "Pushing data to the site");
for (Conflict conflict : graphData) {
conflict.push(manager, null, true, false);
}
manager.pushIndex();
return "Done!";
}
}
return "Done!\nNote: this does not push the data to the site";
return "Done!\n" +
"Note: this does not push the data to the site\n" +
"See: " + CM.conflict.sync.website.cmd.toSlashMention();
}

@Command(desc = "Configure the guilds and conflict ids that will be featured by this guild\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2300,17 +2300,17 @@ public String iachannels(@Me User author, @Me DBNation me, @Me Guild guild, @Me
@Command(desc = "Set the interview message")
@RolePermission(Roles.INTERNAL_AFFAIRS)
public String setInterview(@Me GuildDB db, IACategory category, String message) {
db.setCopyPasta("interview", message);
return "Set `interview` to:\n```md\n" + message + "```\n\nUse ";// TODO CM REF + CM.interview.questions.view.toSlashMention() + " to view";
db.setCopyPasta("interview", message.replace("\\n", "\n"));
return "Set `interview` to:\n```md\n" + message + "```\n\nUse " + CM.interview.questions.view.cmd.toSlashMention() + " to view";
}

@Command(desc = "View the interview message")
@RolePermission(Roles.INTERNAL_AFFAIRS)
public String viewInterview(@Me GuildDB db, IACategory category) {
String message = db.getCopyPasta("interview", true);
if (message == null) {
return "No message set. Set one with " + "";//TODO CM REF CM.interview.questions.set.cmd.toSlashMention();
return "No message set. Set one with " + "" + CM.interview.questions.set.cmd.toSlashMention();
}
return "Interview questions:\n```md\n" + message + "```";
return "Interview questions:\n" + message + "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
import link.locutus.discord.commands.manager.v2.command.IMessageIO;
import link.locutus.discord.commands.manager.v2.impl.discord.DiscordChannelIO;
import link.locutus.discord.commands.manager.v2.impl.discord.DiscordHookIO;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.HasApi;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.HasOffshore;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.IsAlliance;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.RankPermission;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.RolePermission;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.*;
import link.locutus.discord.commands.manager.v2.impl.pw.refs.CM;
import link.locutus.discord.commands.manager.v2.impl.pw.TaxRate;
import link.locutus.discord.commands.manager.v2.impl.pw.filter.NationPlaceholders;
Expand Down Expand Up @@ -559,7 +555,7 @@ public String addApiKey(@Me IMessageIO io, @Me JSONObject command, String apiKey

@Command(desc="Login to allow the bot to run scripts through your account\n" +
"(Avoid using this if possible)")
@RankPermission(Rank.OFFICER)
@CmdAlliancePermission(AlliancePermission.WITHDRAW_BANK)
public static String login(@Me IMessageIO io, DiscordDB discordDB, @Me DBNation me,
@Arg("Your username (i.e. email) for Politics And War")
String username,
Expand All @@ -568,7 +564,7 @@ public static String login(@Me IMessageIO io, DiscordDB discordDB, @Me DBNation
try {
if (msg != null) io.delete(msg.getId());
} catch (Throwable ignore) {};
if (me == null || me.getPosition() < Rank.OFFICER.id) return "You are not an officer of an alliance";
if (me == null || !me.hasAllPermission(Set.of(AlliancePermission.WITHDRAW_BANK))) return "You are not an officer of an alliance";
DBAlliance alliance = me.getAlliance();
Auth existingAuth = alliance.getAuth();;
if (existingAuth != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public String createTemporary(@Me Guild guild, ConflictManager manager, @Me DBNa
}

String id = "n/" + nation.getId() + "/" + UUID.randomUUID();
List<String> urls = conflict.push(manager, id, true);
List<String> urls = conflict.push(manager, id, true, false);
return Settings.INSTANCE.WEB.S3.SITE + "/conflict?id=" + id;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/link/locutus/discord/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public class Settings extends Config {
@Comment("Your api key (generated if username/password is set)")
public String API_KEY_PRIMARY = "";

@Comment({"A list of api keys the bot can use for requests (optional)",
"See: `/admin validateApiKeys`"})
@Comment({"A list of api keys the bot can use for general requests (optional)",
"To validate these keys, see: `/admin validateApiKeys`"})
public List<String> API_KEY_POOL = Arrays.asList();

@Comment({"The discord id of the bot (generated)",
Expand Down
Loading

0 comments on commit cc5f337

Please sign in to comment.