BlakeWilliams / Elixir-Slack

Slack real time messaging and web API client in Elixir
MIT License
674 stars 183 forks source link

Fix channel_joined access to channel.members #240

Closed ashkan18 closed 3 years ago

ashkan18 commented 3 years ago

Problem

When listening on channel_joined event, the code crashes before even getting to the method on Slack.State when trying to access channel.members.

20:49:31.040 [error] ** Websocket client Slack.Bot terminating in :websocket_handle/3
   for the reason :error:{:badkey, :members,

Cause

Looking at what channel is at that point, there is no member attribute:

%{
  reated: 1605533620,
  creator: "***",
  id: "***",
  is_archived: false,
  is_channel: true,
  is_ext_shared: false,
  is_frozen: false,
  is_general: false,
  is_group: false,
  is_im: false,
  is_member: true,
  is_mpim: false,
  is_org_shared: false,
  is_pending_ext_shared: false,
  is_private: false,
  is_shared: false,
  last_read: "1605664021.022400",
  latest: %{subtype: "channel_leave",
  text: "<@"***"> has left the channel",
  ts: ""***",",
  type: "message",
  user: "***"},
  name: "fikabot-support",
  name_normalized: "fikabot-support",
  parent_conversation: nil,
  pending_connected_team_ids: [],
  pending_shared: [],
  previous_names: [],
  priority: 0,
  purpose: %{creator: "***",
  last_set: 1605533621,
  value: "Join if you have questions about fikabot or you’d like to contribute"},
  shared_team_ids: ["***"],
  topic: %{creator: "",
  last_set: 0,
  value: ""},
  unlinked: 0,
  unread_count: 0,
  unread_count_display: 0
}

Possible Solution

Ended up taking members out and also since this bot just joined the channel, added channel before setting is_member to true.