Skip to content

Commit

Permalink
Added support for Youtube/NND accounts, reworked ownerid to use longs…
Browse files Browse the repository at this point in the history
… rather than strings, added the option to disable chat commands, more general improvements
  • Loading branch information
a9lim committed Nov 26, 2023
1 parent b9fdeb8 commit 3c84355
Show file tree
Hide file tree
Showing 22 changed files with 241 additions and 241 deletions.
8 changes: 2 additions & 6 deletions src/main/java/a9lim/jdautilities/command/CommandClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@ public interface CommandClient {

int getCommandUses(String name);

String getOwnerId();
long getOwnerId();

long getOwnerIdLong();

String[] getCoOwnerIds();

long[] getCoOwnerIdsLong();
long[] getCoOwnerIds();

String getSuccess();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
public class CommandClientBuilder {
private Activity activity = Activity.playing("default");
private OnlineStatus status = OnlineStatus.ONLINE;
private String ownerId, serverInvite, success, warning, error, carbonKey, botsKey;
private String[] coOwnerIds;
private String serverInvite, success, warning, error, carbonKey, botsKey;
private long ownerId;
private long[] coOwnerIds;
private List<String> prefixes;
private final List<Command> commands = new LinkedList<>();
private CommandListener listener;
Expand All @@ -52,12 +53,12 @@ public CommandClient build() {
return client;
}

public CommandClientBuilder setOwnerId(String o) {
public CommandClientBuilder setOwnerId(long o) {
ownerId = o;
return this;
}

public CommandClientBuilder setCoOwnerIds(String... co) {
public CommandClientBuilder setCoOwnerIds(long... co) {
coOwnerIds = co;
return this;
}
Expand Down Expand Up @@ -89,11 +90,6 @@ public CommandClientBuilder setActivity(Activity a) {
return this;
}

public CommandClientBuilder useDefaultGame() {
activity = Activity.playing("default");
return this;
}

public CommandClientBuilder setStatus(OnlineStatus stat) {
status = stat;
return this;
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/a9lim/jdautilities/command/CommandEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,16 @@ public Member getSelfMember() {
}

public boolean isOwner() {
if (event.getAuthor().getId().equals(client.getOwnerId()))
if (event.getAuthor().getIdLong() == client.getOwnerId())
return true;
if (client.getCoOwnerIds() == null)
return false;
for (String id : client.getCoOwnerIds())
if (id.equals(event.getAuthor().getId()))
for (long id : client.getCoOwnerIds())
if (id == event.getAuthor().getIdLong())
return true;
return false;
}


// shortcuts

public User getAuthor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import a9lim.jdautilities.command.*;
import a9lim.jdautilities.commons.utils.FixedSizeCache;
import a9lim.jdautilities.commons.utils.SafeIdUtil;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.Permission;
Expand Down Expand Up @@ -58,8 +57,9 @@ public class CommandClientImpl implements CommandClient, EventListener {
private final OffsetDateTime start;
private final Activity activity;
private final OnlineStatus status;
private final String[] coOwnerIds;
private final String ownerId, serverInvite, success, warning, error, botsKey, carbonKey, defaultprefix;
private final long[] coOwnerIds;
private final long ownerId;
private final String serverInvite, success, warning, error, botsKey, carbonKey, defaultprefix;
private final List<String> prefixes;
private final HashMap<String, Integer> uses;
private final HashMap<String, Command> commandIndex;
Expand All @@ -76,18 +76,17 @@ public class CommandClientImpl implements CommandClient, EventListener {
private int totalGuilds;
private String helpword;

public CommandClientImpl(String inownerId, String[] incoOwnerIds, List<String> inprefix, Activity inactivity, OnlineStatus instatus, String inserverInvite,
public CommandClientImpl(long inownerId, long[] incoOwnerIds, List<String> inprefix, Activity inactivity, OnlineStatus instatus, String inserverInvite,
String insuccess, String inwarning, String inerror, String incarbonKey, String inbotsKey, List<Command> incommands,
boolean inshutdownAutomatically, ScheduledExecutorService inexecutor,
int linkedCacheSize, AnnotatedModuleCompiler incompiler, GuildSettingsManager<?> inmanager) {
Checks.check(inownerId != null, "Owner ID was set null or not set! Please provide an User ID to register as the owner!");

if (SafeIdUtil.badId(inownerId))
if (inownerId < 0)
LOG.warn(String.format("The provided Owner ID (%s) was found unsafe! Make sure ID is a non-negative long!", inownerId));

if (incoOwnerIds != null) {
for (String coOwnerId : incoOwnerIds) {
if (SafeIdUtil.badId(coOwnerId))
for (long coOwnerId : incoOwnerIds) {
if (coOwnerId < 0)
LOG.warn(String.format("The provided CoOwner ID (%s) was found unsafe! Make sure ID is a non-negative long!", coOwnerId));
}
}
Expand Down Expand Up @@ -231,31 +230,16 @@ public void addAnnotatedModule(Object module) {
}

@Override
public String getOwnerId() {
public long getOwnerId() {
return ownerId;
}

@Override
public long getOwnerIdLong() {
return Long.parseLong(ownerId);
}

@Override
public String[] getCoOwnerIds() {
return coOwnerIds;
}

@Override
public long[] getCoOwnerIdsLong() {
public long[] getCoOwnerIds() {
// Thought about using java.util.Arrays#setAll(T[], IntFunction<T>)
// here, but as it turns out it's actually the same thing as this but
// it throws an error if null. Go figure.
if (coOwnerIds == null)
return null;
long[] ids = new long[coOwnerIds.length];
for (int i = 0; i < ids.length; i++)
ids[i] = Long.parseLong(coOwnerIds[i]);
return ids;
return coOwnerIds;
}

@Override
Expand Down Expand Up @@ -347,7 +331,8 @@ private void onReady(ReadyEvent event) {
event.getJDA().shutdown();
return;
}
event.getJDA().getPresence().setPresence(status == null ? OnlineStatus.ONLINE : status,
// todo: look at this
event.getJDA().getPresence().setPresence(status,
activity == null ? null : "default".equals(activity.getName()) ? Activity.playing("Type " + defaultprefix + helpword) : activity);

// Start SettingsManager if necessary
Expand Down
39 changes: 0 additions & 39 deletions src/main/java/a9lim/jdautilities/commons/utils/SafeIdUtil.java

This file was deleted.

10 changes: 2 additions & 8 deletions src/main/java/a9lim/raiko/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class Bot {
private final PlaylistLoader playlists;
private final NowplayingHandler nowplaying;
private final AloneInVoiceHandler aloneInVoiceHandler;
private final ChatBot chatBot;

private boolean shuttingDown;
private JDA jda;
private GUI gui;
Expand All @@ -57,12 +55,11 @@ public Bot(EventWaiter inwaiter, BotConfig inconfig, SettingsManager insettings)
playlists = new PlaylistLoader(inconfig);
threadpool = Executors.newSingleThreadScheduledExecutor();
players = new PlayerManager(this);
players.init();
players.init(inconfig);
nowplaying = new NowplayingHandler(this);
nowplaying.init();
aloneInVoiceHandler = new AloneInVoiceHandler(this);
aloneInVoiceHandler.init();
chatBot = new ChatBot(inconfig);
}

public BotConfig getConfig() {
Expand Down Expand Up @@ -96,9 +93,6 @@ public NowplayingHandler getNowplayingHandler() {
public AloneInVoiceHandler getAloneInVoiceHandler() {
return aloneInVoiceHandler;
}
public ChatBot getChatBot() {
return chatBot;
}

public JDA getJDA() {
return jda;
Expand All @@ -111,7 +105,7 @@ public void closeAudioConnection(long guildId) {
}

public void resetGame() {
Activity game = config.getGame() == null || "none".equalsIgnoreCase(config.getGame().getName()) ? null : config.getGame();
Activity game = config.getGame();
if (!Objects.equals(jda.getPresence().getActivity(), game))
jda.getPresence().setActivity(game);
}
Expand Down
44 changes: 29 additions & 15 deletions src/main/java/a9lim/raiko/BotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class BotConfig implements AliasSource {
private Path path;
private String token, playlistsFolder,
successEmoji, warningEmoji, errorEmoji, loadingEmoji, searchingEmoji,
cgpttoken, preprompt;
cgpttoken, preprompt, ytemail, ytpw, nndemail, nndpw;
private List<String> prefixes;
private boolean stayInChannel, songInGame, npImages,
model, valid;
Expand Down Expand Up @@ -86,10 +86,16 @@ public void load() {
aloneTimeUntilStop = config.getLong("alonetimeuntilstop");
playlistsFolder = config.getString("playlistsfolder");
aliases = config.getConfig("aliases");
cgpttoken = config.getString("gpttoken");

cgpttoken = noneblank(config.getString("gpttoken"));
model = config.getBoolean("cheapmodel");
preprompt = config.getString("preprompt");

ytemail = noneblank(config.getString("ytemail"));
ytpw = noneblank(config.getString("ytpw"));
nndemail = noneblank(config.getString("nndemail"));
nndpw = noneblank(config.getString("nndpw"));

// we may need to write a new config file
boolean write = false;

Expand All @@ -108,19 +114,6 @@ public void load() {
}
}

// validate chatgpt token
if (cgpttoken == null || cgpttoken.isEmpty() || "CGPTTOKEN".equalsIgnoreCase(cgpttoken)) {
cgpttoken = prompt.prompt("""
Please provide an OpenAI token.
OpenAI Token:\s""");
if (cgpttoken == null) {
prompt.alert(Prompt.Level.WARNING, CONTEXT, "No token provided! Exiting.\n\nConfig Location: " + path.toAbsolutePath());
return;
} else {
write = true;
}
}

// validate bot owner
if (owner <= 0) {
try {
Expand Down Expand Up @@ -151,6 +144,8 @@ public void load() {
}
}

//todo: fix this

private void writeToFile() {
byte[] bytes = loadDefaultConfig().replace("BOT_TOKEN_HERE", token)
.replace("0 // OWNER ID", Long.toString(owner))
Expand Down Expand Up @@ -194,6 +189,9 @@ public static void writeDefaultConfig() {
}
}

public static String noneblank(String s){
return "none".equalsIgnoreCase(s) || s.isBlank() ? null : s;
}
public boolean isValid() {
return valid;
}
Expand Down Expand Up @@ -293,4 +291,20 @@ public boolean getModel(){
public String getPreprompt() {
return preprompt;
}

public String getYTEmail() {
return ytemail;
}

public String getYTPW() {
return ytpw;
}

public String getNNDEmail() {
return nndemail;
}

public String getNNDPW() {
return nndpw;
}
}
2 changes: 1 addition & 1 deletion src/main/java/a9lim/raiko/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void onReady(ReadyEvent event) {
log.warn("This bot is not on any guilds! Use the following link to add the bot to your guilds!");
log.warn(event.getJDA().getInviteUrl(Raiko.RECOMMENDED_PERMS));
}
event.getJDA().getGuilds().forEach((guild) -> {
event.getJDA().getGuilds().forEach(guild -> {
try {
VoiceChannel vc = bot.getSettingsManager().getSettings(guild).getVoiceChannel(guild);
if (bot.getSettingsManager().getSettings(guild).getDefaultPlaylist() != null
Expand Down
Loading

0 comments on commit 3c84355

Please sign in to comment.