Uses 9211K of libraries. Click here for Pure Java version (14455L/82K).
!7 set flag DynModule. cmodule DiscordBot > DynPrintLogAndEnabled { transient JDA bot; transient new Set<Long> msgsReactedTo; transient double deleteDelay = 60.0; 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; GazelleEvalContext ctx = dm_gazelle_stdEvalContext(dm_gazelle_allRulesWithComment("discord")); GazelleTree tree = new(ctx, content); L<GazelleTree> l = dm_gazelle_getChildren(tree); if (empty(l)) ret; for (GazelleTree t : takeFirst(10, l)) postInChannelWithDelete(e.getChannel(), 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) { bot.getTextChannelById(channelID).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(); }); }); } }
Began life as a copy of #1021622
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1021630 |
Snippet name: | Discord Bot [v6, store lines in DB] |
Eternal ID of this version: | #1021630/24 |
Text MD5: | 9f35ee8ea57389d3886a5e7fa2e5aa32 |
Transpilation MD5: | 43e04ae57bf8614cfda072888c9ae991 |
Author: | stefan |
Category: | javax / discord |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-02-23 21:34:52 |
Source code size: | 4069 bytes / 119 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 359 / 662 |
Version history: | 23 change(s) |
Referenced in: | #1021659 - Discord Bot [v7, with !eval] |