use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.
the class CurrencyCmds method market.
@Subscribe
public void market(CommandRegistry cr) {
final RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 8);
TreeCommand marketCommand = (TreeCommand) cr.register("market", new TreeCommand(Category.CURRENCY) {
@Override
public Command defaultTrigger(GuildMessageReceivedEvent event, String mainCommand, String commandName) {
return new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
EmbedBuilder embed = baseEmbed(event, "Mantaro's Market").setThumbnail("https://png.icons8.com/metro/540/shopping-cart.png");
List<MessageEmbed.Field> fields = new LinkedList<>();
Stream.of(Items.ALL).forEach(item -> {
if (!item.isHidden()) {
String buyValue = item.isBuyable() ? String.format("$%d", item.getValue()) : "N/A";
String sellValue = item.isSellable() ? String.format("$%d", (int) Math.floor(item.getValue() * 0.9)) : "N/A";
fields.add(new MessageEmbed.Field(String.format("%s %s", item.getEmoji(), item.getName()), EmoteReference.BUY + buyValue + " " + EmoteReference.SELL + sellValue, true));
}
});
List<List<MessageEmbed.Field>> splitFields = DiscordUtils.divideFields(8, fields);
boolean hasReactionPerms = event.getGuild().getSelfMember().hasPermission(event.getChannel(), Permission.MESSAGE_ADD_REACTION);
if (hasReactionPerms) {
DiscordUtils.list(event, 120, false, embed, splitFields);
} else {
DiscordUtils.listText(event, 120, false, embed, splitFields);
}
}
};
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Mantaro's market").setDescription("**List current items for buying and selling.**").addField("Buying and selling", "To buy do ~>market buy <item emoji>. It will subtract the value from your money" + " and give you the item.\n" + "To sell do `~>market sell all` to sell all your items or `~>market sell <item emoji>` to sell the specified item. " + "**You'll get the sell value of the item on coins to spend.**\n" + "You can check the value of a single item using `~>market price <item emoji>`\n" + "You can send an item to the trash using `~>market dump <amount> <item emoji>`\n" + "Use `~>inventory -calculate` to check how much is your inventory worth.", false).addField("To know", "If you don't have enough money you cannot buy the items.\n" + "Note: Don't use the item id, it's just for aesthetic reasons, the internal IDs are different than the ones shown here!", false).addField("Information", "To buy and sell multiple items you need to do `~>market <buy/sell> <amount> <item>`", false).build();
}
});
marketCommand.setPredicate((event) -> {
if (!handleDefaultRatelimit(rateLimiter, event.getAuthor(), event))
return false;
Player player = MantaroData.db().getPlayer(event.getMember());
if (player.isLocked()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot access the market now.").queue();
return false;
}
return true;
});
marketCommand.addSubCommand("dump", new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
String[] args = content.split(" ");
String itemName = content;
int itemNumber = 1;
boolean isMassive = !itemName.isEmpty() && itemName.split(" ")[0].matches("^[0-9]*$");
if (isMassive) {
try {
itemNumber = Math.abs(Integer.valueOf(itemName.split(" ")[0]));
itemName = itemName.replace(args[0], "").trim();
} catch (NumberFormatException e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Not a valid number of items to dump.").queue();
return;
} catch (Exception e) {
onHelp(event);
return;
}
}
Item item = Items.fromAny(itemName).orElse(null);
if (item == null) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Cannot check the dump a non-existent item!").queue();
return;
}
Player player = MantaroData.db().getPlayer(event.getAuthor());
if (!player.getInventory().containsItem(item)) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Cannot dump an item you don't have!").queue();
return;
}
if (player.getInventory().getAmount(item) < itemNumber) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot dump more items than what you have.").queue();
return;
}
player.getInventory().process(new ItemStack(item, -itemNumber));
player.saveAsync();
event.getChannel().sendMessage(String.format("%sSent %dx **%s %s** to the trash!", EmoteReference.CORRECT, itemNumber, item.getEmoji(), item.getName())).queue();
}
}).createSubCommandAlias("dump", "trash");
marketCommand.addSubCommand("price", new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
String[] args = content.split(" ");
String itemName = content.replace(args[0] + " ", "");
Item item = Items.fromAny(itemName).orElse(null);
if (item == null) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Cannot check the price of a non-existent item!").queue();
return;
}
if (!item.isBuyable() && !item.isSellable()) {
event.getChannel().sendMessage(EmoteReference.THINKING + "This item is not available neither for sell or buy (could be an exclusive collectible)").queue();
return;
}
if (!item.isBuyable()) {
event.getChannel().sendMessage(EmoteReference.EYES + "This is a collectible item. (Sell value: " + ((int) (item.getValue() * 0.9)) + " credits)").queue();
return;
}
event.getChannel().sendMessage(String.format("%sThe market value of %s**%s** is %s credits to buy it and you can get %s credits if you sell it.", EmoteReference.MARKET, item.getEmoji(), item.getName(), item.getValue(), (int) (item.getValue() * 0.9))).queue();
}
});
marketCommand.addSubCommand("sell", new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
Player player = MantaroData.db().getPlayer(event.getMember());
String[] args = content.split(" ");
String itemName = content;
int itemNumber = 1;
boolean isMassive = !itemName.isEmpty() && itemName.split(" ")[0].matches("^[0-9]*$");
if (isMassive) {
try {
itemNumber = Math.abs(Integer.valueOf(itemName.split(" ")[0]));
itemName = itemName.replace(args[0], "").trim();
} catch (NumberFormatException e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Not a valid number of items to buy.").queue();
return;
} catch (Exception e) {
onHelp(event);
return;
}
}
try {
if (args[0].equals("all")) {
long all = player.getInventory().asList().stream().filter(item -> item.getItem().isSellable()).mapToLong(value -> (long) (value.getItem().getValue() * value.getAmount() * 0.9d)).sum();
player.getInventory().clearOnlySellables();
player.addMoney(all);
event.getChannel().sendMessage(String.format("%sYou sold all your inventory items and gained %d credits!", EmoteReference.MONEY, all)).queue();
player.saveAsync();
return;
}
Item toSell = Items.fromAny(itemName).orElse(null);
if (toSell == null) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot sell a non-existant item.").queue();
return;
}
if (!toSell.isSellable()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot sell an item that cannot be sold.").queue();
return;
}
if (player.getInventory().getAmount(toSell) < 1) {
event.getChannel().sendMessage(EmoteReference.STOP + "You cannot sell an item you don't have.").queue();
return;
}
if (player.getInventory().getAmount(toSell) < itemNumber) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot sell more items than what you have.").queue();
return;
}
int many = itemNumber * -1;
long amount = Math.round((toSell.getValue() * 0.9)) * Math.abs(many);
player.getInventory().process(new ItemStack(toSell, many));
player.addMoney(amount);
player.getData().setMarketUsed(player.getData().getMarketUsed() + 1);
event.getChannel().sendMessage(String.format("%sYou sold %d **%s** and gained %d credits!", EmoteReference.CORRECT, Math.abs(many), toSell.getName(), amount)).queue();
player.saveAsync();
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Invalid syntax.").queue();
}
}
});
marketCommand.addSubCommand("buy", new SubCommand() {
@Override
protected void call(GuildMessageReceivedEvent event, String content) {
Player player = MantaroData.db().getPlayer(event.getMember());
String[] args = content.split(" ");
String itemName = content;
int itemNumber = 1;
boolean isMassive = !itemName.isEmpty() && itemName.split(" ")[0].matches("^[0-9]*$");
if (isMassive) {
try {
itemNumber = Math.abs(Integer.valueOf(itemName.split(" ")[0]));
itemName = itemName.replace(args[0], "").trim();
} catch (Exception e) {
if (e instanceof NumberFormatException) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Not a valid number of items to buy.").queue();
} else {
onHelp(event);
return;
}
}
}
Item itemToBuy = Items.fromAnyNoId(itemName).orElse(null);
if (itemToBuy == null) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot buy an unexistant item.").queue();
return;
}
try {
if (!itemToBuy.isBuyable()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot buy an item that cannot be bought.").queue();
return;
}
ItemStack stack = player.getInventory().getStackOf(itemToBuy);
if (stack != null && !stack.canJoin(new ItemStack(itemToBuy, itemNumber))) {
// assume overflow
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot buy more of that object!").queue();
return;
}
if (player.removeMoney(itemToBuy.getValue() * itemNumber)) {
player.getInventory().process(new ItemStack(itemToBuy, itemNumber));
player.getData().addBadgeIfAbsent(Badge.BUYER);
player.getData().setMarketUsed(player.getData().getMarketUsed() + 1);
player.saveAsync();
event.getChannel().sendMessage(String.format("%sBought %d %s for %d credits successfully. You now have %d credits.", EmoteReference.OK, itemNumber, itemToBuy.getEmoji(), itemToBuy.getValue() * itemNumber, player.getMoney())).queue();
} else {
event.getChannel().sendMessage(EmoteReference.STOP + "You don't have enough money to buy this item.").queue();
}
} catch (Exception e) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Invalid syntax.").queue();
}
}
});
}
use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.
the class DebugCmds method ping.
@Subscribe
public void ping(CommandRegistry cr) {
final RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 5, true);
final Random r = new Random();
final String[] pingQuotes = { "W-Was I fast enough?", "What are you doing?", "W-What are you looking at?!", "Huh.", "Did I do well?", "What do you think?", "Does this happen often?", "Am I performing p-properly?", "<3", "*pats*", "Pong.", "Pang.", "Pung.", "Peng.", "Ping-pong? Yay!", "U-Uh... h-hi" };
cr.register("ping", new SimpleCommand(Category.INFO) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
if (!handleDefaultRatelimit(rateLimiter, event.getAuthor(), event))
return;
long start = System.currentTimeMillis();
event.getChannel().sendTyping().queue(v -> {
long ping = System.currentTimeMillis() - start;
event.getChannel().sendMessage(EmoteReference.MEGA + "*" + pingQuotes[r.nextInt(pingQuotes.length)] + "* - My ping: " + ping + " ms (" + ratePing(ping) + ") `Websocket:" + event.getJDA().getPing() + "ms`").queue();
TextChannelGround.of(event).dropItemWithChance(5, 5);
});
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Ping Command").setDescription("**Plays Ping-Pong with Discord and prints out the result.**").build();
}
});
}
use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.
the class FunCmds method ratewaifu.
@Subscribe
public void ratewaifu(CommandRegistry cr) {
cr.register("ratewaifu", new SimpleCommand(Category.FUN) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
if (args.length == 0) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Give me a waifu to rate!").queue();
return;
}
int waifuRate = content.replaceAll("\\s+", " ").replaceAll("<@!?(\\d+)>", "<@$1>").chars().sum() % 101;
if (content.equalsIgnoreCase("mantaro"))
waifuRate = 100;
new MessageBuilder().setContent(String.format("%sI rate %s with a **%d/100**", EmoteReference.THINKING, content, waifuRate)).stripMentions(event.getGuild(), Message.MentionType.EVERYONE, Message.MentionType.HERE).sendTo(event.getChannel()).queue();
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Rate your waifu").setDescription("**Just rates your waifu from zero to 100. Results may vary.**").build();
}
});
cr.registerAlias("ratewaifu", "rw");
}
use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.
the class FunCmds method coinflip.
@Subscribe
public void coinflip(CommandRegistry cr) {
cr.register("coinflip", new SimpleCommand(Category.FUN) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
int times;
if (args.length == 0 || content.length() == 0)
times = 1;
else {
try {
times = Integer.parseInt(args[0]);
if (times > 1000) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Hold in there! The limit is 1,000 coin flips").queue();
return;
}
} catch (NumberFormatException nfe) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You need to specify an Integer for the amount of repetitions").queue();
return;
}
}
final int[] heads = { 0 };
final int[] tails = { 0 };
doTimes(times, () -> {
if (r.nextBoolean())
heads[0]++;
else
tails[0]++;
});
String flips = times == 1 ? "time" : "times";
event.getChannel().sendMessage(String.format("%s Your result from **%d** %s yielded **%d** heads and **%d** tails", EmoteReference.PENNY, times, flips, heads[0], tails[0])).queue();
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Coinflip command").setDescription("**Flips a coin with a defined number of repetitions**").addField("Usage", "`~>coinflip <number of times>` - **Flips a coin x number of times**", false).build();
}
});
}
use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.
the class GameCmds method startMultipleGames.
private void startMultipleGames(LinkedList<Game> games, GuildMessageReceivedEvent event) {
if (checkRunning(event))
return;
List<String> players = new ArrayList<>();
players.add(event.getAuthor().getId());
if (!event.getMessage().getMentionedRoles().isEmpty()) {
StringBuilder b = new StringBuilder();
event.getMessage().getMentionedRoles().forEach(role -> event.getGuild().getMembersWithRoles(role).forEach(user -> {
if (!user.getUser().getId().equals(event.getJDA().getSelfUser().getId()))
players.add(user.getUser().getId());
b.append(user.getEffectiveName()).append(" ");
}));
event.getChannel().sendMessage(EmoteReference.MEGA + "Started a MP lobby with all users with the specfied role: " + b.toString()).queue();
}
if (!event.getMessage().getMentionedUsers().isEmpty()) {
StringBuilder builder = new StringBuilder();
event.getMessage().getMentionedUsers().forEach(user -> {
if (!user.getId().equals(event.getJDA().getSelfUser().getId()) && !user.isBot())
players.add(user.getId());
builder.append(user.getName()).append(" ");
});
if (players.size() > 1) {
event.getChannel().sendMessage(EmoteReference.MEGA + "Started a MP lobby with users: " + builder.toString()).queue();
}
}
event.getChannel().sendMessage(EmoteReference.CORRECT + "Started a new lobby! **Games: " + games.stream().map(Game::name).collect(Collectors.joining(", ")) + "**\n" + "You can type `endlobby` to end all games and finish the lobby.").queue();
GameLobby lobby = new GameLobby(event, players, games);
lobby.startFirstGame();
}
Aggregations