Search in sources :

Example 1 with GuildData

use of at.xirado.bean.data.GuildData in project Bean by Xirado.

the class GuildDataRoute method handle.

@Override
public Object handle(Request request, Response response) throws Exception {
    String authHeader = request.headers("authorization");
    if (authHeader == null || !authHeader.startsWith("Token ")) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Unauthorized").toString();
    }
    String token = authHeader.substring(7);
    byte[] tokenBytes = token.getBytes(StandardCharsets.UTF_8);
    if (!Bean.getInstance().getAuthenticator().isAuthenticated(tokenBytes)) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Invalid token (Try logging out and in again)").toString();
    }
    DataObject user = Bean.getInstance().getAuthenticator().getUser(tokenBytes);
    DataObject body = DataObject.fromJson(request.bodyAsBytes());
    if (body.isNull("guild") || body.isNull("data")) {
        response.status(400);
        return DataObject.empty().put("code", 400).put("message", "Bad request").toString();
    }
    long guildId = Long.parseUnsignedLong(body.getString("guild"));
    long userId = user.getLong("id");
    ShardManager manager = Bean.getInstance().getShardManager();
    Guild guild = manager.getGuildById(guildId);
    if (guild == null) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Unauthorized").toString();
    }
    Member member;
    try {
        member = guild.retrieveMemberById(userId).complete();
    } catch (ErrorResponseException ex) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", ex.getMessage()).toString();
    }
    if (!member.hasPermission(Permission.ADMINISTRATOR)) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Unauthorized").toString();
    }
    GuildData guildData = GuildManager.getGuildData(guild);
    // Stuff to update
    DataObject updatedData = body.getObject("data");
    // old data
    DataObject currentData = guildData.toData();
    for (String key : updatedData.keys()) {
        if (ALLOWED_SETTINGS.contains(key))
            currentData.put(key, updatedData.get(key));
    }
    guildData.update();
    return user.toString();
}
Also used : GuildData(at.xirado.bean.data.GuildData) DataObject(net.dv8tion.jda.api.utils.data.DataObject) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) Guild(net.dv8tion.jda.api.entities.Guild) Member(net.dv8tion.jda.api.entities.Member)

Example 2 with GuildData

use of at.xirado.bean.data.GuildData in project Bean by Xirado.

the class GuildMemberJoinListener method onGuildMemberJoin.

@Override
public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) {
    if (GuildJoinListener.isGuildBanned(event.getGuild().getIdLong()))
        return;
    if (!event.getGuild().getSelfMember().hasPermission(Permission.MANAGE_ROLES))
        return;
    GuildData guildData = GuildManager.getGuildData(event.getGuild());
    Bean.getInstance().getScheduledExecutor().schedule(() -> {
        long totalXP = RankingSystem.getTotalXP(event.getGuild().getIdLong(), event.getUser().getIdLong());
        int level = RankingSystem.getLevel(totalXP);
        if (level > 0) {
            List<RoleReward> roleRewards = guildData.getEffectiveRoleRewards(level);
            Set<Role> rolesToAdd = new HashSet<>(event.getMember().getRoles());
            for (RoleReward reward : roleRewards) {
                if (reward.isPersistant()) {
                    Role role = event.getGuild().getRoleById(reward.getRoleId());
                    if (role != null && event.getGuild().getSelfMember().canInteract(role))
                        rolesToAdd.add(role);
                }
            }
            if (!rolesToAdd.isEmpty())
                event.getGuild().modifyMemberRoles(event.getMember(), rolesToAdd).queue();
        }
    }, 10, TimeUnit.SECONDS);
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GuildData(at.xirado.bean.data.GuildData) RoleReward(at.xirado.bean.misc.objects.RoleReward) HashSet(java.util.HashSet)

Example 3 with GuildData

use of at.xirado.bean.data.GuildData in project Bean by Xirado.

the class MessageCreateListener method onMessageReceived.

@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
    if (!event.isFromGuild())
        return;
    if (GuildJoinListener.isGuildBanned(event.getGuild().getIdLong())) {
        Metrics.MESSAGES.labels("banned").inc();
        return;
    }
    Metrics.MESSAGES.labels("messages").inc();
    if (event.isWebhookMessage() || event.getAuthor().isBot())
        return;
    Member member = event.getMember();
    if (member == null)
        return;
    String content = event.getMessage().getContentRaw();
    GuildData guildData = GuildManager.getGuildData(event.getGuild());
    String prefix = guildData.getPrefix();
    String[] args = content.split("\\s+");
    if (args.length == 1 && event.getMessage().getMentionedUsers().contains(event.getJDA().getSelfUser()) && event.getMessage().getReferencedMessage() == null) {
        event.getMessage().reply("<a:ping:818580038949273621>").mentionRepliedUser(false).queue(s -> {
        }, e -> {
        });
        return;
    }
    if (content.startsWith(prefix)) {
        Bean.getInstance().getCommandHandler().handleCommandFromGuild(event);
    }
}
Also used : GuildData(at.xirado.bean.data.GuildData) Member(net.dv8tion.jda.api.entities.Member)

Example 4 with GuildData

use of at.xirado.bean.data.GuildData in project Bean by Xirado.

the class MessageReactionAddListener method onMessageReactionAdd.

@Override
public void onMessageReactionAdd(@NotNull MessageReactionAddEvent e) {
    if (!e.isFromGuild())
        return;
    if (GuildJoinListener.isGuildBanned(e.getGuild().getIdLong()))
        return;
    try {
        if (e.getMember().getUser().isBot())
            return;
        Guild g = e.getGuild();
        long id = e.getMessageIdLong();
        ReactionEmote reactionemote = e.getReactionEmote();
        String reacted = reactionemote.isEmoji() ? reactionemote.getAsReactionCode() : reactionemote.getEmote().getId();
        GuildData data = GuildManager.getGuildData(e.getGuild());
        ReactionRole reactionRole = data.getReactionRoles().stream().filter(x -> x.getMessageId() == id && x.getEmote().equals(reacted)).findFirst().orElse(null);
        if (reactionRole != null) {
            Role role = e.getGuild().getRoleById(reactionRole.getRoleId());
            if (role != null)
                g.addRoleToMember(e.getMember(), role).queue(s -> {
                }, ex -> {
                });
        }
    } catch (Exception e2) {
        LOGGER.error("An error occured whilst executing reaction role event!", e2);
    }
}
Also used : Role(net.dv8tion.jda.api.entities.Role) ReactionRole(at.xirado.bean.data.ReactionRole) GuildManager(at.xirado.bean.data.GuildManager) ReactionEmote(net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote) GuildData(at.xirado.bean.data.GuildData) Guild(net.dv8tion.jda.api.entities.Guild) Logger(org.slf4j.Logger) Role(net.dv8tion.jda.api.entities.Role) ReactionRole(at.xirado.bean.data.ReactionRole) LoggerFactory(org.slf4j.LoggerFactory) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) MessageReactionAddEvent(net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent) NotNull(org.jetbrains.annotations.NotNull) GuildData(at.xirado.bean.data.GuildData) Guild(net.dv8tion.jda.api.entities.Guild) ReactionEmote(net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote) ReactionRole(at.xirado.bean.data.ReactionRole)

Example 5 with GuildData

use of at.xirado.bean.data.GuildData in project Bean by Xirado.

the class MessageReactionRemoveListener method onMessageReactionRemoveAll.

@Override
public void onMessageReactionRemoveAll(MessageReactionRemoveAllEvent e) {
    if (!e.isFromGuild())
        return;
    if (GuildJoinListener.isGuildBanned(e.getGuild().getIdLong()))
        return;
    try {
        long messageId = e.getMessageIdLong();
        GuildData data = GuildManager.getGuildData(e.getGuild());
        data.removeReactionRoles(messageId).update();
    } catch (Exception exception) {
        LOGGER.error("An error occured while executing GuildMessageReactionRemoveAllEvent!", exception);
    }
}
Also used : GuildData(at.xirado.bean.data.GuildData)

Aggregations

GuildData (at.xirado.bean.data.GuildData)10 GuildManager (at.xirado.bean.data.GuildManager)4 Role (net.dv8tion.jda.api.entities.Role)4 Guild (net.dv8tion.jda.api.entities.Guild)3 Member (net.dv8tion.jda.api.entities.Member)3 DataObject (net.dv8tion.jda.api.utils.data.DataObject)3 NotNull (org.jetbrains.annotations.NotNull)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Bean (at.xirado.bean.Bean)2 RoleReward (at.xirado.bean.misc.objects.RoleReward)2 List (java.util.List)2 Permission (net.dv8tion.jda.api.Permission)2 ReactionEmote (net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote)2 ListenerAdapter (net.dv8tion.jda.api.hooks.ListenerAdapter)2 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)2 DataArray (net.dv8tion.jda.api.utils.data.DataArray)2 Authenticator (at.xirado.bean.backend.Authenticator)1 DiscordCredentials (at.xirado.bean.backend.DiscordCredentials)1 ObjectBuilder (at.xirado.bean.backend.ObjectBuilder)1