Search in sources :

Example 6 with CommandContext

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);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) CommandContext(net.robinfriedli.aiode.command.CommandContext) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) ClientSession(net.robinfriedli.aiode.entities.ClientSession) Guild(net.dv8tion.jda.api.entities.Guild)

Example 7 with CommandContext

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);
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) NativeAudioSendFactory(com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) Guild(net.dv8tion.jda.api.entities.Guild) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) AudioManager(net.robinfriedli.aiode.audio.AudioManager) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) LoggingUncaughtExceptionHandler(net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler) BlockingTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) IAudioSendSystem(net.dv8tion.jda.api.audio.factory.IAudioSendSystem) Aiode(net.robinfriedli.aiode.Aiode) QueuedTask(net.robinfriedli.aiode.concurrent.QueuedTask) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Playable(net.robinfriedli.aiode.audio.Playable) Tuple2(groovy.lang.Tuple2) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack)

Example 8 with CommandContext

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;
        }
    });
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Guild(net.dv8tion.jda.api.entities.Guild)

Example 9 with CommandContext

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);
    }
}
Also used : MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) CommandManager(net.robinfriedli.aiode.command.CommandManager) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Session(org.hibernate.Session) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) GroovyShell(groovy.lang.GroovyShell) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) Aiode(net.robinfriedli.aiode.Aiode) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) List(java.util.List) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) CommandContext(net.robinfriedli.aiode.command.CommandContext) StoredScript(net.robinfriedli.aiode.entities.StoredScript) Optional(java.util.Optional) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) ExceptionUtils(net.robinfriedli.aiode.exceptions.ExceptionUtils) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) StoredScript(net.robinfriedli.aiode.entities.StoredScript) EmbedTable(net.robinfriedli.aiode.util.EmbedTable)

Example 10 with CommandContext

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);
    }
}
Also used : QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) CommandContext(net.robinfriedli.aiode.command.CommandContext) StoredScript(net.robinfriedli.aiode.entities.StoredScript) Session(org.hibernate.Session)

Aggregations

CommandContext (net.robinfriedli.aiode.command.CommandContext)21 Session (org.hibernate.Session)9 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)8 Guild (net.dv8tion.jda.api.entities.Guild)7 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)6 Aiode (net.robinfriedli.aiode.Aiode)6 SecurityManager (net.robinfriedli.aiode.command.SecurityManager)6 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)5 PermissionTarget (net.robinfriedli.aiode.command.PermissionTarget)5 CustomPermissionTarget (net.robinfriedli.aiode.entities.CustomPermissionTarget)5 StoredScript (net.robinfriedli.aiode.entities.StoredScript)3 List (java.util.List)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 Role (net.dv8tion.jda.api.entities.Role)2 User (net.dv8tion.jda.api.entities.User)2 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)2 AudioManager (net.robinfriedli.aiode.audio.AudioManager)2 ThreadExecutionQueue (net.robinfriedli.aiode.concurrent.ThreadExecutionQueue)2 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)2 GrantedRole (net.robinfriedli.aiode.entities.GrantedRole)2