Skip to content

Commit

Permalink
Fixes missing direct message channels on message events
Browse files Browse the repository at this point in the history
  • Loading branch information
SinisterRectus committed Jan 21, 2023
1 parent 5154509 commit 9403396
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions libs/client/EventHandler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ local function checkReady(shard)
return client:emit('ready')
end

local function getChannel(client, id)
local guild = client._channel_map[id]
if guild then
return guild._text_channels:get(id)
else
return client._private_channels:get(id) or client._group_channels:get(id)
local function getChannel(client, d)
local channel = client:getChannel(d.channel_id)
if not channel and not d.guild_id then
channel = client._api:getChannel(d.channel_id)
if channel then
if channel.type == channelType.private then
channel = client._private_channels:_insert(channel)
elseif channel.type == channelType.group then
channel = client._group_channels:_insert(channel)
end
end
end
return channel
end

local EventHandler = setmetatable({}, {__index = function(self, k)
Expand Down Expand Up @@ -315,14 +321,14 @@ function EventHandler.GUILD_ROLE_DELETE(d, client) -- role object not provided
end

function EventHandler.MESSAGE_CREATE(d, client)
local channel = getChannel(client, d.channel_id)
local channel = getChannel(client, d)
if not channel then return warning(client, 'TextChannel', d.channel_id, 'MESSAGE_CREATE') end
local message = channel._messages:_insert(d)
return client:emit('messageCreate', message)
end

function EventHandler.MESSAGE_UPDATE(d, client) -- may not contain the whole message
local channel = getChannel(client, d.channel_id)
local channel = getChannel(client, d)
if not channel then return warning(client, 'TextChannel', d.channel_id, 'MESSAGE_UPDATE') end
local message = channel._messages:get(d.id)
if message then
Expand All @@ -335,7 +341,7 @@ function EventHandler.MESSAGE_UPDATE(d, client) -- may not contain the whole mes
end

function EventHandler.MESSAGE_DELETE(d, client) -- message object not provided
local channel = getChannel(client, d.channel_id)
local channel = getChannel(client, d)
if not channel then return warning(client, 'TextChannel', d.channel_id, 'MESSAGE_DELETE') end
local message = channel._messages:_delete(d.id)
if message then
Expand All @@ -346,7 +352,7 @@ function EventHandler.MESSAGE_DELETE(d, client) -- message object not provided
end

function EventHandler.MESSAGE_DELETE_BULK(d, client)
local channel = getChannel(client, d.channel_id)
local channel = getChannel(client, d)
if not channel then return warning(client, 'TextChannel', d.channel_id, 'MESSAGE_DELETE_BULK') end
for _, id in ipairs(d.ids) do
local message = channel._messages:_delete(id)
Expand All @@ -359,7 +365,7 @@ function EventHandler.MESSAGE_DELETE_BULK(d, client)
end

function EventHandler.MESSAGE_REACTION_ADD(d, client)
local channel = getChannel(client, d.channel_id)
local channel = getChannel(client, d)
if not channel then return warning(client, 'TextChannel', d.channel_id, 'MESSAGE_REACTION_ADD') end
local message = channel._messages:get(d.message_id)
if message then
Expand All @@ -372,7 +378,7 @@ function EventHandler.MESSAGE_REACTION_ADD(d, client)
end

function EventHandler.MESSAGE_REACTION_REMOVE(d, client)
local channel = getChannel(client, d.channel_id)
local channel = getChannel(client, d)
if not channel then return warning(client, 'TextChannel', d.channel_id, 'MESSAGE_REACTION_REMOVE') end
local message = channel._messages:get(d.message_id)
if message then
Expand All @@ -389,7 +395,7 @@ function EventHandler.MESSAGE_REACTION_REMOVE(d, client)
end

function EventHandler.MESSAGE_REACTION_REMOVE_ALL(d, client)
local channel = getChannel(client, d.channel_id)
local channel = getChannel(client, d)
if not channel then return warning(client, 'TextChannel', d.channel_id, 'MESSAGE_REACTION_REMOVE_ALL') end
local message = channel._messages:get(d.message_id)
if message then
Expand All @@ -407,7 +413,7 @@ function EventHandler.MESSAGE_REACTION_REMOVE_ALL(d, client)
end

function EventHandler.CHANNEL_PINS_UPDATE(d, client)
local channel = getChannel(client, d.channel_id)
local channel = getChannel(client, d)
if not channel then return warning(client, 'TextChannel', d.channel_id, 'CHANNEL_PINS_UPDATE') end
return client:emit('pinsUpdate', channel)
end
Expand Down

0 comments on commit 9403396

Please sign in to comment.