Skip to content

Commit

Permalink
Autorole confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Jul 19, 2023
1 parent 42db130 commit 46f40cd
Show file tree
Hide file tree
Showing 27 changed files with 204 additions and 98 deletions.
11 changes: 9 additions & 2 deletions src/main/java/link/locutus/discord/Locutus.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import link.locutus.discord.util.scheduler.ThrowingConsumer;
import link.locutus.discord.util.task.ia.MapFullTask;
import link.locutus.discord.util.task.mail.AlertMailTask;
import link.locutus.discord.util.task.roles.AutoRoleInfo;
import link.locutus.discord.util.trade.TradeManager;
import link.locutus.discord.util.update.*;
import link.locutus.discord.web.jooby.WebRoot;
Expand Down Expand Up @@ -580,7 +581,8 @@ public void autoRole(DBNation nation) {
Member member = guild.getMember(discordUser);
if (member != null) {
try {
db.getAutoRoleTask().autoRole(member, nation, true);
AutoRoleInfo task = db.getAutoRoleTask().autoRole(member, nation);
task.execute();
} catch (Throwable e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -946,7 +948,8 @@ public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) {
GuildDB db = getGuildDB(guild);

DBNation nation = DiscordUtil.getNation(event.getUser());
db.getAutoRoleTask().autoRole(event.getMember(), nation, true);
AutoRoleInfo task = db.getAutoRoleTask().autoRole(event.getMember(), nation);
task.execute();
db.getHandler().onGuildMemberJoin(event);

eventBus.post(event);
Expand Down Expand Up @@ -1071,6 +1074,10 @@ public void onButtonInteraction(@NotNull ButtonInteractionEvent event) {
}
}

if (behavior == CommandBehavior.DELETE_MESSAGE) {
io.setMessageDeleted();
}

System.out.println("ID 3 " + id + " " + behavior);

if (!id.contains("modal create")) {
Expand Down
34 changes: 30 additions & 4 deletions src/main/java/link/locutus/discord/apiv1/enums/DepositType.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,34 @@ public DepositTypeInfo withCity(long city) {
}

public DepositTypeInfo withValue(long amount, long city) {
return new DepositTypeInfo(this, amount, city);
return new DepositTypeInfo(this, amount, city, false);
}

public DepositTypeInfo withValue(long amount, long city, boolean ignore) {
return new DepositTypeInfo(this, amount, city, ignore);
}


public static class DepositTypeInfo {
public final DepositType type;
public final long amount;
public final long city;
public boolean ignore;

public DepositTypeInfo(DepositType type, long amount, long city) {
public DepositTypeInfo(DepositType type, long amount, long city, boolean ignore) {
this.type = type;
this.amount = amount;
this.city = city;
this.ignore = ignore;
}

public DepositTypeInfo(DepositType type) {
this(type, 0, 0);
this(type, 0, 0, false);
}

public DepositTypeInfo ignore(boolean value) {
this.ignore = value;
return this;
}

public DepositType getType() {
Expand All @@ -92,6 +104,13 @@ public String toString(long accountId) {
if (result.contains("=")) {
throw new IllegalArgumentException("Deposit type " + type.name() + " already has a value");
}
if (result.contains("#ignore")) {
String typeName = type.name().toLowerCase(Locale.ROOT);
result = result.replace(typeName, typeName + "=" + accountId);
} else {
result += "=" + accountId;
}
} else if (result.contains("#ignore")) {
result += "=" + accountId;
} else {
result = "#" + type.parent.name().toLowerCase(Locale.ROOT) + "=" + accountId + " " + result;
Expand All @@ -109,7 +128,14 @@ public String toString() {
if (city != 0) {
note += " #city=" + city;
}
return note;
if (ignore) {
note += " #ignore";
}
return note.trim();
}

public boolean isIgnored() {
return ignore || type == IGNORE;
}
}
}
36 changes: 21 additions & 15 deletions src/main/java/link/locutus/discord/commands/bank/GrantCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public String desc() {
"Add `-o` to only send what funds they are missing for a grant\n" +
"Add `-m` to multiply the grant per city\n" +
"Use `tax_id:1234` to specify tax account\n" +
"Use `-t` to specify receiver's tax account";
"Use `-t` to specify receiver's tax account\n" +
"Add `#ignore` to ignore";
}

@Override
Expand All @@ -94,6 +95,7 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m
}
GuildDB guildDb = Locutus.imp().getGuildDB(guild);

boolean ignore = false;
DBNation nationAccount = null;
DBAlliance allianceAccount = null;
DBAlliance offshoreAccount = null;
Expand Down Expand Up @@ -128,6 +130,11 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m

for (Iterator<String> iter = args.iterator(); iter.hasNext(); ) {
String arg = iter.next().toLowerCase();
if (arg.equalsIgnoreCase("#ignore")) {
iter.remove();
ignore = true;
continue;
}
if (arg.startsWith("-expire") || arg.startsWith("-e") || arg.startsWith("#expire")) {
expire = TimeUtil.timeToSec(arg.split("[:=]", 2)[1]) * 1000L;
iter.remove();
Expand Down Expand Up @@ -195,7 +202,7 @@ else if (args.get(1).equalsIgnoreCase("warchest")) {
row.add(nation.getCities());
row.add(nation.getAvg_infra());
try {
Grant grant = generateGrant(typeArg, guildDb, nation, num, flags, false);
Grant grant = generateGrant(typeArg, guildDb, nation, num, flags, false, ignore);
row.add(grant.getInstructions());
row.add(PnwUtil.convertedTotal(grant.cost()));
row.add(PnwUtil.resourcesToString(grant.cost()));
Expand All @@ -219,7 +226,7 @@ else if (args.get(1).equalsIgnoreCase("warchest")) {
return null;
}

Grant grant = generateGrant(typeArg, guildDb, me, num, flags, true);
Grant grant = generateGrant(typeArg, guildDb, me, num, flags, true, ignore);

Member member = null;

Expand Down Expand Up @@ -280,7 +287,7 @@ else if (args.get(1).equalsIgnoreCase("warchest")) {
return null;
}

public Grant generateGrant(String arg, GuildDB guildDb, DBNation me, double amt, Set<Character> flags, boolean single) throws IOException, ExecutionException, InterruptedException {
public Grant generateGrant(String arg, GuildDB guildDb, DBNation me, double amt, Set<Character> flags, boolean single, boolean ignore) throws IOException, ExecutionException, InterruptedException {
Grant grant;

boolean existing = flags.contains('o');
Expand Down Expand Up @@ -313,31 +320,31 @@ public Grant generateGrant(String arg, GuildDB guildDb, DBNation me, double amt,
if (numBuy <= 0) throw new IllegalArgumentException("Already has " + currentCity + " cities");


grant = new Grant(me, DepositType.CITY.withAmount(currentCity + numBuy));
grant = new Grant(me, DepositType.CITY.withAmount(currentCity + numBuy).ignore(ignore));
grant.setAmount(amt);
grant.addCity(me.getCities());
grant.setInstructions(grantCity(me, numBuy, resources, force));
} else if (arg.equalsIgnoreCase("infra")) {
// city id
// amt
grant = new Grant(me, DepositType.INFRA.withValue((int) amt, -1));
grant = new Grant(me, DepositType.INFRA.withValue((int) amt, -1).ignore(ignore));
grant.setAmount(amt);
grant.setInstructions(grantInfra(me, (int) amt, resources, force, single));
grant.setAllCities();
} else if (arg.equalsIgnoreCase("land")) {
grant = new Grant(me, DepositType.LAND.withValue((int) amt, -1));
grant = new Grant(me, DepositType.LAND.withValue((int) amt, -1).ignore(ignore));
grant.setAmount((int) amt);
grant.setInstructions(grantLand(me, (int) amt, resources, force));
grant.setAllCities();
} else if (arg.contains("mmrbuy=")) {
MMRDouble mmr = MMRDouble.fromString(arg.split("=")[1]);
grant = new Grant(me, DepositType.WARCHEST.withValue());
grant = new Grant(me, DepositType.WARCHEST.withValue().ignore(ignore));
grant.setAmount(amt);
grant.setInstructions(grantMMRBuy(me, mmr, (int) amt, resources, force));
grant.setAllCities();
} else if (arg.contains("mmr=")) {
MMRDouble mmr = MMRDouble.fromString(arg.split("=")[1]);
grant = new Grant(me, DepositType.WARCHEST.withValue());
grant = new Grant(me, DepositType.WARCHEST.withValue().ignore(ignore));
grant.setAmount(amt);
grant.setInstructions(grantMMR(me, mmr, (int) amt, resources, force));
grant.setAllCities();
Expand Down Expand Up @@ -370,7 +377,7 @@ public Grant generateGrant(String arg, GuildDB guildDb, DBNation me, double amt,
}
from = Collections.singletonMap(citiesAmt, found);
}
grant = new Grant(me, DepositType.BUILD.withValue(pair, citiesAmt));
grant = new Grant(me, DepositType.BUILD.withValue(pair, citiesAmt).ignore(ignore));

for (Map.Entry<Integer, JavaCity> entry : from.entrySet()) {
if (noInfra) entry.getValue().setInfra(city.getInfra());
Expand All @@ -382,7 +389,7 @@ public Grant generateGrant(String arg, GuildDB guildDb, DBNation me, double amt,
grant.setInstructions(city.instructions(from, buffer));
resources = PnwUtil.resourcesToMap(buffer);
} else {
grant = new Grant(me, DepositType.GRANT.withValue());
grant = new Grant(me, DepositType.GRANT.withValue().ignore(ignore));
grant.setInstructions("transfer resources");
resources = PnwUtil.parseResources(arg);
}
Expand All @@ -407,7 +414,7 @@ public Grant generateGrant(String arg, GuildDB guildDb, DBNation me, double amt,
else resources.put(entry.getKey(), required);
}
}
grant = new Grant(me, DepositType.WARCHEST.withValue());
grant = new Grant(me, DepositType.WARCHEST.withValue().ignore(ignore));
grant.setInstructions("warchest");
} else {
Project project = Projects.get(arg);
Expand Down Expand Up @@ -451,7 +458,7 @@ public Grant generateGrant(String arg, GuildDB guildDb, DBNation me, double amt,
}

resources = PnwUtil.resourcesToMap(unit.getCost((int) amt));
grant = new Grant(me, DepositType.WARCHEST.withValue());
grant = new Grant(me, DepositType.WARCHEST.withValue().ignore(ignore));
grant.setInstructions("Go to <" + Settings.INSTANCE.PNW_URL() + "/military/" + unit.getName() + "/> and purchase " + (int) amt + " " + unit.getName());
} else {
if (me.projectSlots() <= me.getNumProjects() && !flags.contains('f')) {
Expand All @@ -476,7 +483,7 @@ public Grant generateGrant(String arg, GuildDB guildDb, DBNation me, double amt,
resources = PnwUtil.multiply(resources, factor);
}

grant = new Grant(me, DepositType.PROJECT.withAmount(project.ordinal()));
grant = new Grant(me, DepositType.PROJECT.withAmount(project.ordinal()).ignore(ignore));
grant.setInstructions("Go to <" + Settings.INSTANCE.PNW_URL() + "/nation/projects/> and purchase " + project.name());
}
}
Expand All @@ -497,7 +504,6 @@ public Grant generateGrant(String arg, GuildDB guildDb, DBNation me, double amt,
}
}
}

}

Map<ResourceType, Double> finalResources = resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import link.locutus.discord.commands.manager.Command;
import link.locutus.discord.commands.manager.CommandCategory;
import link.locutus.discord.commands.manager.v2.command.IMessageIO;
import link.locutus.discord.commands.manager.v2.impl.pw.commands.UtilityCommands;
import link.locutus.discord.db.entities.DBNation;
import link.locutus.discord.util.MathMan;
import link.locutus.discord.util.PnwUtil;
Expand Down Expand Up @@ -35,14 +36,6 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m
if (args.size() != 1) return usage(args.size(), 1, channel);
DBNation nation = DiscordUtil.parseNation(args.get(0));
if (nation == null) return "Invalid nation: `" + args.get(0) + "`";


Set<Project> projects = nation.getProjects();
double value = 0;
for (Project project : projects) {
value += PnwUtil.convertedTotal(project.cost());
}

return nation.getNation() + " has " + projects.size() + "/" + nation.projectSlots() + " worth $" + MathMan.format(value);
return UtilityCommands.ProjectSlots(nation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ default CompletableFuture<IMessageBuilder> send(String message) {
return send(create().append(message));
}

void setMessageDeleted();

CompletableFuture<IMessageBuilder> send(IMessageBuilder builder);

IMessageIO update(IMessageBuilder builder, long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ else if (defDesc != null && !defDesc.isEmpty() && !Objects.equals(desc, defDesc)
String keyName = key.toSimpleString();
if (spoiler) keyName = StringEscapeUtils.escapeHtml4(keyName.replace("[", "\\[").replace("]", "\\]"));
if (links) {
String typeLink = MarkupUtil.markdownUrl(keyName, typeUrlBase + "#" + MarkupUtil.pathName(key.toSimpleString()));
String typeLink = MarkupUtil.markdownUrl(keyName, typeUrlBase + "#" + MarkupUtil.pathName(key.toSimpleString().toLowerCase(Locale.ROOT)));
result.append("`" + argFormat + "`").append(" - ").append(typeLink);
} else {
result.append("`" + argFormat + "`").append(" - ").append(keyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public IMessageBuilder create() {
return new StringMessageBuilder(this, id++, System.currentTimeMillis(), user);
}

@Override
public void setMessageDeleted() {

}

@Override
public CompletableFuture<IMessageBuilder> send(IMessageBuilder builder) {
messages.put(builder.getId(), ((StringMessageBuilder) builder).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class DiscordChannelIO implements IMessageIO {
private final MessageChannel channel;
private final Supplier<Message> userMessage;
private Supplier<Message> userMessage;

public DiscordChannelIO(MessageChannel channel, Supplier<Message> userMessage) {
this.channel = channel;
Expand All @@ -56,6 +56,11 @@ public Message getUserMessage() {
return userMessage != null ? userMessage.get() : null;
}

@Override
public void setMessageDeleted() {
userMessage = null;
}

@Override
@Deprecated
public IMessageBuilder getMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import link.locutus.discord.util.discord.DiscordUtil;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.interactions.Interaction;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.callbacks.IModalCallback;
Expand All @@ -29,20 +30,31 @@ public class DiscordHookIO implements IMessageIO {
private final InteractionHook hook;
private final Map<Long, IMessageBuilder> messageCache = new HashMap<>();
private final IModalCallback modalCallback;
private boolean originalDeleted;

public DiscordHookIO(InteractionHook hook, IModalCallback modalCallback) {
this.hook = hook;
this.modalCallback = modalCallback;
}

@Override
public void setMessageDeleted() {
this.originalDeleted = true;
}

public IModalCallback getModalCallback() {
return modalCallback;
}

@Override
@Deprecated
public IMessageBuilder getMessage() {
return new DiscordMessageBuilder(this, RateLimitUtil.complete(hook.retrieveOriginal()));
if (originalDeleted) return null;
try {
return new DiscordMessageBuilder(this, RateLimitUtil.complete(hook.retrieveOriginal()));
} catch (ErrorResponseException ignore) {
return null;
}
}

@Override
Expand Down
Loading

0 comments on commit 46f40cd

Please sign in to comment.