use of sx.blah.discord.util.MissingPermissionsException 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();
}
}
use of sx.blah.discord.util.MissingPermissionsException in project Ublisk by Derkades.
the class DiscordBot method onChat.
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChat(AsyncPlayerChatEvent event) {
if (!enabled) {
super.log(this, LogLevel.DEBUG, "Skipping discord bot for this message - module has not been initialized successfully.");
return;
}
Player player = event.getPlayer();
String message = "[" + player.getName() + "] " + event.getMessage();
IChannel channel = client.getChannelByID("310155421719724052");
try {
channel.sendMessage(message);
} catch (MissingPermissionsException e) {
e.printStackTrace();
} catch (RateLimitException e) {
e.printStackTrace();
} catch (DiscordException e) {
e.printStackTrace();
}
}
use of sx.blah.discord.util.MissingPermissionsException in project Discord4J by Discord4J.
the class RoleBot method handle.
/**
* Client is ready to interact with Discord.
* @see ReadyBot
*/
@Override
public void handle(ReadyEvent event) {
try {
// Gets the first guild the bot is a member of. (NOTE: This is only for demonstration. Getting guilds in this way is NOT recommended. Use IDs or events instead.)
IGuild guild = event.getClient().getGuilds().get(0);
// Instantiate a RoleBuilder which will aide in the creation of the role.
RoleBuilder roleBuilder = new RoleBuilder(guild);
// Set the new role's name
roleBuilder.withName("Awesome Role");
// Set the new role's color
roleBuilder.withColor(Color.GREEN);
// Make the new role display separately from others in Discord.
roleBuilder.setHoist(true);
// Allow this role to be mentionable in chat.
roleBuilder.setMentionable(true);
// Assign the Administrator permission to this role.
roleBuilder.withPermissions(EnumSet.of(Permissions.ADMINISTRATOR));
// Add the role to the guild in Discord.
IRole role = roleBuilder.build();
// Gets the user of the bot
IUser ourUser = event.getClient().getOurUser();
// Assigns our new role to the bot. NOTE: This will make the bot's ONLY role our role.
guild.editUserRoles(ourUser, new IRole[] { role });
} catch (MissingPermissionsException | RateLimitException | DiscordException e) {
// Error occurred
e.printStackTrace();
}
}
use of sx.blah.discord.util.MissingPermissionsException in project Shadbot by Shadorc.
the class BotUtils method sendMessage.
public static RequestFuture<IMessage> sendMessage(MessageBuilder message, int retry) {
IGuild guild = message.getChannel().isPrivate() ? null : message.getChannel().getGuild();
long guildID = guild == null ? -1 : guild.getLongID();
if (retry == 0) {
LogUtils.infof("{Guild ID: %d} Abort attempt to send message (3 failed requests).", guildID);
return null;
}
if (!message.getChannel().getShard().isReady()) {
if (guild != null) {
LogUtils.infof("{Guild ID: %d} A message couldn't be sent because shard isn't ready, adding it to queue.", guildID);
ShardManager.getShadbotShard(guild.getShard()).queue(message);
}
return null;
}
return RequestBuffer.request(() -> {
try {
return message.send();
} catch (MissingPermissionsException err) {
BotUtils.sendMessage(TextUtils.missingPerm(err.getMissingPermissions()), message.getChannel());
LogUtils.infof("{Guild ID: %d} %s", guildID, err.getMessage());
} catch (DiscordException err) {
if (err.getMessage().contains("Message was unable to be sent (Discord didn't return a response)")) {
LogUtils.infof("{Guild ID: %d} A message could not be send because Discord didn't return a response, retrying.", guildID);
RequestFuture<IMessage> msgRequest = BotUtils.sendMessage(message, retry - 1);
if (msgRequest != null) {
return msgRequest.get();
}
} else if (err.getMessage().contains("Failed to make a 400 failed request after 5 tries!")) {
LogUtils.infof("{Guild ID: %d} %s", guildID, err.getMessage());
} else {
LogUtils.error(err, "An error occurred while sending message.");
}
}
return null;
});
}
use of sx.blah.discord.util.MissingPermissionsException in project Shadbot by Shadorc.
the class MessageListener method onMessageReceivedEvent.
private void onMessageReceivedEvent(MessageReceivedEvent event) {
VariousStatsManager.log(VariousEnum.MESSAGES_RECEIVED);
IMessage message = event.getMessage();
try {
if (message.getAuthor().isBot()) {
return;
}
if (message.getChannel().isPrivate()) {
this.privateMessageReceived(message);
return;
}
ShardManager.getShadbotShard(message.getShard()).messageReceived();
if (!BotUtils.isChannelAllowed(message.getGuild(), message.getChannel())) {
return;
}
if (MessageManager.intercept(message)) {
return;
}
String prefix = Database.getDBGuild(message.getGuild()).getPrefix();
if (message.getContent().startsWith(prefix)) {
CommandManager.execute(new Context(prefix, message));
}
} catch (MissingPermissionsException err) {
BotUtils.sendMessage(TextUtils.missingPerm(err.getMissingPermissions()), message.getChannel());
LogUtils.infof("{Guild ID: %d} %s", message.getGuild().getLongID(), err.getMessage());
} catch (Exception err) {
BotUtils.sendMessage(Emoji.RED_FLAG + " Sorry, an unknown error occurred. My developer has been warned.", message.getChannel());
LogUtils.error(message.getContent(), err, String.format("{Guild ID: %d} An unknown error occurred while receiving a message.", message.getGuild().getLongID()));
}
}
Aggregations