use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty in project aiode by robinfriedli.
the class GuildPropertyInterceptor method onFlushDirty.
// use onFlushDirty instead of onFlushDirtyChained as exceptions should get thrown
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
if (entity instanceof GuildSpecification) {
for (int i = 0; i < currentState.length; i++) {
String propertyName = propertyNames[i];
Object current = currentState[i];
Object previous = previousState[i];
if (current != null && !current.equals(previous)) {
AbstractGuildProperty property = guildPropertyManager.getProperty(propertyName);
if (property != null) {
changedProperties.put(property, Pair.of(previous, current));
property.validate(current);
}
}
}
}
return next().onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
}
use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty in project aiode by robinfriedli.
the class GuildPropertyInterceptor method beforeTransactionCompletionChained.
@Override
public void beforeTransactionCompletionChained(Transaction tx) {
if (!tx.getRollbackOnly()) {
for (AbstractGuildProperty property : changedProperties.keySet()) {
if ("argumentPrefix".equals(property.getProperty())) {
// loop of triggering itself
try (Session session = sessionFactory.openSession()) {
HibernateInvoker.create(session).invoke(() -> {
Pair<Object, Object> previousWithNewValue = changedProperties.get(property);
// previous might be null
updatePresets(property, (Character) previousWithNewValue.getLeft(), (char) previousWithNewValue.getRight(), session);
});
} catch (Exception e) {
messageService.sendException("Exception occurred while updating presets with new argument prefix. " + "Presets will have to be updated manually if necessary.", commandContext.getChannel());
LoggerFactory.getLogger(getClass()).error("Exception while updating presets", e);
}
}
}
}
}
use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty in project aiode by robinfriedli.
the class GuildPropertyInterceptor method afterTransactionCompletionChained.
@Override
public void afterTransactionCompletionChained(Transaction tx) {
if (!tx.getRollbackOnly()) {
if (!changedProperties.isEmpty()) {
StringBuilder successMessageBuilder = new StringBuilder();
for (AbstractGuildProperty property : changedProperties.keySet()) {
Pair<Object, Object> previousWithNewValue = changedProperties.get(property);
String updateMessage = property.getContribution().getUpdateMessage(previousWithNewValue.getRight());
successMessageBuilder.append(updateMessage).append(System.lineSeparator());
}
messageService.sendSuccess(successMessageBuilder.toString(), commandContext.getChannel());
}
}
changedProperties.clear();
}
use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty 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);
}
}
use of net.robinfriedli.aiode.discord.property.AbstractGuildProperty in project aiode by robinfriedli.
the class PropertyCommand method listProperties.
private void listProperties() {
GuildPropertyManager guildPropertyManager = Aiode.get().getGuildPropertyManager();
List<AbstractGuildProperty> properties = guildPropertyManager.getProperties();
GuildSpecification specification = getContext().getGuildContext().getSpecification();
EmbedBuilder embedBuilder = new EmbedBuilder();
EmbedTable table = new EmbedTable(embedBuilder);
table.addColumn("Name", properties, AbstractGuildProperty::getName);
table.addColumn("Default Value", properties, AbstractGuildProperty::getDefaultValue);
table.addColumn("Set Value", properties, property -> property.display(specification));
table.build();
sendMessage(embedBuilder);
}
Aggregations