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 Oct 24, 2024
2 parents 13732e6 + fc90594 commit 318a8f0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
69 changes: 65 additions & 4 deletions src/main/java/link/locutus/discord/db/GuildHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,17 @@ public void onMemberLeaveVM(NationLeaveVacationEvent event) {
}
}

private void onNewApplicant(DBNation current) {
private void onNewApplicant(DBNation previous, DBNation current) {
Set<Integer> aaIds = db.getAllianceIds();
if (!aaIds.isEmpty()) {

// New applicant
if (current.getPositionEnum() == Rank.APPLICANT && aaIds.contains(current.getAlliance_id()) && !aaIds.contains(current.getAlliance_id()) && current.active_m() < 2880) {
if (current.getPositionEnum() == Rank.APPLICANT
&& aaIds.contains(current.getAlliance_id())
&& (previous == null || !aaIds.contains(previous.getAlliance_id())) && current.active_m() < 2880) {

sendApplicantMail(current);

MessageChannel channel = db.getOrNull(GuildKey.MEMBER_LEAVE_ALERT_CHANNEL);
if (channel != null) {
String type = "New Applicant Ingame";
Expand All @@ -509,6 +514,62 @@ private void onNewApplicant(DBNation current) {
}
}

public void sendApplicantMail(DBNation nation) {
if (GuildKey.MAIL_NEW_APPLICANTS.getOrNull(db) != Boolean.TRUE) return;
DBAlliance alliance = nation.getAlliance();
if (alliance == null) return;
ApiKeyPool mailKey = db.getMailKey();
if (mailKey == null) return;

String invite = alliance.getDiscord_link();

String title = "Joining " + alliance.getName();
MessageChannel channel = db.getOrNull(GuildKey.RECRUIT_MESSAGE_OUTPUT);
if (channel == null) channel = db.getOrNull(GuildKey.MEMBER_LEAVE_ALERT_CHANNEL);
if (channel != null) {
title += "/" + channel.getIdLong();
}
String message = """
Hey {leader}, we use DISCORD to coordinate with our members. So please join our discord server.<br>
Discord is a texting application that you can install from playstore or open in your browser!<br>
<a href=\"https://discord.com/download\">Download Discord</a><br>""";
if (invite != null && !invite.isEmpty()) {
message += "<br><br>Once you have discord installed, click the link below to join our server:<br>" +
"<a href=\"{invite}\">Join Discord</a>";
}
String newMessage = GuildKey.MAIL_NEW_APPLICANTS_TEXT.getOrNull(db);
if (newMessage != null && !newMessage.isEmpty()) {
message = newMessage;
if (!newMessage.contains("<br>") && !newMessage.contains("<br/>") && !newMessage.contains("<br />")) {
message = MarkupUtil.markdownToHTML(message);
}
}

if (invite != null && !invite.isEmpty()) {
message = message.replace("{invite}", invite);
}

NationPlaceholders formatter = Locutus.imp().getCommandManager().getV2().getNationPlaceholders();
if (message.contains("%") || message.contains("{")) {
try {
message = formatter.format2(guild, null, null, message, nation, false);
} catch (Exception e) {
e.printStackTrace();
return;
}
}

String finalTitle = title;
String finalMsg = message;
Locutus.imp().getExecutor().submit(() -> {
try {
nation.sendMail(mailKey, finalTitle, finalMsg, false);
} catch (IOException e) {
e.printStackTrace();
}
});
}

@Subscribe
public void onBuyInfra(CityInfraBuyEvent event) {
DBNation nation = event.getNation();
Expand Down Expand Up @@ -552,7 +613,7 @@ public void onNationChangeAlliance(NationChangeAllianceEvent event) {
DBNation current = event.getCurrent();
DBNation previous = event.getPrevious();

onNewApplicant(current);
onNewApplicant(previous, current);
autoRoleMemberApp(current);
Set<Integer> aaIds = db.getAllianceIds();
if (!aaIds.isEmpty()) {
Expand Down Expand Up @@ -2406,7 +2467,7 @@ public void handleInactiveAudit() {

@Subscribe
public void onNationCreate(NationCreateEvent event) {
onNewApplicant(event.getCurrent());
onNewApplicant(event.getPrevious(), event.getCurrent());
}

public void onGlobalNationCreate(NationCreateEvent event) {
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/link/locutus/discord/db/guild/GuildKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,33 @@ public String help() {
return "The channel to receive alerts when a member leaves";
}
}.setupRequirements(f -> f.requireValidAlliance());

public static GuildSetting<Boolean> MAIL_NEW_APPLICANTS = new GuildBooleanSetting(GuildSettingCategory.RECRUIT) {
@Override
public String help() {
return "If new applicants are sent an ingame mail with instructions on how to join the discord";
}
@NoFormat
@Command(descMethod = "help")
@RolePermission(Roles.ADMIN)
public String MAIL_NEW_APPLICANTS(@Me GuildDB db, @Me User user, boolean enabled) {
return MAIL_NEW_APPLICANTS.setAndValidate(db, user, enabled);
}
}.setupRequirements(f -> f.requireValidAlliance());
public static GuildSetting<String> MAIL_NEW_APPLICANTS_TEXT = new GuildStringSetting(GuildSettingCategory.RECRUIT) {
@Override
public String help() {
return "The message to send to new applicants via in-game mail.\n" +
"Supports nation placeholders, see: <https://github.com/xdnw/locutus/wiki/nation_placeholders>";
}
@NoFormat
@Command(descMethod = "help")
@RolePermission(Roles.ADMIN)
public String MAIL_NEW_APPLICANTS_TEXT(@Me GuildDB db, @Me User user, String message) {
return MAIL_NEW_APPLICANTS_TEXT.setAndValidate(db, user, message);
}
}.setupRequirements(f -> f.requireValidAlliance().requires(MAIL_NEW_APPLICANTS));

// public static GuildSetting<MessageChannel> LOW_TIER_BUY_CITY_ALERTS = new GuildChannelSetting(GuildSettingCategory.AUDIT) {
// @Override
// public String help() {
Expand Down

0 comments on commit 318a8f0

Please sign in to comment.