Search in sources :

Example 6 with GuildContext

use of net.robinfriedli.aiode.discord.GuildContext in project aiode by robinfriedli.

the class WidgetListener method handleWidgetExecution.

private void handleWidgetExecution(GuildMessageReactionAddEvent event, AbstractWidget activeWidget) {
    TextChannel channel = event.getChannel();
    Guild guild = event.getGuild();
    Aiode aiode = Aiode.get();
    SpotifyApi.Builder spotifyApiBuilder = aiode.getSpotifyApiBuilder();
    GuildContext guildContext = aiode.getGuildManager().getContextForGuild(guild);
    String emojiUnicode = event.getReaction().getReactionEmote().getName();
    try {
        Message message = activeWidget.getMessage().retrieve();
        if (message == null) {
            throw new IllegalStateException("Message of widget could not be retrieved.");
        }
        CommandContext commandContext = new CommandContext(event, guildContext, message, hibernateComponent.getSessionFactory(), spotifyApiBuilder, emojiUnicode);
        activeWidget.handleReaction(event, commandContext);
    } catch (UserException e) {
        messageService.sendError(e.getMessage(), channel);
    } catch (InsufficientPermissionException e) {
        Permission permission = e.getPermission();
        messageService.send("Bot is missing permission: " + permission.getName(), channel);
        logger.warn(String.format("Missing permission %s on guild %s", permission, guild));
    } catch (Exception e) {
        logger.error("Exception while handling WidgetAction execution.", e);
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandContext(net.robinfriedli.aiode.command.CommandContext) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) UserException(net.robinfriedli.aiode.exceptions.UserException) TextChannel(net.dv8tion.jda.api.entities.TextChannel) GuildContext(net.robinfriedli.aiode.discord.GuildContext) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) Permission(net.dv8tion.jda.api.Permission) UserException(net.robinfriedli.aiode.exceptions.UserException)

Example 7 with GuildContext

use of net.robinfriedli.aiode.discord.GuildContext in project aiode by robinfriedli.

the class RequestInterceptorHandler method setupExecutionContext.

private ExecutionContext setupExecutionContext(ClientSession clientSession) {
    SessionFactory sessionFactory = hibernateComponent.getSessionFactory();
    Guild guild = shardManager.getGuildById(clientSession.getGuildId());
    if (guild == null) {
        return null;
    }
    Member member = guild.getMemberById(clientSession.getUserId());
    if (member == null) {
        return null;
    }
    TextChannel textChannel = guild.getTextChannelById(clientSession.getTextChannelId());
    if (textChannel == null) {
        textChannel = guildManager.getDefaultTextChannelForGuild(guild);
        TextChannel finalTextChannel = textChannel;
        hibernateComponent.consumeSession(session -> {
            clientSession.setTextChannelId(finalTextChannel.getIdLong());
            session.update(clientSession);
        });
    }
    JDA jda = guild.getJDA();
    GuildContext guildContext = guildManager.getContextForGuild(guild);
    return new ExecutionContext(guild, guildContext, jda, member, sessionFactory, spotifyApiBuilder, textChannel);
}
Also used : SessionFactory(org.hibernate.SessionFactory) TextChannel(net.dv8tion.jda.api.entities.TextChannel) GuildContext(net.robinfriedli.aiode.discord.GuildContext) ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) JDA(net.dv8tion.jda.api.JDA) Guild(net.dv8tion.jda.api.entities.Guild) Member(net.dv8tion.jda.api.entities.Member)

Aggregations

GuildContext (net.robinfriedli.aiode.discord.GuildContext)7 Guild (net.dv8tion.jda.api.entities.Guild)6 TextChannel (net.dv8tion.jda.api.entities.TextChannel)3 Aiode (net.robinfriedli.aiode.Aiode)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 CommandContext (net.robinfriedli.aiode.command.CommandContext)2 CommandExecutionQueueManager (net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)2 GuildManager (net.robinfriedli.aiode.discord.GuildManager)2 Duration (java.time.Duration)1 LocalDateTime (java.time.LocalDateTime)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ToIntFunction (java.util.function.ToIntFunction)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 JDA (net.dv8tion.jda.api.JDA)1 Permission (net.dv8tion.jda.api.Permission)1 Member (net.dv8tion.jda.api.entities.Member)1 Message (net.dv8tion.jda.api.entities.Message)1 Role (net.dv8tion.jda.api.entities.Role)1 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)1