added serverInfo help

added userInfo command
added offsetDateTime to the custom date formatter
This commit is contained in:
Sheldan
2020-05-27 15:10:54 +02:00
parent be97e825cc
commit de4eeeacca
27 changed files with 195 additions and 22 deletions

View File

@@ -25,7 +25,7 @@
<#assign user>${note.fullUser.member.asMention}</#assign>
<#assign noteText>${note.note.note}</#assign>
<#assign noteId>${note.note.id}</#assign>
<#assign date>${formatInstant(note.note.created, "yyyy-MM-dd HH:mm:ss")}</#assign>
<#assign date>${formatDate(note.note.created, "yyyy-MM-dd HH:mm:ss")}</#assign>
<#include "user_notes_note_entry"><#else><#include "user_notes_no_notes">
</#list>"
}

View File

@@ -34,7 +34,7 @@
},
{
"name": "<#include "mute_log_muted_until_field_title">",
"value": "${formatInstant(mute.muteTargetDate, "yyyy-MM-dd HH:mm:ss")}"
"value": "${formatDate(mute.muteTargetDate, "yyyy-MM-dd HH:mm:ss")}"
}
],
"footer": {

View File

@@ -37,7 +37,7 @@
},
{
"name": "<#include "unMute_log_muted_since_field_title">",
"value": "${formatInstant(mute.muteDate, "yyyy-MM-dd HH:mm:ss")}"
"value": "${formatDate(mute.muteDate, "yyyy-MM-dd HH:mm:ss")}"
},
{
"name": "<#include "mute_log_mute_duration_field_title">",

View File

@@ -2,10 +2,10 @@
<#assign reason>${warning.warning.reason}</#assign>
<#assign warnedUserText><#if warning.warnedUser.member??>${warning.warnedUser.member.asMention}(${warning.warnedUser.member.user.id})<#else>${warning.warnedUser.aUserInAServer.id}</#if></#assign>
<#assign warningUserText><#if warning.warningUser.member??>${warning.warningUser.member.asMention}(${warning.warningUser.member.user.id})<#else>${warning.warningUser.aUserInAServer.id}</#if></#assign>
<#assign warnDate>${formatInstant(warning.warning.warnDate, "yyyy-MM-dd HH:mm:ss")}</#assign>
<#assign warnDate>${formatDate(warning.warning.warnDate, "yyyy-MM-dd HH:mm:ss")}</#assign>
<#include "warnings_warn_entry_text">
<#if warning.warning.decayed>
<#assign decayDate>${formatInstant(warning.warning.decayDate, "yyyy-MM-dd HH:mm:ss")}</#assign>
<#assign decayDate>${formatDate(warning.warning.decayDate, "yyyy-MM-dd HH:mm:ss")}</#assign>
<#include "warnings_warn_is_decayed">
</#if>

View File

@@ -9,7 +9,7 @@
},
<#assign messageCount>${closedThread.messages?size}</#assign>
<#assign user>user</#assign>
<#assign startDate>${formatInstant(closedThread.created,"yyyy-MM-dd HH:mm:ss")}</#assign>
<#assign startDate>${formatDate(closedThread.created,"yyyy-MM-dd HH:mm:ss")}</#assign>
<#assign duration>${fmtDuration(duration)}</#assign>
"description": "<#include "close_closing_description">"
}

View File

@@ -0,0 +1,77 @@
package dev.sheldan.abstracto.utility.commands;
import dev.sheldan.abstracto.core.command.UtilityModuleInterface;
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.service.BotService;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.utility.config.features.UtilityFeature;
import dev.sheldan.abstracto.utility.models.template.commands.userinfo.UserInfoModel;
import net.dv8tion.jda.api.entities.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Component
public class UserInfo extends AbstractConditionableCommand {
@Autowired
private ChannelService channelService;
@Autowired
private BotService botService;
@Autowired
private UserInfo self;
@Override
public CommandResult execute(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
Member memberToShow = parameters.size() == 1 ? (Member) parameters.get(0) : commandContext.getUserInitiatedContext().getMember();
UserInfoModel model = (UserInfoModel) ContextConverter.fromCommandContext(commandContext, UserInfoModel.class);
if(!memberToShow.hasTimeJoined()) {
botService.forceReloadMember(memberToShow).thenAccept(member -> {
model.setMemberInfo(member);
self.sendResponse(commandContext, model);
});
} else {
model.setMemberInfo(memberToShow);
self.sendResponse(commandContext, model);
}
return CommandResult.fromSuccess();
}
@Transactional
public void sendResponse(CommandContext commandContext, UserInfoModel model) {
channelService.sendEmbedTemplateInChannel("userInfo_response", model, commandContext.getChannel());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().type(Member.class).name("member").templated(true).optional(true).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("userInfo")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.causesReaction(false)
.parameters(parameters)
.help(helpInfo)
.build();
}
@Override
public FeatureEnum getFeature() {
return UtilityFeature.UTILITY;
}
}

View File

@@ -0,0 +1,49 @@
{
"author": {
"name": "${memberInfo.user.name}#${memberInfo.user.discriminator}",
"avatar": "${memberInfo.user.effectiveAvatarUrl}"
},
"color" : {
"r": 200,
"g": 0,
"b": 255
},
"thumbnail": "${memberInfo.user.effectiveAvatarUrl}",
"fields": [
{
"name": "<#include "userInfo_response_embed_id_field_title">",
"value": "${memberInfo.user.id}",
"inline": "true"
},
<#if memberInfo.nickname?has_content>
{
"name": "<#include "userInfo_response_embed_nickname_field_title">",
"value": "${memberInfo.nickname}",
"inline": "true"
},
</#if>
{
"name": "<#include "userInfo_response_embed_status_field_title">",
"value": "${memberInfo.onlineStatus.key}",
"inline": "true"
},
{
"name": "<#include "userInfo_response_embed_joined_field_title">",
"value": "${formatDate(memberInfo.timeJoined, "yyyy-MM-dd HH:mm:ss")}",
"inline": "true"
},
{
"name": "<#include "userInfo_response_embed_registered_field_title">",
"value": "${formatDate(memberInfo.timeCreated, "yyyy-MM-dd HH:mm:ss")}",
"inline": "true"
}
<#if memberInfo.activities?size gt 0>
,
{
"name": "<#include "userInfo_response_embed_activity_field_title">",
"value": "<#list memberInfo.activities as activity>${activity.type}<#sep>, </#list>",
"inline": "true"
}
</#if>
]
}

View File

@@ -0,0 +1,14 @@
package dev.sheldan.abstracto.utility.models.template.commands.userinfo;
import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import net.dv8tion.jda.api.entities.Member;
@Getter
@Setter
@SuperBuilder
public class UserInfoModel extends UserInitiatedServerContext {
private Member memberInfo;
}

View File

@@ -124,6 +124,11 @@ public class BotServiceBean implements BotService {
return CompletableFuture.completedFuture(null);
}
@Override
public CompletableFuture<Member> forceReloadMember(Member member) {
return member.getGuild().retrieveMember(member.getUser()).submit();
}
@Override
public Optional<Emote> getEmote(Long serverId, AEmote emote) {
if(!emote.getCustom()) {

View File

@@ -25,6 +25,7 @@ public interface BotService {
Member getMemberInServer(AServer server, AUser member);
CompletableFuture<Void> deleteMessage(Long serverId, Long channelId, Long messageId);
CompletableFuture<Void> deleteMessage(Long channelId, Long messageId);
CompletableFuture<Member> forceReloadMember(Member member);
Optional<Emote> getEmote(Long serverId, AEmote emote);
Optional<TextChannel> getTextChannelFromServer(Guild serverId, Long textChannelId);
Optional<TextChannel> getTextChannelFromServer(Long serverId, Long textChannelId);

View File

@@ -1 +1 @@
${warnedUser} was warned on ${formatInstant(warning.warning.warnDate, "yyyy-MM-dd HH:mm:ss")} with reason `${warning.warning.reason}` by ${warningUser}
${warnedUser} was warned on ${formatDate(warning.warning.warnDate, "yyyy-MM-dd HH:mm:ss")} with reason `${warning.warning.reason}` by ${warningUser}

View File

@@ -0,0 +1 @@
Shows the ID, owner, member count, role count, features and emotes of the server.

View File

@@ -0,0 +1 @@
Cancels the given reminder to not be executed anymore.

View File

@@ -0,0 +1,3 @@
Shows the ID, nickname (if set), joined date, registration date, status and activities of a user.
Has an optional parameter to define for which user the information should be shown.
If no parameter is given, it shows this information for the user executing the command.

View File

@@ -2,7 +2,7 @@ package dev.sheldan.abstracto.templating.config;
import dev.sheldan.abstracto.templating.loading.DatabaseTemplateLoader;
import dev.sheldan.abstracto.templating.methods.DurationMethod;
import dev.sheldan.abstracto.templating.methods.InstantMethod;
import dev.sheldan.abstracto.templating.methods.DateMethod;
import dev.sheldan.abstracto.templating.methods.SafeFieldIterations;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
@@ -26,7 +26,7 @@ public class FreemarkerConfiguration {
private DurationMethod durationMethod;
@Autowired
private InstantMethod instantMethod;
private DateMethod instantMethod;
@Autowired
private SafeFieldIterations safeFieldIterations;
@@ -43,7 +43,7 @@ public class FreemarkerConfiguration {
factory.setPreTemplateLoaders(templateLoader);
Configuration configuration = factory.createConfiguration();
configuration.setSharedVariable("fmtDuration", durationMethod);
configuration.setSharedVariable("formatInstant", instantMethod);
configuration.setSharedVariable("formatDate", instantMethod);
configuration.setSharedVariable("safeFieldLength", safeFieldIterations);
configuration.setEncoding(Locale.getDefault(), "utf-8");
// needed to support default methods in interfaces

View File

@@ -9,24 +9,25 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
* Formats the passed {@link Instant} object with the given Formatter. The format will be directly passed to {@link DateTimeFormatter}.
* Formats the passed {@link Instant} or {@link OffsetDateTime} object with the given Formatter. The format will be directly passed to {@link DateTimeFormatter}.
*/
@Component
public class InstantMethod implements TemplateMethodModelEx {
public class DateMethod implements TemplateMethodModelEx {
@Autowired
private TemplateService service;
/**
* Renders the given {@link Instant} object with the given String. Internally {@link DateTimeFormatter} will be used.
* @param arguments The list of arguments, first element must be an {@link Instant} and the second one must be a {@link String}.
* @param arguments The list of arguments, first element must be an {@link Instant} or {@link OffsetDateTime} and the second one must be a {@link String}.
* @return The formatted {@link Instant} as a string.
* @throws TemplateModelException If there are less or more arguments in the list and if the first element is not a {@link Instant}.
* @throws TemplateModelException If there are less or more arguments in the list and if the first element is not a {@link Instant} of {@link OffsetDateTime}
*/
@Override
public Object exec(List arguments) throws TemplateModelException {
@@ -34,14 +35,21 @@ public class InstantMethod implements TemplateMethodModelEx {
throw new TemplateModelException("Incorrect parameters passed.");
}
Object wrappedObject = ((StringModel) arguments.get(0)).getWrappedObject();
if(!(wrappedObject instanceof Instant)) {
boolean isOffsetDateTime = wrappedObject instanceof OffsetDateTime;
boolean isInstant = wrappedObject instanceof Instant;
if(!isInstant && !isOffsetDateTime) {
throw new TemplateModelException("Passed argument was not a instant object");
}
String formatString = ((SimpleScalar) arguments.get(1)).getAsString();
Instant timeStamp = (Instant) wrappedObject;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatString)
.withZone(ZoneId.systemDefault());
return formatter.format(timeStamp);
if(isInstant) {
Instant timeStamp = (Instant) wrappedObject;
return formatter.format(timeStamp);
} else {
OffsetDateTime offsetDateTime = (OffsetDateTime) wrappedObject;
return formatter.format(offsetDateTime);
}
}
}

View File

@@ -92,11 +92,15 @@ public class TemplateServiceBean implements TemplateService {
if(configuration.getFields() != null) {
for (int i = 0; i < configuration.getFields().size(); i++) {
EmbedField field = configuration.getFields().get(i);
if(field.getValue().length() > 1024) {
String substring = field.getValue().substring(1024);
field.setValue(field.getValue().substring(0, 1024));
EmbedField secondPart = EmbedField.builder().inline(field.getInline()).name(field.getName() + " 2").value(substring).build();
configuration.getFields().add(i + 1, secondPart);
if(field != null && field.getValue() != null) {
if(field.getValue().length() > MessageEmbed.VALUE_MAX_LENGTH) {
String substring = field.getValue().substring(MessageEmbed.VALUE_MAX_LENGTH);
field.setValue(field.getValue().substring(0, MessageEmbed.VALUE_MAX_LENGTH));
EmbedField secondPart = EmbedField.builder().inline(field.getInline()).name(field.getName() + " 2").value(substring).build();
configuration.getFields().add(i + 1, secondPart);
}
} else {
log.warn("Field {} in template {} is null.", i, key);
}
}
double neededIndex = Math.ceil(configuration.getFields().size() / 25D) - 1;