Java Code Examples for discord4j.core.event.domain.message.MessageCreateEvent#getMessage()

The following examples show how to use discord4j.core.event.domain.message.MessageCreateEvent#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private void onMessageReceived(MessageCreateEvent event) {
  Message message = event.getMessage();

  message.getContent().ifPresent(it -> {
    MessageChannel channel = message.getChannel().block();

    if (channel instanceof TextChannel) {
      String[] command = it.split(" ", 2);

      if ("~play".equals(command[0]) && command.length == 2) {
        loadAndPlay((TextChannel) channel, command[1]);
      } else if ("~skip".equals(command[0])) {
        skipTrack((TextChannel) channel);
      }
    }
  });
}
 
Example 2
Source File: ExtraBotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    return Mono.justOrEmpty(message.getContent())
            .filter(content -> content.startsWith("!addrole "))
            .filterWhen(content -> message.getGuild().hasElement())
            .flatMap(content -> {
                // !addrole userId roleName
                String[] tokens = content.split(" ", 3);
                if (tokens.length > 2) {
                    String user = tokens[1];
                    String roleName = tokens[2];
                    Mono<Member> member = message.getGuild()
                            .flatMap(g -> g.getMemberById(Snowflake.of(user)));
                    Mono<Role> role = message.getGuild()
                            .flatMapMany(Guild::getRoles)
                            .filter(r -> r.getName().equalsIgnoreCase(roleName))
                            .next();
                    return member.zipWith(role)
                            .flatMap(t2 -> t2.getT1().addRole(t2.getT2().getId()));
                }
                return Mono.empty();
            })
            .then();
}
 
Example 3
Source File: ExtraBotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    if (message.getContent().startsWith("!user ")) {
        return Mono.justOrEmpty(message.getContent())
                .map(content -> content.split(" ", 2))
                .flatMap(tokens -> message.getClient().getUserById(Snowflake.of(tokens[1])))
                .flatMap(user -> message.getChannel()
                        .flatMap(channel -> channel.createMessage(msg ->
                                msg.setEmbed(embed -> embed
                                        .addField("Name", user.getUsername(), false)
                                        .addField("Avatar URL", user.getAvatarUrl(), false)
                                        .setImage(user.getAvatarUrl()))
                        )))
                .switchIfEmpty(Mono.just("Not found")
                        .flatMap(reason -> message.getChannel()
                                .flatMap(channel -> channel.createMessage(reason)))
                )
                .then();
    }
    return Mono.empty();
}
 
Example 4
Source File: ExtraBotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    if (message.getContent().startsWith("!log ")) {
        return Mono.justOrEmpty(message.getContent())
                .map(content -> content.split(" ", 3))
                .doOnNext(tokens -> {
                    String level = tokens[1];
                    String name = tokens[2];
                    Level logLevel = Level.valueOf(level);
                    context.getLoggerList().stream()
                            .filter(logger -> logger.getName().startsWith(name))
                            .forEach(logger -> {
                                log.info("Changing {} to {}", logger, logLevel);
                                logger.setLevel(logLevel);
                            });
                })
                .then();
    }
    return Mono.empty();
}
 
Example 5
Source File: ExtraBotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    if (message.getContent().startsWith("!react")) {
        String rawCount = message.getContent().substring("!react".length());
        int count = 1;
        if (!rawCount.isEmpty()) {
            try {
                count = Math.max(1, Math.min(20, Integer.parseInt(rawCount.trim())));
            } catch (NumberFormatException e) {
                throw Exceptions.propagate(e);
            }
        }
        return Flux.fromIterable(fetch(count))
                .flatMap(emoji -> message.addReaction(ReactionEmoji.unicode(emoji)))
                .then();
    }
    return Mono.empty();
}
 
Example 6
Source File: ExtraBotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    if (message.getContent().equals("!avatar")) {
        for (Attachment attachment : message.getAttachments()) {
            // This code is very optimistic as it does not check for status codes or file types
            return HttpClient.create()
                    .get()
                    .uri(attachment.getUrl())
                    .responseSingle((res, mono) -> mono.asByteArray())
                    .flatMap(input -> message.getClient()
                            .edit(spec -> spec.setAvatar(Image.ofRaw(input, Image.Format.PNG))))
                    .then();
        }
    }
    return Mono.empty();
}
 
Example 7
Source File: BotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    return Mono.justOrEmpty(message.getContent())
            .filter(content -> content.equals("!status"))
            .flatMap(source -> message.getChannel()
                    .publishOn(Schedulers.boundedElastic())
                    .flatMap(channel -> channel.createEmbed(spec -> {
                        spec.setThumbnail(event.getClient().getSelf()
                                .blockOptional()
                                .orElseThrow(RuntimeException::new)
                                .getAvatarUrl());
                        spec.addField("Servers", event.getClient().getGuilds().count()
                                .blockOptional()
                                .orElse(-1L)
                                .toString(), false);
                    })))
            .then();
}
 
Example 8
Source File: BotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    return Mono.justOrEmpty(message.getContent())
            .filter(content -> content.startsWith("!status "))
            .map(content -> {
                String status = content.substring("!status ".length());
                if (status.equalsIgnoreCase("online")) {
                    return Presence.online();
                } else if (status.equalsIgnoreCase("dnd")) {
                    return Presence.doNotDisturb();
                } else if (status.equalsIgnoreCase("idle")) {
                    return Presence.idle();
                } else if (status.equalsIgnoreCase("invisible")) {
                    return Presence.invisible();
                } else {
                    throw new IllegalArgumentException("Invalid argument");
                }
            })
            .flatMap(presence -> event.getClient().updatePresence(presence))
            .then();
}
 
Example 9
Source File: BotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    String content = message.getContent();
    if (content.startsWith("!members ")) {
        String guildId = content.substring("!members ".length());
        return event.getClient().requestMembers(Snowflake.of(guildId))
                .doOnNext(member -> log.info("{}", member))
                .then(message.getRestChannel().createMessage(
                        MessageCreateRequest.builder()
                                .content("<@" + message.getUserData().id() + "> Done!")
                                .build()))
                .then();
    }
    return Mono.empty();
}
 
Example 10
Source File: ExtraBotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    if (message.getContent().startsWith("!rr ")) {
        String[] tokens = message.getContent().split(" ");
        return event.getClient().getMessageById(Snowflake.of(tokens[1]), Snowflake.of(tokens[2]))
                .flatMap(m -> m.removeReactions(ReactionEmoji.unicode("✅")));
    }
    return Mono.empty();
}
 
Example 11
Source File: BotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    String content = message.getContent();
    if (content.startsWith("!echo ")) {
        return message.getRestChannel().createMessage(
                MessageCreateRequest.builder()
                        .content("<@" + message.getUserData().id() + "> " + content.substring("!echo ".length()))
                        .build())
                .flatMap(data -> event.getClient().rest().restMessage(data)
                        .createReaction("✅"))
                .then();
    }
    return Mono.empty();
}
 
Example 12
Source File: BotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    return Mono.justOrEmpty(message.getContent())
            .filter(content -> content.equals("!exit"))
            .flatMap(presence -> event.getClient().logout())
            .then();
}
 
Example 13
Source File: BotSupport.java    From Discord4J with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Mono<Void> onMessageCreate(MessageCreateEvent event) {
    Message message = event.getMessage();
    String content = message.getContent();
    if (content.startsWith("!getMembers ")) {
        String guildId = content.substring("!getMembers ".length());
        return event.getClient().getGuildById(Snowflake.of(guildId))
          .flatMapMany(Guild::getMembers)
                .doOnNext(member -> log.info("{}", member.getTag()))
                .then();
    }
    return Mono.empty();
}