use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class ConnectCommand method doRun.
@Override
public void doRun() {
CommandContext context = getContext();
Guild guild = context.getGuild();
User user = context.getUser();
long guildId = guild.getIdLong();
long userId = user.getIdLong();
long textChannelId = context.getChannel().getIdLong();
if (USERS_WITH_PENDING_CONNECTION.put(userId, DUMMY) != null) {
throw new InvalidCommandException("The bot is already waiting to receive the token in the DMs.");
}
ClientSession clientSession = new ClientSession();
clientSession.setGuildId(guildId);
clientSession.setUserId(userId);
clientSession.setTextChannelId(textChannelId);
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setDescription("Send me the token shown by the web client as a direct message");
getMessageService().sendTemporary(embedBuilder, context.getChannel());
awaitPrivateMessage(clientSession, userId, guild, user, 1);
}
use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class AudioTrafficSimulationCommand method runAdmin.
@Override
public void runAdmin() throws Exception {
Aiode aiode = Aiode.get();
AudioManager audioManager = aiode.getAudioManager();
AudioPlayerManager playerManager = audioManager.getPlayerManager();
PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), new BlockingTrackLoadingExecutor());
CommandContext context = getContext();
Guild guild = context.getGuild();
ThreadExecutionQueue threadExecutionQueue = aiode.getExecutionQueueManager().getForGuild(guild);
SpotifyApi spotifyApi = context.getSpotifyApi();
AudioTrackLoader audioTrackLoader = new AudioTrackLoader(playerManager);
int streams = getArgumentValueOrElse("streams", DEFAULT_STREAM_COUNT);
int durationSecs = getArgumentValueOrElse("duration", DEFAULT_DURATION);
String playbackUrl = getArgumentValueOrElse("url", DEFAULT_PLAYBACK_URL);
Integer nativeBuffer = getArgumentValueWithTypeOrElse("nativeBuffer", Integer.class, null);
List<Playable> playables = playableFactory.createPlayables(playbackUrl, spotifyApi, true);
if (playables.isEmpty()) {
throw new InvalidCommandException("No playables found for url " + playbackUrl);
}
Playable playable = playables.get(0);
AudioItem audioItem = audioTrackLoader.loadByIdentifier(playable.getPlaybackUrl());
AudioTrack track;
if (audioItem instanceof AudioTrack) {
track = (AudioTrack) audioItem;
} else {
throw new IllegalStateException("Could not get AudioTrack for Playable " + playable);
}
LoopTrackListener loopTrackListener = new LoopTrackListener(track);
List<Thread> playbackThreads = nativeBuffer != null ? null : Lists.newArrayListWithCapacity(streams);
List<Tuple2<IAudioSendSystem, AudioPlayer>> audioSendSystems = nativeBuffer != null ? Lists.newArrayListWithCapacity(streams) : null;
NativeAudioSendFactory nativeAudioSendFactory = nativeBuffer != null ? new NativeAudioSendFactory(nativeBuffer) : null;
LoggingUncaughtExceptionHandler eh = new LoggingUncaughtExceptionHandler();
for (int i = 0; i < streams; i++) {
AudioPlayer player = playerManager.createPlayer();
player.addListener(loopTrackListener);
player.playTrack(track.makeClone());
if (nativeAudioSendFactory != null) {
IAudioSendSystem sendSystem = nativeAudioSendFactory.createSendSystem(new FakePacketProvider(player));
audioSendSystems.add(new Tuple2<>(sendSystem, player));
} else {
Thread playbackThread = new Thread(new PlayerPollingRunnable(player));
playbackThread.setDaemon(true);
playbackThread.setUncaughtExceptionHandler(eh);
playbackThread.setName("simulated-playback-thread-" + i);
playbackThreads.add(playbackThread);
}
}
QueuedTask playbackThreadsManagementTask = new QueuedTask(threadExecutionQueue, new FakePlayerManagementTask(playbackThreads, audioSendSystems, durationSecs)) {
@Override
protected boolean isPrivileged() {
return true;
}
};
playbackThreadsManagementTask.setName("simulated-playback-management-task");
threadExecutionQueue.add(playbackThreadsManagementTask, false);
}
use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class RenameCommand method doRun.
@Override
public void doRun() {
String name = getCommandInput();
CommandContext context = getContext();
Guild guild = context.getGuild();
if (name.length() < 1 || name.length() > 32) {
throw new InvalidCommandException("Length should be 1 - 32 characters");
}
RENAME_SYNC.execute(guild.getIdLong(), () -> {
invoke(() -> context.getGuildContext().setBotName(name));
try {
guild.getSelfMember().modifyNickname(name).complete();
couldChangeNickname = true;
} catch (InsufficientPermissionException ignored) {
couldChangeNickname = false;
}
});
}
use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class AbstractScriptCommand method listAllScripts.
private void listAllScripts(QueryBuilderFactory queryBuilderFactory, Session session, CommandContext context) {
List<StoredScript> storedScripts = queryBuilderFactory.find(StoredScript.class).where((cb, root, subQueryFactory) -> cb.equal(root.get("scriptUsage"), subQueryFactory.createUncorrelatedSubQuery(StoredScript.ScriptUsage.class, "pk").where((cb1, root1) -> cb1.equal(root1.get("uniqueId"), scriptUsageId)).build(session))).build(session).getResultList();
EmbedBuilder embedBuilder = new EmbedBuilder();
if (storedScripts.isEmpty()) {
embedBuilder.setDescription(String.format("No %ss saved", scriptUsageId));
getMessageService().sendTemporary(embedBuilder, context.getChannel());
} else {
embedBuilder.setDescription(String.format("Show a specific %s by entering its identifier", scriptUsageId));
EmbedTable table = new EmbedTable(embedBuilder);
table.addColumn("Identifier", storedScripts, StoredScript::getIdentifier);
table.addColumn("Active", storedScripts, script -> String.valueOf(script.isActive()));
table.build();
sendMessage(embedBuilder);
}
}
use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.
the class AbstractScriptCommand method doRun.
@Override
public void doRun() {
CommandContext context = getContext();
Session session = context.getSession();
QueryBuilderFactory queryBuilderFactory = Aiode.get().getQueryBuilderFactory();
if (argumentSet("delete")) {
StoredScript foundScript = findScript(session);
invoke(() -> session.delete(foundScript));
} else if (argumentSet("identifier") && !getCommandInput().isBlank()) {
saveNewScript(queryBuilderFactory, session, context);
} else if (argumentSet("activate")) {
toggleActivation(false, session);
} else if (argumentSet("deactivate")) {
toggleActivation(true, session);
} else if (getCommandInput().isBlank()) {
if (argumentSet("identifier")) {
sendScript(getArgumentValue("identifier"), session);
} else {
listAllScripts(queryBuilderFactory, session, context);
}
} else {
sendScript(getCommandInput(), session);
}
}
Aggregations