Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get last team messages in tickets #4440

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lib/sanbase/discord_bot/support_reminder.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule Sanbase.DiscordBot.SupportReminder do
def get_last_team_messages_in_threads do

guild_id = 334289660698427392
team_role_id = 409_637_386_012_721_155
general_support_channel_id = 1166232322803236905
subscription_support_channel_id = 1166255941944090644

{:ok, result} = Nostrum.Api.list_guild_threads(guild_id)

thread_channels =
Enum.filter(result[:threads], fn thread ->
thread.type == 12 and
thread.parent_id in [general_support_channel_id, subscription_support_channel_id]
end)

member_cache = %{}

Enum.map(thread_channels, fn thread_channel ->
{:ok, messages} = Nostrum.Api.get_channel_messages(thread_channel.id, 10)
{team_messages, _member_cache} =
Enum.reduce(messages, {[], member_cache}, fn message, {acc, cache} ->
user_id = message.author.id

{member, cache} =
case Map.get(cache, user_id) do
nil ->
# Fetch member from the guild
case Nostrum.Api.get_guild_member(guild_id, user_id) do
{:ok, member} -> {member, Map.put(cache, user_id, member)}
{:error, _reason} -> {nil, cache}
end

member ->
{member, cache}
end

# Check if the member has the @team role
if member && Enum.any?(member.roles, fn role_id -> role_id == team_role_id end) do
{[message | acc], cache}
else
{acc, cache}
end
end)

# Sort the messages by timestamp to find the last one
team_messages = Enum.sort_by(team_messages, & &1.timestamp)
last_team_message = List.last(team_messages)

{thread_channel, last_team_message}
end)
end
end