Warning: session_start(): open(/var/lib/php/sessions/sess_7j1b0r1hssftu4c7cmd82lsiip, 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
set flag DynModule.
cmodule DiscordBot > DynPrintLogAndEnabled {
transient JDA bot;
transient new Set msgsReactedTo;
transient double deleteDelay = 60.0;
transient Color imageEmbedMysteriousLineColor = colorFromHex("36393f");
transient new InheritableThreadLocal currentChannel;
start {
logModuleOutput();
// TODO: log JDA output
bot = discordBot(new ListenerAdapter {
public void onMessageReceived(MessageReceivedEvent e) pcall {
ret if !enabled || !licensed();
bool bot = e.getAuthor().isBot();
Message msg = e.getMessage();
print("Channel type: " + e.getChannelType());
bool isPrivate = e.getChannelType() == ChannelType.PRIVATE;
print("Msg from " + e.getAuthor().getName() + ": " + e.getMessage().getContentDisplay());
S content = trim(msg.getContentRaw());
if (empty(content)) ret;
O user = userConcept(e.getAuthor());
S crud = dm_gazelle_linesCRUD();
O channel = dm_call(crud, 'uniqChannel, msg.getChannel().getIdLong());
dm_call(crud, 'cset, channel, litobjectarray(name := msg.getChannel().getName()));
O lineConcept = dm_call(crud, 'uniqConcept, litObjectArrayAsObject(msgID := e.getMessage().getIdLong()));
dm_call(crud, 'cset, lineConcept, litobjectarray(
text := content,
+bot, +isPrivate,
author := user, +channel));
ret if bot;
final MessageChannel ch = e.getChannel();
if (swicOneOf(content, "!eval ", "!fresh ", "!real-eval", "!safe-eval ") || eqic(content, "!fresh")) {
O safetyCheck = null;
bool authed = dm_discord_userCanEval(e.getAuthor().getIdLong());
new Matches m;
if (swic(content, "!safe-eval ", m)) {
content = "!eval " + m.rest();
authed = false;
}
if (!authed) {
//ret with postInChannel(ch, "Sorry, you're not authed to eval");
safetyCheck = func(S code) -> S {
S safety = joinWithComma(getCodeFragmentSafety(code));
if (eq(safety, 'safe)) ret "";
S s = "Sorry. Safety level is " + safety;
Set unknown = codeAnalysis_getUnknownIdentifiers(code);
if (nempty(unknown)) s += ". Unknown identifiers: " + joinWithComma(unknown);
ret s;
};
}
temp tempSetTL(currentChannel, ch);
ret with dm_bot_execEvalCmd(voidfunc(S s) {
postInChannel(ch, shorten(s, 1000))
}, content, +safetyCheck);
}
GazelleEvalContext ctx = dm_gazelle_stdEvalContext(dm_gazelle_allRulesWithComment("discord"));
GazelleTree tree = new(ctx, content);
L l = dm_gazelle_getChildren(tree);
if (empty(l)) ret;
for (GazelleTree t : takeFirst(10, l))
postInChannelWithDelete(ch, t.line);
}
public void onMessageReactionAdd(MessageReactionAddEvent e) pcall {
ret if !enabled || !licensed();
MessageReaction r = e.getReaction();
bool bot = e.getUser().isBot();
long msgID = r.getMessageIdLong();
add(msgsReactedTo, msgID);
print("User " + e.getUser() + (bot ? " (bot)" : "") + " reacted to message " + msgID + " with " + r.getReactionEmote());
if (bot) ret;
S crud = dm_gazelle_linesCRUD();
O lineConcept = dm_call(crud, 'uniqConcept, litObjectArrayAsObject(+msgID));
L reactions = cast get(lineConcept, 'reactions);
print("lineConcept=" + lineConcept);
print("reactions=" + reactions);
O reaction = dm_call(crud, 'nuReaction, litObjectArrayAsObject(
user := userConcept(e.getUser()),
emoji := r.getReactionEmote().getName(),
created := now()));
print("reaction=" + reaction);
dm_call(crud, 'cset, lineConcept, litobjectarray(
reactions := listPlus(reactions, reaction)));
}
});
dm_registerAs('discordBot);
print("Started bot");
}
void cleanMeUp {
if (bot != null) pcall {
print("Shutting down bot");
bot.shutdown();
print("Bot shut down");
}
bot = null;
}
O userConcept(User user) {
S crud = dm_gazelle_linesCRUD();
O userConcept = dm_call(crud, 'uniqUser, user.getIdLong());
dm_call(crud, 'cset, userConcept, litobjectarray(name := user.getName()));
ret userConcept;
}
// API
void postInChannel(long channelID, S msg) {
postInChannel(bot.getTextChannelById(channelID), msg);
}
void postInChannel(MessageChannel channel, S msg) {
channel.sendMessage(msg).queue();
}
void postInChannel(S channel, S msg) {
long id = dm_discord_channelID(channel);
if (id == 0) fail("Channel not found: " + channel);
postInChannel(id, msg);
}
void postInChannelWithDelete(MessageChannel channel, S msg) {
channel.sendMessage(msg).queue(msg2 -> {
final long msgId = msg2.getIdLong();
print("I sent msg: " + msgId);
doLater(deleteDelay, r {
ret if contains(msgsReactedTo, msgId);
print("Deleting msg " + msgId);
msg2.delete().queue();
});
});
}
void postImage(S url) {
postImage(currentChannel!, url);
}
void postImage(S url, S title) {
postImage(currentChannel!, url, title);
}
void postImage(MessageChannel channel, S url) {
postImage(channel, url, "");
}
void postImage(MessageChannel channel, S url, S description) {
channel.sendMessage(
new EmbedBuilder()
.setImage(absoluteURL(url))
//.setTitle(unnull(title))
.setDescription(unnull(description))
.setColor(imageEmbedMysteriousLineColor)
.build()).queue();
}
}