use of tk.ardentbot.rethink.models.GuildModel in project Ardent by adamint.
the class CommandFactory method pass.
/**
* Handles generic message events, parses message content
* and creates a new AsyncCommandExecutor that will execute the command
*
* @param event the MessageReceivedEvent to be handled
*/
public void pass(MessageReceivedEvent event, String prefix) {
try {
Guild guild = event.getGuild();
User ardent = guild.getSelfMember().getUser();
User user = event.getAuthor();
if (user.isBot())
return;
Message message = event.getMessage();
MessageChannel channel = event.getChannel();
String[] args = message.getContent().split(" ");
String rawContent = message.getRawContent();
if (rawContent.startsWith(guild.getSelfMember().getUser().getAsMention())) {
rawContent = rawContent.replaceFirst(ardent.getAsMention(), "");
if (rawContent.length() == 0)
channel.sendMessage("Type @Ardent [msg] to talk to me or /help to see a list of commands").queue();
else {
if (!Ardent.disabledCommands.contains("cleverbot")) {
channel.sendMessage(Unirest.post("https://cleverbot.io/1.0/ask").field("user", Ardent.cleverbotUser).field("key", Ardent.cleverbotKey).field("nick", "ardent").field("text", rawContent).asJson().getBody().getObject().getString("response")).queue();
} else
channel.sendMessage("Cleverbot is currently disabled, sorry.").queue();
}
return;
}
if (!args[0].startsWith(prefix))
return;
String cmd = args[0].replaceFirst(prefix, "");
final boolean[] ranCommand = { false };
String pre = StringEscapeUtils.escapeJava(prefix);
if (args[0].startsWith(pre)) {
args[0] = args[0].replaceFirst(pre, "");
baseCommands.forEach(command -> {
if (command.getBotCommand().containsAlias(args[0])) {
command.botCommand.usages++;
if (!Ardent.disabledCommands.contains(command.getName())) {
EntityGuild entityGuild = EntityGuild.get(guild);
for (RestrictedUser u : entityGuild.getRestrictedUsers()) {
if (u.getUserId().equalsIgnoreCase(user.getId())) {
command.sendRestricted(user);
return;
}
}
GuildModel guildModel = BaseCommand.asPojo(r.table("guilds").get(guild.getId()).run(connection), GuildModel.class);
if (guildModel == null) {
guildModel = new GuildModel(guild.getId(), "english", "/");
r.table("guilds").insert(r.json(shard.gson.toJson(guildModel))).runNoReply(connection);
}
if (guildModel.role_permissions != null) {
for (RolePermission rolePermission : guildModel.role_permissions) {
Member member = guild.getMember(user);
Role r = guild.getRoleById(rolePermission.getId());
if (r != null && member.getRoles().contains(r) && !member.hasPermission(Permission.MANAGE_SERVER)) {
if (!rolePermission.getCanUseArdentCommands()) {
user.openPrivateChannel().queue(privateChannel -> privateChannel.sendMessage("One of " + "your roles, **" + r.getName() + "**, cannot send Ardent commands!").queue());
return;
}
if (!message.getRawContent().toLowerCase().contains("discord.gg") && !rolePermission.getCanSendDiscordInvites()) {
message.delete().queue();
user.openPrivateChannel().queue(privateChannel -> privateChannel.sendMessage("One of " + "your roles, **" + r.getName() + "**, cannot send Discord server invite " + "links!").queue());
return;
}
if (!rolePermission.getCanSendLinks()) {
if (message.getContent().toLowerCase().contains("http://") || message.getContent().toLowerCase().contains("https://")) {
message.delete().queue();
user.openPrivateChannel().queue(privateChannel -> privateChannel.sendMessage("One" + " of " + "your roles, **" + r.getName() + "**, cannot send websiet links!").queue());
return;
}
}
}
}
}
new AsyncCommandExecutor(command.botCommand, guild, channel, event.getAuthor(), message, args, user).run();
commandsReceived++;
ranCommand[0] = true;
UserUtils.addMoney(user, 1);
} else {
command.sendTranslatedMessage("Sorry, this command is currently disabled and will be re-enabled soon.", channel, user);
ranCommand[0] = true;
}
}
});
}
if (!ranCommand[0]) {
if (!prefix.equalsIgnoreCase("/")) {
pass(event, "/");
}
}
} catch (Throwable ex) {
if (ex instanceof PermissionException) {
event.getAuthor().openPrivateChannel().queue(privateChannel -> privateChannel.sendMessage("I don't have permission to " + "send a message in this channel, please tell a server administrator").queue());
} else {
new BotException(ex);
}
}
}
use of tk.ardentbot.rethink.models.GuildModel in project Ardent by adamint.
the class Join method onJoinUser.
@SubscribeEvent
public void onJoinUser(GuildMemberJoinEvent event) throws SQLException {
userJoinEvents.add(Instant.now());
Member joined = event.getMember();
User joinedUser = joined.getUser();
Guild guild = event.getGuild();
Role role = DefaultRole.getDefaultRole(guild);
if (role != null) {
try {
guild.getController().addRolesToMember(event.getMember(), role).queue();
} catch (PermissionException ex) {
}
}
Triplet<String, String, String> automessageSettings = Automessage.getMessagesAndChannel(guild);
String channelId = automessageSettings.getA();
String welcome = automessageSettings.getB();
if (channelId != null && welcome != null) {
TextChannel channel = guild.getTextChannelById(channelId);
if (channel != null) {
try {
welcome = welcome.replace("{user}", joinedUser.getAsMention()).replace("{servername}", guild.getName()).replace("{amtusers}", String.valueOf(guild.getMembers().size()));
channel.sendMessage(welcome).queue();
} catch (PermissionException ex) {
}
}
}
GuildModel guildModel = BaseCommand.asPojo(r.table("guilds").get(guild.getId()).run(connection), GuildModel.class);
guildModel.role_permissions.forEach(rolePermission -> {
Rankable rankable = rolePermission.getRankable();
if (rankable != null && rankable.getStartsOnServerJoin()) {
rankable.getQueued().put(joinedUser.getId(), Instant.now().getEpochSecond());
}
});
}
use of tk.ardentbot.rethink.models.GuildModel in project Ardent by adamint.
the class Join method onJoin.
@SubscribeEvent
public void onJoin(GuildJoinEvent event) {
Guild guild = event.getGuild();
Shard shard = GuildUtils.getShard(guild);
Cursor<GuildModel> guilds = r.db("data").table("guilds").filter(r.hashMap("guild_id", guild.getId())).run(connection);
if (!guilds.hasNext()) {
TextChannel channel = guild.getPublicChannel();
channel.sendMessage(welcomeText).queue();
r.db("data").table("guilds").insert(r.hashMap("guild_id", guild.getId()).with("language", "english").with("prefix", "/").with("role_permissions", new ArrayList<>())).run(connection);
String prefix = "/";
shard.botPrefixData.set(guild, prefix);
botJoinEvents.add(Instant.now());
Status.commandsByGuild.put(guild.getId(), 0);
shard.musicManagers.put(Long.parseLong(guild.getId()), new GuildMusicManager(shard.playerManager, null));
Ardent.cleverbots.put(guild.getId(), shard.cleverBot.createSession());
Ardent.sentAnnouncement.put(guild.getId(), false);
Status.commandsByGuild.put(guild.getId(), 0);
shard.executorService.schedule(() -> {
guild.getOwner().getUser().openPrivateChannel().queue(privateChannel -> privateChannel.sendMessage("Thanks for adding Ardent. If you have questions or something isn't working, join over 300 people on our" + " support server @ https://discordapp.com/invite/rfGSxNA").queue());
}, 3, TimeUnit.SECONDS);
}
}
Aggregations