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();
}
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);
}
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);
}
}
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);
}
}
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);
}
}
Aggregations