-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordMessageModifiedScriptEvent.java
167 lines (158 loc) · 7.46 KB
/
DiscordMessageModifiedScriptEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.denizenscript.ddiscordbot.events;
import com.denizenscript.ddiscordbot.DiscordScriptEvent;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.objects.DiscordChannelTag;
import com.denizenscript.ddiscordbot.objects.DiscordGroupTag;
import com.denizenscript.ddiscordbot.objects.DiscordUserTag;
import discord4j.core.event.domain.message.MessageUpdateEvent;
import discord4j.core.object.entity.GuildChannel;
import discord4j.core.object.entity.MessageChannel;
import discord4j.core.object.entity.User;
import discord4j.core.object.util.Snowflake;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.utilities.CoreUtilities;
public class DiscordMessageModifiedScriptEvent extends DiscordScriptEvent {
public static DiscordMessageModifiedScriptEvent instance;
// <--[event]
// @Events
// discord message modified
//
// @Regex ^on discord message modified$
//
// @Switch for <bot>
// @Switch channel <channel_id>
// @Switch group <group_id>
//
// @Triggers when a Discord user modified a message.
//
// @Plugin dDiscordBot
//
// @Context
// <context.bot> returns the Denizen ID of the bot.
// <context.self> returns the bots own Discord user ID.
// <context.channel> returns the channel.
// <context.group> returns the group.
// <context.author> returns the user that authored the message.
// <context.mentions> returns a list of all mentioned users.
// <context.is_direct> returns whether the message was sent directly to the bot (if false, the message was sent to a public channel).
// <context.message> returns the message (raw).
// <context.message_id> returns the message ID.
// <context.no_mention_message> returns the message with all user mentions stripped.
// <context.formatted_message> returns the formatted message (mentions/etc. are written cleanly). CURRENTLY NON-FUNCTIONAL.
// <context.old_message_valid> returns whether the old message is available (it may be lost due to caching).
// <context.old_message> returns the previous message (raw).
// <context.old_no_mention_message> returns the previous message with all user mentions stripped.
// <context.old_formatted_message> returns the formatted previous message (mentions/etc. are written cleanly). CURRENTLY NON-FUNCTIONAL.
//
// -->
public MessageUpdateEvent getEvent() {
return (MessageUpdateEvent) event;
}
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
return CoreUtilities.toLowerCase(s).startsWith("discord message modified");
}
@Override
public boolean matches(ScriptPath path) {
if (!path.checkSwitch("channel", String.valueOf(getEvent().getChannelId().asLong()))) {
return false;
}
if (getEvent().getGuildId().isPresent() && !path.checkSwitch("group", String.valueOf(getEvent().getGuildId().get().asLong()))) {
return false;
}
return super.matches(path);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("channel")) {
return new DiscordChannelTag(botID, getEvent().getChannelId().asLong());
}
else if (name.equals("group")) {
if (getEvent().getChannel().block() instanceof GuildChannel) {
return new DiscordGroupTag(botID, ((GuildChannel) getEvent().getChannel().block()).getGuildId().asLong());
}
}
else if (name.equals("old_message_valid")) {
return new ElementTag(getEvent().getOld().isPresent());
}
else if (name.equals("old_message")) {
if (getEvent().getOld().isPresent()) {
return new ElementTag(getEvent().getOld().get().getContent().get());
}
}
else if (name.equals("old_no_mention_message")) {
if (getEvent().getOld().isPresent()) {
return new ElementTag(stripMentions(getEvent().getOld().get().getContent().get(),
getEvent().getOld().get().getUserMentions()));
}
}
else if (name.equals("old_formatted_message")) {
if (getEvent().getOld().isPresent()) {
return new ElementTag(getEvent().getOld().get().getContent().get());
}
}
else if (name.equals("message")) {
return new ElementTag(getEvent().getMessage().block().getContent().orElse(""));
}
else if (name.equals("message_id")) {
return new ElementTag(getEvent().getMessageId().asString());
}
else if (name.equals("no_mention_message")) {
return new ElementTag(stripMentions(getEvent().getMessage().block().getContent().orElse(""),
getEvent().getMessage().block().getUserMentions()));
}
else if (name.equals("formatted_message")) {
return new ElementTag(getEvent().getMessage().block().getContent().orElse(""));
}
else if (name.equals("author")) {
return new DiscordUserTag(botID, getEvent().getMessage().block().getAuthor().get());
}
else if (name.equals("mentions")) {
ListTag list = new ListTag();
for (Snowflake user : getEvent().getMessage().block().getUserMentionIds()) {
list.addObject(new DiscordUserTag(botID, user.asLong()));
}
return list;
}
else if (name.equals("is_direct")) {
return new ElementTag(!(getEvent().getChannel().block() instanceof GuildChannel));
}
else if (name.equals("channel_name")) {
DenizenDiscordBot.userContextDeprecation.warn();
MessageChannel channel = getEvent().getChannel().block();
if (channel instanceof GuildChannel) {
return new ElementTag(((GuildChannel) channel).getName());
}
}
else if (name.equals("mention_names")) {
DenizenDiscordBot.userContextDeprecation.warn();
ListTag list = new ListTag();
for (User user : getEvent().getMessage().block().getUserMentions().toIterable()) {
list.add(String.valueOf(user.getUsername()));
}
return list;
}
else if (name.equals("group_name")) {
DenizenDiscordBot.userContextDeprecation.warn();
if (getEvent().getChannel().block() instanceof GuildChannel) {
return new ElementTag(((GuildChannel) getEvent().getChannel().block()).getGuild().block().getName());
}
}
else if (name.equals("author_id")) {
DenizenDiscordBot.userContextDeprecation.warn();
return new ElementTag(getEvent().getMessage().block().getAuthor().get().getId().asLong());
}
else if (name.equals("author_name")) {
DenizenDiscordBot.userContextDeprecation.warn();
return new ElementTag(getEvent().getMessage().block().getAuthor().get().getUsername());
}
return super.getContext(name);
}
@Override
public String getName() {
return "DiscordModifiedMessage";
}
}