/
MessageCreateHandler.java
149 lines (140 loc) · 5.71 KB
/
MessageCreateHandler.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
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.handle;
import net.dv8tion.jda.client.entities.impl.GroupImpl;
import net.dv8tion.jda.client.events.message.group.GroupMessageReceivedEvent;
import net.dv8tion.jda.core.entities.EntityBuilder;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageType;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import net.dv8tion.jda.core.entities.impl.PrivateChannelImpl;
import net.dv8tion.jda.core.entities.impl.TextChannelImpl;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.core.events.message.priv.PrivateMessageReceivedEvent;
import net.dv8tion.jda.core.requests.GuildLock;
import net.dv8tion.jda.core.requests.WebSocketClient;
import org.json.JSONObject;
public class MessageCreateHandler extends SocketHandler
{
//private static final Pattern invitePattern = Pattern.compile("\\bhttps://(?:www\\.)?discord(?:\\.gg|app\\.com/invite)/([a-zA-Z0-9-]+)\\b");
public MessageCreateHandler(JDAImpl api)
{
super(api);
}
@Override
protected Long handleInternally(JSONObject content)
{
MessageType type = MessageType.fromId(content.getInt("type"));
switch (type)
{
case DEFAULT:
return handleDefaultMessage(content);
default:
WebSocketClient.LOG.debug("JDA received a message of unknown type. Type: " + type + " JSON: " + content);
}
return null;
}
private Long handleDefaultMessage(JSONObject content)
{
Message message;
try
{
message = api.getEntityBuilder().createMessage(content, true);
}
catch (IllegalArgumentException e)
{
switch (e.getMessage())
{
case EntityBuilder.MISSING_CHANNEL:
{
final long channelId = content.getLong("channel_id");
api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a message for a channel that JDA does not currently have cached");
return null;
}
case EntityBuilder.MISSING_USER:
{
final long authorId = content.getJSONObject("author").getLong("id");
api.getEventCache().cache(EventCache.Type.USER, authorId, () -> handle(responseNumber, allContent));
EventCache.LOG.debug("Received a message for a user that JDA does not currently have cached");
return null;
}
default:
throw e;
}
}
switch (message.getChannelType())
{
case TEXT:
{
TextChannelImpl channel = (TextChannelImpl) message.getTextChannel();
if (api.getGuildLock().isLocked(channel.getGuild().getIdLong()))
{
return channel.getGuild().getIdLong();
}
channel.setLastMessageId(message.getIdLong());
api.getEventManager().handle(
new GuildMessageReceivedEvent(
api, responseNumber,
message));
break;
}
case PRIVATE:
{
PrivateChannelImpl channel = (PrivateChannelImpl) message.getPrivateChannel();
channel.setLastMessageId(message.getIdLong());
api.getEventManager().handle(
new PrivateMessageReceivedEvent(
api, responseNumber,
message));
break;
}
case GROUP:
{
GroupImpl channel = (GroupImpl) message.getGroup();
channel.setLastMessageId(message.getIdLong());
api.getEventManager().handle(
new GroupMessageReceivedEvent(
api, responseNumber,
message));
break;
}
default:
WebSocketClient.LOG.warn("Received a MESSAGE_CREATE with a unknown MessageChannel ChannelType. JSON: " + content);
return null;
}
//Combo event
api.getEventManager().handle(
new MessageReceivedEvent(
api, responseNumber,
message));
// //searching for invites
// Matcher matcher = invitePattern.matcher(message.getContent());
// while (matcher.find())
// {
// InviteUtil.Invite invite = InviteUtil.resolve(matcher.group(1));
// if (invite != null)
// {
// api.getEventManager().handle(
// new InviteReceivedEvent(
// api, responseNumber,
// message,invite));
// }
// }
return null;
}
}