Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/gpt'
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Jul 17, 2023
2 parents ec647e2 + dd29e0c commit 0316851
Show file tree
Hide file tree
Showing 21 changed files with 905 additions and 416 deletions.
16 changes: 9 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ dependencies {
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'

implementation "org.xerial:sqlite-jdbc:3.30.1"
implementation 'com.google.http-client:google-http-client-jackson2:1.43.3'

implementation('org.xerial:sqlite-jdbc:3.40.0.0')
implementation "com.google.code.gson:gson:2.8.6"
implementation 'org.jsoup:jsoup:1.12.1'
implementation 'org.bitbucket.cowwoc:diff-match-patch:1.2'
Expand All @@ -90,10 +92,11 @@ dependencies {
implementation 'com.ptsmods:mysqlw:1.7'
implementation 'org.openjdk.nashorn:nashorn-core:15.4'

implementation 'com.google.api-client:google-api-client:1.33.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.32.1'
implementation 'com.google.apis:google-api-services-sheets:v4-rev20210629-1.32.1'
implementation 'com.google.apis:google-api-services-drive:v3-rev20211107-1.32.1'
implementation 'com.google.api-client:google-api-client:2.0.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.34.1'
implementation 'com.google.apis:google-api-services-sheets:v4-rev20220927-2.0.0'
implementation 'com.google.apis:google-api-services-drive:v3-rev20220815-2.0.0'
implementation 'com.google.apis:google-api-services-docs:v1-rev20220609-2.0.0'
implementation 'de.erichseifert.gral:gral-core:0.11'

implementation 'org.kefirsf:kefirbb:1.5'
Expand Down Expand Up @@ -133,15 +136,14 @@ dependencies {
// https://mvnrepository.com/artifact/de.siegmar/fastcsv
implementation group: 'de.siegmar', name: 'fastcsv', version: '2.2.0'

implementation('org.xerial:sqlite-jdbc:3.40.0.0')

implementation('org.hibernate:hibernate-core:6.1.5.Final')
implementation('org.hibernate:hibernate-community-dialects:6.1.5.Final')

implementation('org.jooq:jooq:3.17.5')
implementation("org.jooq:jooq-meta:3.17.5")
implementation("org.jooq:jooq-codegen:3.17.5")

jooqGenerator 'org.xerial:sqlite-jdbc:3.36.0.3'

implementation("com.theokanning.openai-gpt3-java:client:0.12.0")

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/link/locutus/discord/Locutus.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,7 @@ public void autoRole(DBNation nation) {
Member member = guild.getMember(discordUser);
if (member != null) {
try {
db.getAutoRoleTask().autoRole(member, s -> {
});
db.getAutoRoleTask().autoRole(member, nation, true);
} catch (Throwable e) {
e.printStackTrace();
}
Expand Down
64 changes: 10 additions & 54 deletions src/main/java/link/locutus/discord/commands/account/AutoRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
import link.locutus.discord.apiv1.enums.Rank;
import link.locutus.discord.commands.manager.Command;
import link.locutus.discord.commands.manager.CommandCategory;
import link.locutus.discord.commands.manager.v2.binding.annotation.Switch;
import link.locutus.discord.commands.manager.v2.command.IMessageIO;
import link.locutus.discord.commands.manager.v2.impl.pw.CM;
import link.locutus.discord.commands.manager.v2.impl.pw.commands.UtilityCommands;
import link.locutus.discord.db.GuildDB;
import link.locutus.discord.db.entities.DBNation;
import link.locutus.discord.db.guild.GuildKey;
import link.locutus.discord.user.Roles;
import link.locutus.discord.util.RateLimitUtil;
import link.locutus.discord.util.StringMan;
import link.locutus.discord.util.TimeUtil;
import link.locutus.discord.util.discord.DiscordUtil;
import link.locutus.discord.util.task.roles.AutoRoleInfo;
import link.locutus.discord.util.task.roles.IAutoRoleTask;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -53,6 +55,7 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m
if (args.size() != 1) {
return usage(args.size(), 1, channel);
}
boolean force = flags.contains('f');

GuildDB db = Locutus.imp().getGuildDB(guild);
if (db == null) return "No registered guild.";
Expand All @@ -63,64 +66,17 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m

if (args.get(0).equalsIgnoreCase("*")) {
if (!Roles.INTERNAL_AFFAIRS.has(author, guild)) return "No permission";
Function<Long, Boolean> func = i -> {
task.autoRoleAll(new Consumer<>() {
private boolean messaged = false;

@Override
public void accept(String s) {
if (messaged) return;
messaged = true;
channel.send(s);
}
});
return true;
};
channel.send("Please wait...");
func.apply(0L);

if (db.hasAlliance()) {
for (Map.Entry<Member, GuildDB.UnmaskedReason> entry : db.getMaskedNonMembers().entrySet()) {
response.append(entry.getKey().getAsMention());
DBNation nation = DiscordUtil.getNation(entry.getKey().getUser());
if (nation != null) {
String active = TimeUtil.secToTime(TimeUnit.MINUTES, nation.getActive_m());
if (nation.getActive_m() > 10000) active = "**" + active + "**";
response.append(nation.getName()).append(" | <").append(nation.getNationUrl()).append("> | ").append(active).append(" | ").append(Rank.byId(nation.getPosition())).append(" in the alliance:").append(nation.getAllianceName());
}
response.append("- ").append(entry.getValue());
response.append("\n");
}
}


JSONObject command = CM.role.autoassign.cmd.create().toJson();
return UtilityCommands.autoroleall(author, db, channel, command, force);
} else {
DBNation nation = DiscordUtil.parseNation(args.get(0));
if (nation == null) return "That nation isn't registered: " + CM.register.cmd.toSlashMention() + "";
if (nation == null) return "That nation isn't registered: `" + args.get(0) + "` see:" + CM.register.cmd.toSlashMention() + "";
User user = nation.getUser();
if (user == null) return "User is not registered.";
Member member = db.getGuild().getMember(user);
if (member == null) return "Member not found in guild: " + DiscordUtil.getFullUsername(user);
List<String> output = new ArrayList<>();
Consumer<String> out = output::add;
task.autoRole(member, out);

if (!output.isEmpty()) {
channel.send(StringMan.join(output, "\n"));
}
JSONObject command = CM.role.autorole.cmd.create(user.getAsMention()).toJson();
return UtilityCommands.autorole(db, channel, command, member, force);
}

response.append("Done!");

if (db.getOrNull(GuildKey.AUTOROLE) == null) {
response.append("\n- AutoRole disabled. To enable it use: ").append(GuildKey.AUTOROLE.getCommandMention());
} else response.append("\n- AutoRole Mode: ").append(db.getOrNull(GuildKey.AUTOROLE) + "");
if (db.getOrNull(GuildKey.AUTONICK) == null) {
response.append("\n- AutoNick disabled. To enable it use: " + GuildKey.AUTONICK.getCommandMention() + "");
}
else response.append("\n- AutoNick Mode: ").append(db.getOrNull(GuildKey.AUTONICK) + "");
if (Roles.REGISTERED.toRole(db) == null) response.append("\n- Please set a registered role: " + CM.role.setAlias.cmd.create(Roles.REGISTERED.name(), "", null, null).toSlashCommand() + "");

return response.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ public String create(@Me Guild guild, @Me User user, @Me DBNation me, @Me IMessa
return help;
}

@Command(desc = "Autorole members for an exchange")
@RolePermission(guild = StockDB.ROOT_GUILD)
public String autoRole(@Me GuildDB db, @Me Guild guildl, @Me Member member, @Me DBNation me, StockDB stockDB, @Me Exchange exchange) {
exchange.autoRole(member);
exchange.autoRole();
StringBuilder response = new StringBuilder();
db.getAutoRoleTask().autoRole(member, f -> response.append(f).append("\n"));
return response.toString().trim();
}

@Command(desc = "Destroy an exchange")
@RolePermission(guild = StockDB.ROOT_GUILD)
public String drop(@Me Guild guild, @Me IMessageIO io, @Me DBNation me, @Me JSONObject command, StockDB db, @Me Exchange exchange, @Switch("f") boolean force) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,12 @@ public String embassy(@Me GuildDB db, @Me Guild guild, @Me User user, @Me DBNati
Role role = aaRoles.get(nation.getAlliance_id());
if (role == null) {
db.addCoalition(nation.getAlliance_id(), Coalition.MASKEDALLIANCES);
GuildDB.AutoRoleOption autoRoleValue = db.getOrNull(GuildKey.AUTOROLE);
GuildDB.AutoRoleOption autoRoleValue = db.getOrNull(GuildKey.AUTOROLE_ALLIANCES);
if (autoRoleValue == null || autoRoleValue == GuildDB.AutoRoleOption.FALSE) {
return "AutoRole is disabled. See " + GuildKey.AUTOROLE.getCommandMention() + "";
return "AutoRole is disabled. See " + GuildKey.AUTOROLE_ALLIANCES.getCommandMention() + "";
}
db.getAutoRoleTask().syncDB();
db.getAutoRoleTask().autoRole(member, f -> {});
db.getAutoRoleTask().autoRole(member, nation, true);
aaRoles = DiscordUtil.getAARoles(guild.getRoles());
role = aaRoles.get(nation.getAlliance_id());
if (role == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
import link.locutus.discord.util.offshore.test.IAChannel;
import link.locutus.discord.util.sheet.SpreadSheet;
import link.locutus.discord.util.task.nation.MultiReport;
import link.locutus.discord.util.task.roles.AutoRoleInfo;
import link.locutus.discord.util.task.roles.IAutoRoleTask;
import link.locutus.discord.apiv1.domains.subdomains.attack.DBAttack;
import link.locutus.discord.apiv1.domains.subdomains.SAllianceContainer;
import link.locutus.discord.apiv1.enums.AttackType;
import link.locutus.discord.apiv1.enums.MilitaryUnit;
import link.locutus.discord.apiv1.enums.Rank;
Expand Down Expand Up @@ -1187,39 +1187,23 @@ public String ProjectCost(Set<Project> projects, @Default("false") boolean techn

@Command(desc = "Auto rank users on discord")
@RolePermission(Roles.INTERNAL_AFFAIRS)
public String autoroleall(@Me User author, @Me GuildDB db, @Me IMessageIO channel) {
public static String autoroleall(@Me User author, @Me GuildDB db, @Me IMessageIO channel, @Me JSONObject command, @Switch("f") boolean force) {
IAutoRoleTask task = db.getAutoRoleTask();
task.syncDB();

StringBuilder response = new StringBuilder();

Function<Long, Boolean> func = new Function<Long, Boolean>() {
@Override
public Boolean apply(Long i) {
task.autoRoleAll(new Consumer<String>() {
private boolean messaged = false;
@Override
public void accept(String s) {
if (messaged) return;
messaged = true;
channel.send(s);
}
});
return true;
}
};
if (Roles.ADMIN.hasOnRoot(author) || true) {
AutoRoleInfo result = task.autoRoleAll(false);
if (force) {
channel.send("Please wait...");
func.apply(0L);
} else if (Roles.INTERNAL_AFFAIRS.has(author, db.getGuild())) {
String taskId = getClass().getSimpleName() + "." + db.getGuild().getId();
Boolean result = TimeUtil.runTurnTask(taskId, func);
if (result != Boolean.TRUE) return "Task already ran this turn";
} else {
return "No permission";
result.execute();
return result.getChangesAndErrorMessage();
}

String body = "`note: Results may differ if settings or users change`\n" +
result.getSyncDbResult() + "\n------\n" + result.toString();
IMessageBuilder msg = channel.create().confirmation("Auto role all", body, command);

if (db.hasAlliance()) {
StringBuilder response = new StringBuilder();
for (Map.Entry<Member, GuildDB.UnmaskedReason> entry : db.getMaskedNonMembers().entrySet()) {
User user = entry.getKey().getUser();
response.append("`" + DiscordUtil.getFullUsername(user) + "`" + "`<@" + user.getIdLong() + ">`");
Expand All @@ -1232,42 +1216,32 @@ public void accept(String s) {
response.append("- ").append(entry.getValue());
response.append("\n");
}
if (response.length() > 0) {
msg.append(response.toString());
}
}

if (db.getOrNull(GuildKey.AUTOROLE) == null) {
response.append("\n- AutoRole disabled. To enable it use: " + GuildKey.AUTOROLE.getCommandMention() + "");
}
else response.append("\n- AutoRole Mode: ").append(db.getOrNull(GuildKey.AUTOROLE) + "");
if (db.getOrNull(GuildKey.AUTONICK) == null) {
response.append("\n- AutoNick disabled. To enable it use: " + GuildKey.AUTONICK.getCommandMention() + "");
}
else response.append("\n- AutoNick Mode: ").append(db.getOrNull(GuildKey.AUTONICK) + "");
if (Roles.REGISTERED.toRole(db) == null) response.append("\n- Please set a registered role: " + CM.role.setAlias.cmd.create(Roles.REGISTERED.name(), "", null, null).toSlashCommand() + "");
return response.toString();
msg.send();
return null;
}

@Command(desc = "Auto rank users on discord")
public String autorole(@Me GuildDB db, @Me IMessageIO channel, Member member) {
public static String autorole(@Me GuildDB db, @Me IMessageIO channel, @Me JSONObject command, Member member, @Switch("f") boolean force) {
IAutoRoleTask task = db.getAutoRoleTask();
task.syncDB();

DBNation nation = DiscordUtil.getNation(member.getUser());
Consumer<String> out = channel::send;
if (nation == null) out.accept("That nation isn't registered: " + CM.register.cmd.toSlashMention() + "");
task.autoRole(member, out);

StringBuilder response = new StringBuilder("Done!");

if (db.getOrNull(GuildKey.AUTOROLE) == null) {
response.append("\n- AutoRole disabled. To enable it use: " + GuildKey.AUTOROLE.getCommandMention() + "");
}
else response.append("\n- AutoRole Mode: ").append((Object) db.getOrNull(GuildKey.AUTOROLE));
if (db.getOrNull(GuildKey.AUTONICK) == null) {
response.append("\n- AutoNick disabled. To enable it use: " + GuildKey.AUTONICK.getCommandMention() + "");
if (nation == null) return "That nation isn't registered: " + CM.register.cmd.toSlashMention();
AutoRoleInfo result = task.autoRole(member, nation, force);
if (force) {
result.execute();
return result.getChangesAndErrorMessage();
}
else response.append("\n- AutoNick Mode: ").append((Object) db.getOrNull(GuildKey.AUTONICK));
if (Roles.REGISTERED.toRole(db) == null) response.append("\n- Please set a registered role: " + CM.role.setAlias.cmd.create(Roles.REGISTERED.name(), "", null, null).toSlashCommand() + "");
return response.toString();

String body = "`note: Results may differ if settings or users change`\n" +
result.getSyncDbResult() + "\n------\n" + result.toString();
channel.create().confirmation("Auto role " + nation.getNation(), body, command).send();
return null;
}

@RolePermission(value = {Roles.MILCOM, Roles.INTERNAL_AFFAIRS,Roles.ECON,Roles.FOREIGN_AFFAIRS}, any=true)
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/link/locutus/discord/db/GuildDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,12 @@ public void createTables() {
} catch (SQLException e) {
e.printStackTrace();
}
{
// Rename AUTOROLE to AUTOROLE_ALLIANCES in `key`
String updateKey = "UPDATE INFO SET key = 'AUTOROLE_ALLIANCES' WHERE key = 'AUTOROLE'";
executeStmt(updateKey);
}

};
{
String create = "CREATE TABLE IF NOT EXISTS `LOANS` (`loan_id` INTEGER PRIMARY KEY AUTOINCREMENT, `server` BIGINT NOT NULL, `message`, `receiver` INT NOT NULL, `resources` BLOB NOT NULL, `due` BIGINT NOT NULL, `repaid` BIGINT NOT NULL)";
Expand Down Expand Up @@ -2526,8 +2532,8 @@ public String toString() {

public enum AutoRoleOption {
FALSE("No roles given"),
ALL("Roles for the alliance"),
ALLIES("Roles for allies (e.g. if a coalition server)"),
ALL("Alliance roles created for all (see: `AUTOROLE_TOP_X`)"),
ALLIES("Alliance roles created for allies (see: `allies` coalition)"),
;

private final String description;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/link/locutus/discord/db/GuildHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,17 +414,14 @@ public void onNationChangeColor(NationChangeColorEvent event) {
public void onCityCreate(CityCreateEvent event) {
DBNation nation = event.getNation();
if (nation != null) {

// Auto role
User user = nation.getUser();
if (user != null) {
Member member = guild.getMember(user);
if (member != null) {
db.getAutoRoleTask().autoRoleCities(member, () -> nation, ignore1 -> {}, ignore2 -> {});
db.getAutoRoleTask().autoRoleCities(member, nation);
}
}


}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/link/locutus/discord/db/entities/DBNation.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import link.locutus.discord.util.sheet.SpreadSheet;
import link.locutus.discord.util.task.MailTask;
import link.locutus.discord.util.task.multi.GetUid;
import link.locutus.discord.util.task.roles.AutoRoleInfo;
import link.locutus.discord.util.trade.TradeManager;
import link.locutus.discord.web.jooby.handler.CommandResult;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -371,9 +372,8 @@ public String register(User user, GuildDB db, boolean isNewRegistration) {
if (member != null) {
RateLimitUtil.complete(db.getGuild().addRoleToMember(user, role));
output.append("You have been assigned the role: " + role.getName());
db.getAutoRoleTask().autoRole(member, s -> {
output.append("\n").append(s);
});
AutoRoleInfo task = db.getAutoRoleTask().autoRole(member, this, true);
output.append("\n" + task.getChangesAndErrorMessage());
} else {
member = db.getGuild().retrieveMember(user).complete();
output.append("Member " + DiscordUtil.getFullUsername(user) + " not found in guild: " + db.getGuild());
Expand Down
Loading

0 comments on commit 0316851

Please sign in to comment.