!7 !include once #1026298 // DynTalkBot2 for JDA 4.0 standardBot1 RolesExporter { init { myName = "HyperCubes"; preprocessAtSelfToMyName = true; } sync S processSimplifiedLine(S s, O... _) null { try answer super.processSimplifiedLine(s, _); optPar long channelID; optPar Message msg; Message.Attachment attachment = msg == null ? null : first(msg.getAttachments()); if (attachment != null) { File file = prepareCacheProgramFile(guildID + "/" + attachment.getFileName()); print("Downloading attachment: " + file); deleteFile(file); attachment.downloadToFile(file) .exceptionally(error -> { temp enter(); print("download fail"); ret null with postInChannel(channelID, "Couldn't download attachment"); }) .thenAccept(file2 -> postInChannel(channelID, handleAttachment(file2))); } if null (s = dropMyPrefixOrNull(s)) null; if "help" ret autoUnindent([[ @myName export - export roles + members as JSON @myName rights - check bot's rights Upload a JSON file to get it diffed with current server ]].replace("@myName", myName); if "rights" ret "Manage roles: " + yesNo(guild.getSelfMember().hasPermission(Permission.MANAGE_ROLES)); } Map makeData() { L allRoles = map(guild.getRoles(), r -> litorderedmap( "name" := r.getName(), "id" := r.getIdLong(), "permissions" := r.getPermissionsRaw())); L members = map(guild.getMembers(), m -> litorderedmap( "nickName" := m.getNickname(), "effectiveName" := m.getEffectiveName(), "userName" := m.getUser().getName(), "id" := m.getIdLong(), "isOwner" := trueOrNull(m.isOwner()), "roles" := map(m.getRoles(), r -> litorderedmap("name" := r.getName(), "id" := r.getIdLong())))); ret litorderedmap( dataType := "members and roles for guild", dateExported := dateWithSecondsGMT(), guild := litorderedmap( name := guild.getName(), id := guild.getIdLong()), +allRoles, +members); } S handleAttachment(File file) enter { try { print("Handling attachment " + f2s(file)); O json; try { json = decodeJson(loadTextFile(file)); } catch e { ret null with print("Not a JSON file: " + f2s(file)); } new JsonDiff differ; // identify roles by names and members by ID differ.getListKey = path -> eq(last(path), 'allRoles) ? 'name : 'id; O diff = differ.diff(makeData(), json); //ret "Differences: " + structForUser(diff); ret "Differences: " + nicelyFormatJsonDiff(diff); } catch print e { ret "Internal error"; } } }