-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordMessageReceivedScriptEvent.java
142 lines (128 loc) · 4.96 KB
/
DiscordMessageReceivedScriptEvent.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
package com.denizenscript.ddiscordbot.events;
import net.aufdemrand.denizencore.events.ScriptEvent;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
import sx.blah.discord.handle.obj.IUser;
public class DiscordMessageReceivedScriptEvent extends ScriptEvent {
public static DiscordMessageReceivedScriptEvent instance;
// <--[event]
// @Events
// discord message received (by <bot>)
//
// @Regex ^on discord message received(by [^\s]+)?$
//
// @Triggers when a Discord bot receives a message.
//
// @Plugin dDiscordBot
//
// @Context
// <context.bot> returns the Denizen ID of the bot.
// <context.channel> returns the channel ID.
// <context.channel_name> returns the channel name.
// <context.group> returns the group ID.
// <context.group_name> returns the group name.
// <context.message> returns the message sent (raw).
// <context.no_mention_message> returns the message with all user mentions stripped.
// <context.formatted_message> returns the formatted message sent (mentions/etc. are written cleanly).
// <context.author_id> returns the author's internal ID.
// <context.author_name> returns the author's name.
// <context.mentions> returns a list of all mentioned user IDs.
// <context.mention_names> returns a list of all mentioned user names.
// <context.self> returns the bots own Discord user ID.
// <context.is_private> returns true if the message was received in a private channel.
//
// -->
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
return CoreUtilities.toLowerCase(s).startsWith("discord message received");
}
@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
if (lower.equals("discord message received")) {
return true;
}
else if (CoreUtilities.xthArgEquals(4, lower, botID)) {
return true;
}
return false;
}
public String botID;
public MessageReceivedEvent mre;
@Override
public dObject getContext(String name) {
if (name.equals("bot")) {
return new Element(botID);
}
else if (name.equals("self")) {
return new Element(mre.getClient().getOurUser().getLongID());
}
else if (name.equals("channel")) {
return new Element(mre.getChannel().getLongID());
}
else if (name.equals("channel_name")) {
return new Element(mre.getChannel().getName());
}
else if (name.equals("group")) {
return new Element(mre.getGuild().getLongID());
}
else if (name.equals("group_name")) {
return new Element(mre.getGuild().getName());
}
else if (name.equals("message")) {
return new Element(mre.getMessage().getContent());
}
else if (name.equals("no_mention_message")) {
String res = mre.getMessage().getContent();
for (IUser user : mre.getMessage().getMentions()) {
res = res.replace(user.mention(true), "")
.replace(user.mention(false), "");
}
return new Element(res);
}
else if (name.equals("formatted_message")) {
return new Element(mre.getMessage().getFormattedContent());
}
else if (name.equals("author_id")) {
return new Element(mre.getAuthor().getLongID());
}
else if (name.equals("author_name")) {
return new Element(mre.getAuthor().getName());
}
else if (name.equals("mentions")) {
dList list = new dList();
for (IUser user : mre.getMessage().getMentions()) {
list.add(String.valueOf(user.getLongID()));
}
return list;
}
else if (name.equals("mention_names")) {
dList list = new dList();
for (IUser user : mre.getMessage().getMentions()) {
list.add(String.valueOf(user.getName()));
}
return list;
}
else if (name.equals("is_private")) {
return new Element(mre.getChannel().isPrivate());
}
return super.getContext(name);
}
@Override
public String getName() {
return "DiscordMessageReceived";
}
boolean enab = false;
@Override
public void init() {
enab = true;
}
@Override
public void destroy() {
enab = false;
}
}