use of net.robinfriedli.aiode.Aiode in project aiode by robinfriedli.
the class GuildPropertyInterceptor method updatePresets.
private void updatePresets(AbstractGuildProperty argumentPrefixProperty, Character oldArgumentPrefix, char newArgumentPrefix, Session session) {
List<Preset> presets = queryBuilderFactory.find(Preset.class).build(session).getResultList();
String defaultValue = argumentPrefixProperty.getDefaultValue();
char[] chars = defaultValue.toCharArray();
char defaultArgPrefix;
if (chars.length == 1) {
defaultArgPrefix = chars[0];
} else {
defaultArgPrefix = ArgumentPrefixProperty.DEFAULT_FALLBACK;
}
char argPrefix = oldArgumentPrefix != null ? oldArgumentPrefix : defaultArgPrefix;
ArgumentPrefixProperty.Config argumentPrefixConfig = new ArgumentPrefixProperty.Config(argPrefix, defaultArgPrefix);
for (Preset preset : presets) {
Aiode aiode = Aiode.get();
AbstractCommand command = preset.instantiateCommand(aiode.getCommandManager(), commandContext, preset.getName());
String oldPreset = preset.getPreset();
StringBuilder newPresetBuilder = new StringBuilder(oldPreset);
List<Integer> oldPrefixOccurrences = Lists.newArrayList();
CommandParser commandParser = new CommandParser(command, argumentPrefixConfig, new CommandParseListener() {
@Override
public void onModeSwitch(CommandParser.Mode previousMode, CommandParser.Mode newMode, int index, char character) {
// they are in fact argument prefixes
if (newMode instanceof ArgumentBuildingMode) {
oldPrefixOccurrences.add(index);
}
}
});
commandParser.parse(oldPreset);
for (int oldPrefixOccurrence : oldPrefixOccurrences) {
newPresetBuilder.setCharAt(oldPrefixOccurrence, newArgumentPrefix);
}
String newPreset = newPresetBuilder.toString();
if (!oldPreset.equals(newPreset)) {
preset.setPreset(newPreset);
}
}
}
use of net.robinfriedli.aiode.Aiode in project aiode by robinfriedli.
the class PermissionExceptionHandler method handleException.
@Override
public Result handleException(Throwable uncaughtException, PermissionException exceptionToHandle) {
if (Aiode.isInitialised()) {
Aiode aiode = Aiode.get();
ExecutionContext executionContext = ExecutionContext.Current.get();
if (executionContext != null) {
MessageService messageService = aiode.getMessageService();
Permission permission = exceptionToHandle.getPermission();
TextChannel channel = executionContext.getChannel();
Guild guild = executionContext.getGuild();
Aiode.LOGGER.warn(String.format("Bot is missing permission %s on guild %s", permission, guild));
messageService.send("Bot is missing permission: " + permission.getName(), channel);
return Result.HANDLED;
}
}
return Result.UNHANDLED;
}
use of net.robinfriedli.aiode.Aiode in project aiode by robinfriedli.
the class ExceptionHandlerExecutor method handleException.
/**
* Finds applicable {@link ExceptionHandler}s by calling {@link ExceptionHandlerRegistry#getApplicableExceptionHandlersOrdered(Class)}
* and calls {@link ExceptionHandler#handleException(Throwable, Throwable)}.
*
* @param e the exception to handle.
* @throws Throwable any throwable, usually the input throwable propagated by {@link #handleUnhandled(Throwable)}
*/
@SuppressWarnings("unchecked")
public final void handleException(Throwable e) throws Throwable {
if (Aiode.isInitialised()) {
boolean handled = false;
try {
Aiode aiode = Aiode.get();
ExceptionHandlerRegistry exceptionHandlerRegistry = aiode.getExceptionHandlerRegistry();
Throwable toHandle = e;
outer: do {
List<ExceptionHandler<?>> applicableHandlers = exceptionHandlerRegistry.getApplicableExceptionHandlersOrdered(toHandle.getClass());
// safety: applicableHandlers only contains handlers that accept Throwables of the type of toHandle or a supertype thereof
for (@SuppressWarnings("rawtypes") ExceptionHandler handler : applicableHandlers) {
// noinspection unchecked
ExceptionHandler.Result result = handler.handleException(e, toHandle);
if (result == ExceptionHandler.Result.HANDLED) {
handled = true;
break outer;
} else if (result == ExceptionHandler.Result.SKIP_TO_CAUSE) {
break;
}
}
} while ((toHandle = toHandle.getCause()) != null);
} catch (Exception e1) {
logger.error(String.format("Exception occurred while calling exception handlers for exception %s. Falling back to #handleUnhandled", e), e1);
handleUnhandled(e);
return;
}
if (!handled) {
handleUnhandled(e);
}
} else {
handleUnhandled(e);
}
}
use of net.robinfriedli.aiode.Aiode in project aiode by robinfriedli.
the class DeleteGrantedRolesForDeletedRolesTask method run.
@Override
protected void run(JobExecutionContext jobExecutionContext) {
Aiode aiode = Aiode.get();
ShardManager shardManager = aiode.getShardManager();
QueryBuilderFactory queryBuilderFactory = aiode.getQueryBuilderFactory();
StaticSessionProvider.consumeSession(session -> {
List<GuildSpecification> guildSpecifications = queryBuilderFactory.find(GuildSpecification.class).build(session).getResultList();
int deletionCounter = 0;
for (GuildSpecification guildSpecification : guildSpecifications) {
Guild guild = guildSpecification.getGuild(shardManager);
if (guild == null) {
continue;
}
for (AccessConfiguration accessConfiguration : guildSpecification.getAccessConfigurations()) {
Set<GrantedRole> grantedRoles = accessConfiguration.getRoles();
for (GrantedRole grantedRole : grantedRoles) {
Role guildRole = grantedRole.getRole(guild);
if (guildRole == null) {
// role has been deleted
accessConfiguration.removeRole(grantedRole);
session.delete(grantedRole);
++deletionCounter;
}
}
}
}
if (deletionCounter > 0) {
logger.info("Deleted " + deletionCounter + " GrantedRole entities for roles that no longer exist");
}
});
}
use of net.robinfriedli.aiode.Aiode in project aiode by robinfriedli.
the class GuildManager method getDefaultTextChannelForGuild.
private TextChannel getDefaultTextChannelForGuild(Guild guild, GuildContext guildContext) {
Aiode aiode = Aiode.get();
Member selfMember = guild.getSelfMember();
// fetch the default text channel from the customised property
GuildPropertyManager guildPropertyManager = aiode.getGuildPropertyManager();
AbstractGuildProperty defaultTextChannelProperty = guildPropertyManager.getProperty("defaultTextChannelId");
if (defaultTextChannelProperty != null) {
String defaultTextChannelId;
try {
defaultTextChannelId = (String) hibernateComponent.invokeWithSession(session -> defaultTextChannelProperty.get(guildContext.getSpecification(session)));
} catch (NullPointerException e) {
throw new RuntimeException(e);
}
if (!Strings.isNullOrEmpty(defaultTextChannelId)) {
TextChannel textChannelById = guild.getTextChannelById(defaultTextChannelId);
if (textChannelById != null && selfMember.hasAccess(textChannelById) && textChannelById.canTalk(selfMember)) {
return textChannelById;
}
}
}
// check if the guild's playback has a current communication text channel
MessageChannel playbackCommunicationChannel = guildContext.getPlayback().getCommunicationChannel();
if (playbackCommunicationChannel instanceof TextChannel) {
TextChannel textChannelFromPlayback = (TextChannel) playbackCommunicationChannel;
if (selfMember.hasAccess(textChannelFromPlayback) && textChannelFromPlayback.canTalk(selfMember)) {
return textChannelFromPlayback;
}
}
// use guild default defined by discord
TextChannel defaultChannel = guild.getDefaultChannel();
if (defaultChannel != null && selfMember.hasAccess(defaultChannel) && defaultChannel.canTalk(selfMember)) {
return defaultChannel;
} else {
TextChannel systemChannel = guild.getSystemChannel();
if (systemChannel != null && selfMember.hasAccess(systemChannel) && systemChannel.canTalk()) {
return systemChannel;
}
}
List<TextChannel> availableChannels = guild.getTextChannels().stream().filter(channel -> selfMember.hasAccess(channel) && channel.canTalk(selfMember)).collect(Collectors.toList());
if (availableChannels.isEmpty()) {
return null;
} else {
return availableChannels.get(0);
}
}
Aggregations