net.dv8tion.jda.api.requests.restaction.MessageAction Java Examples

The following examples show how to use net.dv8tion.jda.api.requests.restaction.MessageAction. 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: MessageBuilder.java    From JDA with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link MessageAction MessageAction}
 * with the current settings without building a {@link net.dv8tion.jda.api.entities.Message Message} instance first.
 *
 * @param  channel
 *         The not-null target {@link net.dv8tion.jda.api.entities.MessageChannel MessageChannel}
 *
 * @throws java.lang.IllegalArgumentException
 *         If the provided channel is {@code null}
 * @throws net.dv8tion.jda.api.exceptions.PermissionException
 *         If the currently logged in account does not have permission to send or read messages in this channel.
 * @throws java.lang.UnsupportedOperationException
 *         If this is a PrivateChannel and both users (sender and receiver) are bots
 *
 * @return {@link MessageAction MessageAction}
 */
@Nonnull
@CheckReturnValue
public MessageAction sendTo(@Nonnull MessageChannel channel)
{
    Checks.notNull(channel, "Target Channel");
    switch (channel.getType())
    {
        case TEXT:
            final TextChannel text = (TextChannel) channel;
            final Member self = text.getGuild().getSelfMember();
            if (!self.hasPermission(text, Permission.MESSAGE_READ))
                throw new InsufficientPermissionException(text, Permission.MESSAGE_READ);
            if (!self.hasPermission(text, Permission.MESSAGE_WRITE))
                throw new InsufficientPermissionException(text, Permission.MESSAGE_WRITE);
            break;
        case PRIVATE:
            final PrivateChannel priv = (PrivateChannel) channel;
            if (priv.getUser().isBot() && channel.getJDA().getAccountType() == AccountType.BOT)
                throw new UnsupportedOperationException("Cannot send a private message between bots.");
    }
    final Route.CompiledRoute route = Route.Messages.SEND_MESSAGE.compile(channel.getId());
    final MessageActionImpl action = new MessageActionImpl(channel.getJDA(), route, channel, builder);
    return action.tts(isTTS).embed(embed).nonce(nonce);
}
 
Example #2
Source File: TextChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendFile(@Nonnull InputStream data, @Nonnull String fileName, @Nonnull AttachmentOption... options)
{
    checkPermission(Permission.MESSAGE_READ);
    checkPermission(Permission.MESSAGE_WRITE);
    checkPermission(Permission.MESSAGE_ATTACH_FILES);

    //Call MessageChannel's default method
    return TextChannel.super.sendFile(data, fileName, options);
}
 
Example #3
Source File: PrivateChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendMessage(@Nonnull CharSequence text)
{
    checkBot();
    return PrivateChannel.super.sendMessage(text);
}
 
Example #4
Source File: PrivateChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendMessage(@Nonnull MessageEmbed embed)
{
    checkBot();
    return PrivateChannel.super.sendMessage(embed);
}
 
Example #5
Source File: PrivateChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendMessage(@Nonnull Message msg)
{
    checkBot();
    return PrivateChannel.super.sendMessage(msg);
}
 
Example #6
Source File: PrivateChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendFile(@Nonnull File file, @Nonnull String fileName, @Nonnull AttachmentOption... options)
{
    checkBot();
    final long maxSize = getJDA().getSelfUser().getAllowedFileSize();
    Checks.check(file == null || file.length() <= maxSize,
                "File may not exceed the maximum file length of %d bytes!", maxSize);
    return PrivateChannel.super.sendFile(file, fileName, options);
}
 
Example #7
Source File: PrivateChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendFile(@Nonnull byte[] data, @Nonnull String fileName, @Nonnull AttachmentOption... options)
{
    checkBot();
    final long maxSize = getJDA().getSelfUser().getAllowedFileSize();
    Checks.check(data == null || data.length <= maxSize, "File is too big! Max file-size is %d bytes", maxSize);
    return PrivateChannel.super.sendFile(data, fileName, options);
}
 
Example #8
Source File: ReceivedMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction editMessageFormat(@Nonnull String format, @Nonnull Object... args)
{
    Checks.notBlank(format, "Format String");
    return editMessage(new MessageBuilder().appendFormat(format, args).build());
}
 
Example #9
Source File: ReceivedMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction editMessage(@Nonnull Message newContent)
{
    if (!getJDA().getSelfUser().equals(getAuthor()))
        throw new IllegalStateException("Attempted to update message that was not sent by this account. You cannot modify other User's messages!");

    return getChannel().editMessageById(getIdLong(), newContent);
}
 
Example #10
Source File: TextChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendMessage(@Nonnull CharSequence text)
{
    checkPermission(Permission.MESSAGE_READ);
    checkPermission(Permission.MESSAGE_WRITE);
    return TextChannel.super.sendMessage(text);
}
 
Example #11
Source File: TextChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendMessage(@Nonnull MessageEmbed embed)
{
    checkPermission(Permission.MESSAGE_READ);
    checkPermission(Permission.MESSAGE_WRITE);
    // this is checked because you cannot send an empty message
    checkPermission(Permission.MESSAGE_EMBED_LINKS);
    return TextChannel.super.sendMessage(embed);
}
 
Example #12
Source File: TextChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendMessage(@Nonnull Message msg)
{
    Checks.notNull(msg, "Message");

    checkPermission(Permission.MESSAGE_READ);
    checkPermission(Permission.MESSAGE_WRITE);
    if (msg.getContentRaw().isEmpty() && !msg.getEmbeds().isEmpty())
        checkPermission(Permission.MESSAGE_EMBED_LINKS);

    //Call MessageChannel's default
    return TextChannel.super.sendMessage(msg);
}
 
Example #13
Source File: TextChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendFile(@Nonnull File file, @Nonnull String fileName, @Nonnull AttachmentOption... options)
{
    checkPermission(Permission.MESSAGE_READ);
    checkPermission(Permission.MESSAGE_WRITE);
    checkPermission(Permission.MESSAGE_ATTACH_FILES);

    final long maxSize = getGuild().getMaxFileSize();
    Checks.check(file == null || file.length() <= maxSize,
                "File may not exceed the maximum file length of %d bytes!", maxSize);

    //Call MessageChannel's default method
    return TextChannel.super.sendFile(file, fileName, options);
}
 
Example #14
Source File: PrivateChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendFile(@Nonnull InputStream data, @Nonnull String fileName, @Nonnull AttachmentOption... options)
{
    checkBot();
    return PrivateChannel.super.sendFile(data, fileName, options);
}
 
Example #15
Source File: TextChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction sendFile(@Nonnull byte[] data, @Nonnull String fileName, @Nonnull AttachmentOption... options)
{
    checkPermission(Permission.MESSAGE_READ);
    checkPermission(Permission.MESSAGE_WRITE);
    checkPermission(Permission.MESSAGE_ATTACH_FILES);

    final long maxSize = getGuild().getMaxFileSize();
    Checks.check(data == null || data.length <= maxSize, "File is too big! Max file-size is %d bytes", maxSize);

    //Call MessageChannel's default method
    return TextChannel.super.sendFile(data, fileName, options);
}
 
Example #16
Source File: TextChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction editMessageById(@Nonnull String messageId, @Nonnull CharSequence newContent)
{
    checkPermission(Permission.MESSAGE_READ);
    checkPermission(Permission.MESSAGE_WRITE);
    return TextChannel.super.editMessageById(messageId, newContent);
}
 
Example #17
Source File: TextChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction editMessageById(@Nonnull String messageId, @Nonnull MessageEmbed newEmbed)
{
    checkPermission(Permission.MESSAGE_READ);
    checkPermission(Permission.MESSAGE_WRITE);
    checkPermission(Permission.MESSAGE_EMBED_LINKS);
    return TextChannel.super.editMessageById(messageId, newEmbed);
}
 
Example #18
Source File: TextChannelImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction editMessageById(@Nonnull String id, @Nonnull Message newContent)
{
    Checks.notNull(newContent, "Message");

    //checkVerification(); no verification needed to edit a message
    checkPermission(Permission.MESSAGE_READ);
    checkPermission(Permission.MESSAGE_WRITE);
    if (newContent.getContentRaw().isEmpty() && !newContent.getEmbeds().isEmpty())
        checkPermission(Permission.MESSAGE_EMBED_LINKS);

    //Call MessageChannel's default
    return TextChannel.super.editMessageById(id, newContent);
}
 
Example #19
Source File: AbstractMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction editMessage(@Nonnull CharSequence newContent)
{
    unsupported();
    return null;
}
 
Example #20
Source File: AbstractMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction editMessage(@Nonnull MessageEmbed newContent)
{
    unsupported();
    return null;
}
 
Example #21
Source File: AbstractMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction editMessageFormat(@Nonnull String format, @Nonnull Object... args)
{
    unsupported();
    return null;
}
 
Example #22
Source File: AbstractMessage.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction editMessage(@Nonnull Message newContent)
{
    unsupported();
    return null;
}
 
Example #23
Source File: MessageActionImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction mention(@Nonnull IMentionable... mentions)
{
    Checks.noneNull(mentions, "Mentionables");
    for (IMentionable mentionable : mentions)
    {
        if (mentionable instanceof User || mentionable instanceof Member)
            mentionableUsers.add(mentionable.getId());
        else if (mentionable instanceof Role)
            mentionableRoles.add(mentionable.getId());
    }
    return this;
}
 
Example #24
Source File: MessageActionImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction mentionUsers(@Nonnull String... userIds)
{
    Checks.noneNull(userIds, "User Id");
    Collections.addAll(mentionableUsers, userIds);
    return this;
}
 
Example #25
Source File: MessageActionImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MessageAction mentionRoles(@Nonnull String... roleIds)
{
    Checks.noneNull(roleIds, "Role Id");
    Collections.addAll(mentionableRoles, roleIds);
    return this;
}
 
Example #26
Source File: MessageBuilder.java    From JDA with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the provided {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} to the whitelist.
 *
 * @param  types
 *         The mention types to allow
 *
 * @throws IllegalArgumentException
 *         If null is provided
 *
 * @return The MessageBuilder instance. Useful for chaining.
 */
@Nonnull
public MessageBuilder allowMentions(@Nonnull Message.MentionType... types)
{
    Checks.noneNull(types, "MentionTypes");
    if (types.length > 0)
    {
        if (allowedMentions == null)
            allowedMentions = MessageAction.getDefaultMentions();
        Collections.addAll(allowedMentions, types);
    }
    return this;
}
 
Example #27
Source File: MessageBuilder.java    From JDA with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the provided {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} from the whitelist.
 *
 * @param  types
 *         The mention types to deny
 *
 * @throws IllegalArgumentException
 *         If null is provided
 *
 * @return The MessageBuilder instance. Useful for chaining.
 */
@Nonnull
public MessageBuilder denyMentions(@Nonnull Message.MentionType... types)
{
    Checks.noneNull(types, "MentionTypes");
    if (types.length > 0)
    {
        if (allowedMentions == null)
            allowedMentions = MessageAction.getDefaultMentions();
        for (Message.MentionType type : types)
            allowedMentions.remove(type);
    }
    return this;
}
 
Example #28
Source File: MessageChannel.java    From JDA with Apache License 2.0 4 votes vote down vote up
/**
 * Uploads a file to the Discord servers and sends it to this {@link net.dv8tion.jda.api.entities.MessageChannel MessageChannel}.
 * Sends the provided {@link net.dv8tion.jda.api.entities.Message Message} with the uploaded file.
 * <br>If you want to send a Message with the uploaded file, you can add the file to the {@link net.dv8tion.jda.api.requests.restaction.MessageAction}
 * returned by {@link #sendMessage(Message)}.
 *
 * <p>The {@code fileName} parameter is used to inform Discord about what the file should be called. This is 2 fold:
 * <ol>
 *     <li>The file name provided is the name that is found in {@link net.dv8tion.jda.api.entities.Message.Attachment#getFileName()}
 *          after upload and it is the name that will show up in the client when the upload is displayed.
 *     <br>Note: The fileName does not show up on the Desktop client for images. It does on mobile however.</li>
 *     <li>The extension of the provided fileName also determines how Discord will treat the file. Discord currently only
 *         has special handling for image file types, but the fileName's extension must indicate that it is an image file.
 *         This means it has to end in something like .png, .jpg, .jpeg, .gif, etc. As a note, you can also not provide
 *         a full name for the file and instead ONLY provide the extension like "png" or "gif" and Discord will generate
 *         a name for the upload and append the fileName as the extension.</li>
 * </ol>
 *
 * <p><b>Uploading images with Embeds</b>
 * <br>When uploading an <u>image</u> you can reference said image using the specified filename as URI {@code attachment://filename.ext}.
 *
 * <p><u>Example</u>
 * <pre><code>
 * MessageChannel channel; // = reference of a MessageChannel
 * EmbedBuilder embed = new EmbedBuilder();
 * File file = new File("cat_01.gif");
 * embed.setImage("attachment://cat.gif") // we specify this in sendFile as "cat.gif"
 *      .setDescription("This is a cute cat :3");
 * channel.sendFile(file, "cat.gif").embed(embed.build()).queue();
 * </code></pre>
 *
 * <p>The following {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} are possible:
 * <ul>
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
 *     <br>The send request was attempted after the account lost access to the {@link net.dv8tion.jda.api.entities.Guild Guild}
 *         typically due to being kicked or removed.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
 *     <br>The send request was attempted after the account lost {@link net.dv8tion.jda.api.Permission#MESSAGE_WRITE Permission.MESSAGE_WRITE} or
 *         {@link net.dv8tion.jda.api.Permission#MESSAGE_ATTACH_FILES Permission.MESSAGE_ATTACH_FILES}
 *         in the {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#CANNOT_SEND_TO_USER CANNOT_SEND_TO_USER}
 *     <br>If this is a {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel} and the currently logged in account
 *         does not share any Guilds with the recipient User</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_CHANNEL UNKNOWN_CHANNEL}
 *     <br>The send request was attempted after the channel was deleted.</li>
 * </ul>
 *
 * @param  file
 *         The file to upload to the {@link net.dv8tion.jda.api.entities.MessageChannel MessageChannel}.
 * @param  fileName
 *         The name that should be sent to discord
 * @param  options
 *         Possible options to apply to this attachment, such as marking it as spoiler image
 *
 * @throws java.lang.IllegalArgumentException
 *         <ul>
 *             <li>Provided {@code file} is null.</li>
 *             <li>Provided {@code file} does not exist.</li>
 *             <li>Provided {@code file} is unreadable.</li>
 *             <li>Provided {@code file} is greater than 8 MiB on a normal or 50 MiB on a nitro account.</li>
 *         </ul>
 * @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
 *         If this is a {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} and the logged in account does not have
 *         <ul>
 *             <li>{@link net.dv8tion.jda.api.Permission#MESSAGE_READ Permission.MESSAGE_READ}</li>
 *             <li>{@link net.dv8tion.jda.api.Permission#MESSAGE_WRITE Permission.MESSAGE_WRITE}</li>
 *             <li>{@link net.dv8tion.jda.api.Permission#MESSAGE_ATTACH_FILES Permission.MESSAGE_ATTACH_FILES}</li>
 *         </ul>
 * @throws java.lang.UnsupportedOperationException
 *         If this is a {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel}
 *         and both the currently logged in account and the target user are bots.
 *
 * @return {@link MessageAction MessageAction}
 *         <br>Providing the {@link net.dv8tion.jda.api.entities.Message Message} created from this upload.
 */
@Nonnull
@CheckReturnValue
default MessageAction sendFile(@Nonnull File file, @Nonnull String fileName, @Nonnull AttachmentOption... options)
{
    Checks.notNull(file, "file");
    Checks.check(file.exists() && file.canRead(),
                "Provided file doesn't exist or cannot be read!");
    Checks.notNull(fileName, "fileName");

    try
    {
        return sendFile(new FileInputStream(file), fileName, options);
    }
    catch (FileNotFoundException ex)
    {
        throw new IllegalArgumentException(ex);
    }
}
 
Example #29
Source File: MessageActionImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public MessageAction deadline(long timestamp)
{
    return (MessageAction) super.deadline(timestamp);
}
 
Example #30
Source File: MessageActionImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public MessageAction setCheck(BooleanSupplier checks)
{
    return (MessageAction) super.setCheck(checks);
}