Warning: session_start(): open(/var/lib/php/sessions/sess_1ep0duicclfjcb6en4tt27aq66, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
!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;
if (channelID == 0) null;
optPar Message msg;
Message.Attachment attachment = msg == null ? null : first(msg.getAttachments());
if (attachment != null) {
File file = downloadFileForChannel(channelID);
print("Downloading attachment " + attachment.getFileName() + " to " + 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 trimAllLines([[
@myName export - export roles + members as JSON
@myName rights - check bot's rights
@myName diff - diff last upload against server
Upload a JSON file to get it diffed with current server
]].replace("@myName", myName);
if "rights"
ret "Manage roles: " + yesNo(canManageRoles());
if "diff" {
File f = downloadFileForChannel(channelID);
if (!fileExists(f)) ret "No past upload found in this channel";
}
}
File downloadFileForChannel(long channelID) {
ret prepareCacheProgramFile(guildID + "/" + channelID);
}
bool canManageRoles() {
ret 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));
ret diff(file);
} catch print e {
ret "Internal error";
}
}
S diff(File 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);
}
}