-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
MessageCreateHandler.java
128 lines (121 loc) · 4.9 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
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* 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.hooks.IEventManager;
import net.dv8tion.jda.core.requests.WebSocketClient;
import org.json.JSONObject;
public class MessageCreateHandler extends SocketHandler
{
public MessageCreateHandler(JDAImpl api)
{
super(api);
}
@Override
protected Long handleInternally(JSONObject content)
{
MessageType type = MessageType.fromId(content.getInt("type"));
if (type == MessageType.UNKNOWN)
{
WebSocketClient.LOG.debug("JDA received a message of unknown type. Type: {} JSON: {}", type, content);
return null;
}
Message message;
try
{
message = getJDA().getEntityBuilder().createMessage(content, true);
}
catch (IllegalArgumentException e)
{
switch (e.getMessage())
{
case EntityBuilder.MISSING_CHANNEL:
{
final long channelId = content.getLong("channel_id");
getJDA().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");
getJDA().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;
}
}
final IEventManager manager = getJDA().getEventManager();
switch (message.getChannelType())
{
case TEXT:
{
TextChannelImpl channel = (TextChannelImpl) message.getTextChannel();
if (getJDA().getGuildLock().isLocked(channel.getGuild().getIdLong()))
{
return channel.getGuild().getIdLong();
}
channel.setLastMessageId(message.getIdLong());
manager.handle(
new GuildMessageReceivedEvent(
getJDA(), responseNumber,
message));
break;
}
case PRIVATE:
{
PrivateChannelImpl channel = (PrivateChannelImpl) message.getPrivateChannel();
channel.setLastMessageId(message.getIdLong());
manager.handle(
new PrivateMessageReceivedEvent(
getJDA(), responseNumber,
message));
break;
}
case GROUP:
{
GroupImpl channel = (GroupImpl) message.getGroup();
channel.setLastMessageId(message.getIdLong());
manager.handle(
new GroupMessageReceivedEvent(
getJDA(), responseNumber,
message));
break;
}
default:
WebSocketClient.LOG.warn("Received a MESSAGE_CREATE with a unknown MessageChannel ChannelType. JSON: {}", content);
return null;
}
//Combo event
manager.handle(
new MessageReceivedEvent(
getJDA(), responseNumber,
message));
return null;
}
}