Search in sources :

Example 1 with MessageBuilder

use of sx.blah.discord.util.MessageBuilder in project Discord4J by Discord4J.

the class ParrotBot method handle.

/**
 * Called when the client receives a message.
 */
@Override
public void handle(MessageReceivedEvent event) {
    // Gets the message from the event object NOTE: This is not the content of the message, but the object itself
    IMessage message = event.getMessage();
    // Gets the channel in which this message was sent.
    IChannel channel = message.getChannel();
    try {
        // Builds (sends) and new message in the channel that the original message was sent with the content of the original message.
        new MessageBuilder(this.client).withChannel(channel).withContent(message.getContent()).build();
    } catch (RateLimitException e) {
        // RateLimitException thrown. The bot is sending messages too quickly!
        System.err.print("Sending messages too quickly!");
        e.printStackTrace();
    } catch (DiscordException e) {
        // DiscordException thrown. Many possibilities. Use getErrorMessage() to see what went wrong.
        // Print the error message sent by Discord
        System.err.print(e.getErrorMessage());
        e.printStackTrace();
    } catch (MissingPermissionsException e) {
        // MissingPermissionsException thrown. The bot doesn't have permission to send the message!
        System.err.print("Missing permissions for channel!");
        e.printStackTrace();
    }
}
Also used : IChannel(sx.blah.discord.handle.obj.IChannel) MessageBuilder(sx.blah.discord.util.MessageBuilder) RateLimitException(sx.blah.discord.util.RateLimitException) IMessage(sx.blah.discord.handle.obj.IMessage) DiscordException(sx.blah.discord.util.DiscordException) MissingPermissionsException(sx.blah.discord.util.MissingPermissionsException)

Example 2 with MessageBuilder

use of sx.blah.discord.util.MessageBuilder in project Shadbot by Shadorc.

the class BotUtils method sendMessage.

public static RequestFuture<IMessage> sendMessage(EmbedObject embed, IChannel channel) {
    if (!BotUtils.hasPermissions(channel, Permissions.SEND_MESSAGES, Permissions.EMBED_LINKS)) {
        BotUtils.sendMessage(TextUtils.missingPerm(Permissions.EMBED_LINKS), channel);
        LogUtils.infof("{Guild ID: %d} Shadbot wasn't allowed to send embed link.", channel.getGuild().getLongID());
        return null;
    }
    VariousStatsManager.log(VariousEnum.EMBEDS_SENT);
    return BotUtils.sendMessage(new MessageBuilder(channel.getClient()).withChannel(channel).withEmbed(embed));
}
Also used : MessageBuilder(sx.blah.discord.util.MessageBuilder)

Example 3 with MessageBuilder

use of sx.blah.discord.util.MessageBuilder in project Shadbot by Shadorc.

the class CommandManager method execute.

public static void execute(Context context) {
    AbstractCommand cmd = COMMANDS_MAP.get(context.getCommandName());
    if (cmd == null) {
        return;
    }
    if (!BotUtils.isCommandAllowed(context.getGuild(), cmd)) {
        return;
    }
    CommandPermission authorPermission = context.getAuthorPermission();
    if (cmd.getPermission().isSuperior(authorPermission)) {
        BotUtils.sendMessage(Emoji.ACCESS_DENIED + " You do not have the permission to execute this command.", context.getChannel());
        return;
    }
    if (cmd.getRateLimiter() != null && cmd.getRateLimiter().isLimited(context.getChannel(), context.getAuthor())) {
        CommandStatsManager.log(CommandEnum.COMMAND_LIMITED, cmd);
        return;
    }
    try {
        cmd.execute(context);
        CommandStatsManager.log(CommandEnum.COMMAND_USED, cmd);
        VariousStatsManager.log(VariousEnum.COMMANDS_EXECUTED);
    } catch (IllegalCmdArgumentException err) {
        BotUtils.sendMessage(Emoji.GREY_EXCLAMATION + err.getMessage(), context.getChannel());
        CommandStatsManager.log(CommandEnum.COMMAND_ILLEGAL_ARG, cmd);
    } catch (MissingArgumentException err) {
        BotUtils.sendMessage(new MessageBuilder(context.getClient()).withChannel(context.getChannel()).withContent(TextUtils.MISSING_ARG).withEmbed(cmd.getHelp(context.getPrefix())));
        CommandStatsManager.log(CommandEnum.COMMAND_MISSING_ARG, cmd);
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MessageBuilder(sx.blah.discord.util.MessageBuilder) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException)

Example 4 with MessageBuilder

use of sx.blah.discord.util.MessageBuilder in project Shadbot by Shadorc.

the class SettingsCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    List<String> splitArgs = StringUtils.split(context.getArg(), 2);
    if (splitArgs.isEmpty()) {
        throw new MissingArgumentException();
    }
    SettingEnum settingEnum = Utils.getValueOrNull(SettingEnum.class, splitArgs.get(0));
    if (settingEnum == null || !SETTINGS_MAP.containsKey(settingEnum)) {
        throw new IllegalCmdArgumentException(String.format("Setting `%s` does not exist. Use `%shelp %s` to see all available settings.", splitArgs.get(0), context.getPrefix(), this.getName()));
    }
    AbstractSetting setting = SETTINGS_MAP.get(settingEnum);
    String arg = splitArgs.size() == 2 ? splitArgs.get(1) : null;
    if ("help".equals(arg)) {
        BotUtils.sendMessage(this.getHelp(context.getPrefix(), setting), context.getChannel());
        return;
    }
    try {
        setting.execute(context, arg);
    } catch (MissingArgumentException e) {
        BotUtils.sendMessage(new MessageBuilder(context.getClient()).withChannel(context.getChannel()).withContent(TextUtils.MISSING_ARG).withEmbed(this.getHelp(context.getPrefix(), setting)));
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) AbstractSetting(me.shadorc.shadbot.command.admin.setting.core.AbstractSetting) MessageBuilder(sx.blah.discord.util.MessageBuilder) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) SettingEnum(me.shadorc.shadbot.command.admin.setting.core.SettingEnum)

Aggregations

MessageBuilder (sx.blah.discord.util.MessageBuilder)4 IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)2 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)2 AbstractSetting (me.shadorc.shadbot.command.admin.setting.core.AbstractSetting)1 SettingEnum (me.shadorc.shadbot.command.admin.setting.core.SettingEnum)1 IChannel (sx.blah.discord.handle.obj.IChannel)1 IMessage (sx.blah.discord.handle.obj.IMessage)1 DiscordException (sx.blah.discord.util.DiscordException)1 MissingPermissionsException (sx.blah.discord.util.MissingPermissionsException)1 RateLimitException (sx.blah.discord.util.RateLimitException)1