Skip to content

Commit

Permalink
Run on new nostrum member cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristgit committed May 6, 2023
1 parent 92f2fad commit 3f662c7
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 155 deletions.
4 changes: 2 additions & 2 deletions ansible/roles/bolt/templates/bolt.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ StateDirectory=bolt bolt/rrd
WorkingDirectory={{ bolt_directory }}/current
EnvironmentFile=/etc/opt/bolt/bolt.env

MemoryHigh=480M
MemoryMax=500M
MemoryHigh=180M
MemoryMax=200M
RemainAfterExit=no
Restart=on-failure
RestartSec=5
Expand Down
4 changes: 2 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ config :crow,
tables: [
:nostrum_channels,
:nostrum_guilds,
# :nostrum_members,
:nostrum_members,
:nostrum_users
]},
{CrowPlugins.BEAM.ETS,
Expand All @@ -41,7 +41,7 @@ config :crow,
tables: [
:nostrum_channels,
:nostrum_guilds,
# :nostrum_members,
:nostrum_members,
:nostrum_users
]},
CrowPlugins.BEAM.GarbageCollections,
Expand Down
8 changes: 2 additions & 6 deletions lib/bolt/cogs/assign.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Bolt.Cogs.Assign do
alias Bolt.{Converters, ErrorFormatters, Helpers, Humanizer, ModLog, Repo}
alias Nosedrum.Predicates
alias Nostrum.Api
alias Nostrum.Cache.GuildCache
alias Nostrum.Cache.MemberCache
require Logger

@impl true
Expand Down Expand Up @@ -116,11 +116,7 @@ defmodule Bolt.Cogs.Assign do
"🚫 no valid roles to be given - if you meant to assign a single role, " <>
"check your spelling. errors:\n#{errors |> Stream.map(&"• #{&1}") |> Enum.join("\n")}"
else
with {:ok, member} when member != nil <-
GuildCache.select(
msg.guild_id,
&Map.get(&1.members, msg.author.id)
),
with {:ok, member} <- MemberCache.get(msg.guild_id, msg.author.id),
{:ok, _member} <-
Api.modify_guild_member(msg.guild_id, msg.author.id,
roles: Enum.uniq(member.roles ++ Enum.map(selected_self_assignable_roles, & &1.id))
Expand Down
10 changes: 5 additions & 5 deletions lib/bolt/cogs/banrange.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Bolt.Cogs.BanRange do
alias Bolt.Paginator
alias Nosedrum.Predicates
alias Nostrum.Api
alias Nostrum.Cache.GuildCache
alias Nostrum.Cache.MemberCache
alias Nostrum.Struct.Embed
alias Nostrum.Struct.Guild
alias Nostrum.Struct.Message
Expand Down Expand Up @@ -53,8 +53,8 @@ defmodule Bolt.Cogs.BanRange do
case Integer.parse(lower) do
{start, ""} ->
msg.guild_id
|> GuildCache.select!(& &1.members)
|> Stream.filter(fn {flake, _member} -> flake >= start end)
|> MemberCache.get()
|> Stream.filter(fn %{user_id: id} -> id >= start end)
|> execute(msg.guild_id, msg.author, reason)
|> display(msg)

Expand All @@ -75,8 +75,8 @@ defmodule Bolt.Cogs.BanRange do
with {start, ""} <- Integer.parse(lower),
{stop, ""} <- Integer.parse(upper) do
msg.guild_id
|> GuildCache.select!(& &1.members)
|> Stream.filter(fn {flake, _member} -> flake >= start and flake <= stop end)
|> MemberCache.get()
|> Stream.filter(fn %{user_id: id} -> id >= start and id <= stop end)
|> execute(msg.guild_id, msg.author, reason)
|> display(msg)
else
Expand Down
13 changes: 6 additions & 7 deletions lib/bolt/cogs/infraction/detail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ defmodule Bolt.Cogs.Infraction.Detail do

alias Bolt.Cogs.Infraction.General
alias Nosedrum.Predicates
alias Bolt.{Constants, Helpers, Repo}
alias Bolt.{Constants, Helpers, Humanizer, Repo}
alias Bolt.Schema.Infraction
alias Nostrum.Api
alias Nostrum.Struct.Embed
alias Nostrum.Struct.Embed.{Field, Footer}
alias Nostrum.Struct.Message

@spec add_specific_fields(Embed.t(), Infraction) :: Embed.t()
defp add_specific_fields(embed, %Infraction{type: "temprole", data: data}) do
Expand Down Expand Up @@ -62,15 +61,15 @@ defmodule Bolt.Cogs.Infraction.Detail do
end
end

@spec format_detail(Message.t(), Infraction) :: Embed.t()
defp format_detail(msg, infraction) do
@spec format_detail(Infraction) :: Embed.t()
defp format_detail(infraction) do
%Embed{
title: "Infraction ##{infraction.id}",
color: Constants.color_blue(),
fields: [
%Field{
name: "User",
value: General.format_user(msg.guild_id, infraction.user_id),
value: Humanizer.human_user(infraction.user_id),
inline: true
},
%Field{
Expand All @@ -86,7 +85,7 @@ defmodule Bolt.Cogs.Infraction.Detail do
}
],
footer: %Footer{
text: "authored by #{General.format_user(msg.guild_id, infraction.actor_id)}"
text: "authored by #{Humanizer.human_user(infraction.actor_id)}"
}
}
|> add_specific_fields(infraction)
Expand Down Expand Up @@ -139,7 +138,7 @@ defmodule Bolt.Cogs.Infraction.Detail do
with {id, _} <- Integer.parse(maybe_id),
infraction when infraction != nil <-
Repo.get_by(Infraction, id: id, guild_id: msg.guild_id) do
embed = format_detail(msg, infraction)
embed = format_detail(infraction)
{:ok, _msg} = Api.create_message(msg.channel_id, embed: embed)
else
nil ->
Expand Down
20 changes: 0 additions & 20 deletions lib/bolt/cogs/infraction/general.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
defmodule Bolt.Cogs.Infraction.General do
@moduledoc "General utilities used across the infraction subcommands."

alias Bolt.Humanizer
alias Nostrum.Cache.GuildCache
alias Nostrum.Cache.UserCache

@type_emojis %{
"note" => "📔",
"tempmute" => "🔇⏲",
Expand All @@ -25,20 +21,4 @@ defmodule Bolt.Cogs.Infraction.General do
def emoji_for_type(type) do
Map.get(@type_emojis, type, "?")
end

@spec format_user(Nostrum.Struct.Snowflake.t(), Nostrum.Struct.Snowflake.t()) :: String.t()
def format_user(guild_id, user_id) do
default_string = "unknown user (`#{user_id}`)"

case UserCache.get(user_id) do
{:ok, user} ->
Humanizer.human_user(user)

{:error, _reason} ->
case GuildCache.get(guild_id) do
{:ok, guild} -> Map.get(guild.members, user_id, default_string)
{:error, _reason} -> default_string
end
end
end
end
4 changes: 2 additions & 2 deletions lib/bolt/cogs/infraction/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Bolt.Cogs.Infraction.User do

alias Bolt.Cogs.Infraction.General
alias Nosedrum.Predicates
alias Bolt.{Constants, Helpers, Paginator, Repo}
alias Bolt.{Constants, Helpers, Humanizer, Paginator, Repo}
alias Bolt.Schema.Infraction
alias Nostrum.Api
alias Nostrum.Struct.Embed
Expand Down Expand Up @@ -41,7 +41,7 @@ defmodule Bolt.Cogs.Infraction.User do

queryset = Repo.all(query)

user_string = General.format_user(msg.guild_id, user_id)
user_string = Humanizer.human_user(user_id)

base_embed = %Embed{
title: "Infractions for #{user_string}",
Expand Down
57 changes: 30 additions & 27 deletions lib/bolt/cogs/inrole.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
defmodule Bolt.Cogs.InRole do
@moduledoc "Shows members in the given role."
@maximum_fetched 500

@behaviour Nosedrum.Command

alias Bolt.{Constants, Converters, ErrorFormatters, Paginator}
alias Nosedrum.Predicates
alias Nostrum.Api
alias Nostrum.Cache.GuildCache
alias Nostrum.Cache.MemberCache
alias Nostrum.Struct.{Embed, User}

@impl true
Expand All @@ -17,6 +18,8 @@ defmodule Bolt.Cogs.InRole do
do: """
Show members in the given role. The converter is case-insensitive.
The command is capped to show at most #{@maximum_fetched} members.
**Example**:
```rs
// Show members in the 'Muted' role
Expand All @@ -37,34 +40,34 @@ defmodule Bolt.Cogs.InRole do
end

def command(msg, role_string) do
with {:ok, role} <- Converters.to_role(msg.guild_id, role_string, true),
{:ok, members} <-
GuildCache.select(
msg.guild_id,
fn guild ->
guild.members
|> Map.values()
|> Enum.filter(&(role.id in &1.roles))
end
) do
base_embed = %Embed{
title: "Members with role `#{role.name}` (`#{length(members)}` total)",
color: Constants.color_blue()
}
case Converters.to_role(msg.guild_id, role_string, true) do
{:ok, role} ->
members =
msg.guild_id
|> MemberCache.get_with_users()
|> Stream.filter(fn {member, _user} -> role.id in member.roles end)

base_embed = %Embed{
title: "Members with role `#{role.name}` (`#{length(members)}` total)",
color: Constants.color_blue()
}

pages =
members
|> Enum.take(@maximum_fetched)
|> Enum.sort_by(fn {_member, user} -> String.downcase(user.username) end)
|> Stream.map(fn {_member, user} ->
"#{User.full_name(user)} (#{User.mention(user)})"
end)
|> Stream.chunk_every(25)
|> Enum.map(fn mention_chunk ->
%Embed{
description: Enum.join(mention_chunk, ", ")
}
end)

pages =
members
|> Enum.sort_by(&String.downcase(&1.user.username))
|> Stream.map(&"#{User.full_name(&1.user)} (#{User.mention(&1.user)})")
|> Stream.chunk_every(25)
|> Enum.map(fn mention_chunk ->
%Embed{
description: Enum.join(mention_chunk, ", ")
}
end)
Paginator.paginate_over(msg, base_embed, pages)

Paginator.paginate_over(msg, base_embed, pages)
else
error ->
response = ErrorFormatters.fmt(msg, error)
{:ok, _msg} = Api.create_message(msg.channel_id, response)
Expand Down
3 changes: 1 addition & 2 deletions lib/bolt/cogs/lastjoins.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ defmodule Bolt.Cogs.LastJoins do
@impl true
def command(msg, {options, _args, []}) do
{limit, options} = Keyword.pop_first(options, :total, @default_shown)
total_members = MemberCache.get(msg.guild_id) |> Enum.count()

most_recent_members =
msg.guild_id
Expand Down Expand Up @@ -176,7 +175,7 @@ defmodule Bolt.Cogs.LastJoins do
key when size < limit ->
{:gb_trees.insert(key, item, tree), key, size + 1}

key ->
_key ->
{tree, smallest, size}
end
end)
Expand Down
18 changes: 7 additions & 11 deletions lib/bolt/cogs/memberinfo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Bolt.Cogs.MemberInfo do
alias Bolt.{Converters, Helpers}
alias Nosedrum.Predicates
alias Nostrum.Api
alias Nostrum.Cache.GuildCache
alias Nostrum.Cache.MemberCache
alias Nostrum.Snowflake
alias Nostrum.Struct.{Embed, User}

Expand Down Expand Up @@ -72,17 +72,13 @@ defmodule Bolt.Cogs.MemberInfo do

@impl true
def command(msg, "") do
with {:ok, guild} <- GuildCache.get(msg.guild_id),
member when member != nil <- Map.get(guild.members, msg.author.id) do
embed = format_member_info(msg.guild_id, member)
{:ok, _msg} = Api.create_message(msg.channel_id, embed: embed)
else
nil ->
response = "❌ failed to find you in this guild's members - that's a bit weird"
{:ok, _msg} = Api.create_message(msg.channel_id, response)
case MemberCache.get(msg.guild_id, msg.author.id) do
{:ok, member} ->
embed = format_member_info(msg.guild_id, member)
{:ok, _msg} = Api.create_message(msg.channel_id, embed: embed)

{:error, reason} ->
response = "❌ error: #{Helpers.clean_content(reason)}"
{:error, _reason} ->
response = "❌ failed to find you in this guild's members - that's a bit weird"
{:ok, _msg} = Api.create_message(msg.channel_id, response)
end
end
Expand Down
16 changes: 5 additions & 11 deletions lib/bolt/cogs/roleinfo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Bolt.Cogs.RoleInfo do
alias Bolt.{Converters, Helpers}
alias Nosedrum.Predicates
alias Nostrum.Api
alias Nostrum.Cache.GuildCache
alias Nostrum.Cache.MemberCache
alias Nostrum.Snowflake
alias Nostrum.Struct.Guild.Role
alias Nostrum.Struct.{Embed, Guild}
Expand Down Expand Up @@ -93,15 +93,9 @@ defmodule Bolt.Cogs.RoleInfo do

@spec count_role_members(Role.id(), Guild.id()) :: String.t()
defp count_role_members(role_id, guild_id) do
case GuildCache.get(guild_id) do
{:ok, guild} ->
guild.members
|> Map.values()
|> Enum.count(&(role_id in &1.roles))
|> Integer.to_string()

_error ->
"*unknown, guild not cached*"
end
guild_id
|> MemberCache.get()
|> Enum.count(&(role_id in &1.roles))
|> Integer.to_string()
end
end
14 changes: 4 additions & 10 deletions lib/bolt/cogs/roles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Bolt.Cogs.Roles do
alias Nosedrum.Predicates
alias Nostrum.Api
alias Nostrum.Cache.GuildCache
alias Nostrum.Cache.MemberCache
alias Nostrum.Struct.Embed
alias Nostrum.Struct.Guild.Role

Expand Down Expand Up @@ -126,16 +127,9 @@ defmodule Bolt.Cogs.Roles do
@spec sort_key(sort_by :: String.t(), role :: Role.t(), guild_id :: Guild.id()) ::
non_neg_integer() | String.t()
defp sort_key("members", role, guild_id) do
selector = fn guild ->
guild.members
|> Map.values()
|> Enum.count(&(role.id in &1.roles))
end

case GuildCache.select(guild_id, selector) do
{:ok, count} -> count
{:error, _why} -> role.name
end
guild_id
|> MemberCache.get()
|> Enum.count(&(role.id in &1.roles))
end

defp sort_key("name", role, _guild_id), do: role.name
Expand Down
6 changes: 3 additions & 3 deletions lib/bolt/cogs/tag/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Bolt.Cogs.Tag.Info do
alias Bolt.{Helpers, Repo}
alias Nosedrum.Predicates
alias Nostrum.Api
alias Nostrum.Cache.GuildCache
alias Nostrum.Cache.MemberCache
alias Nostrum.Struct.{Embed, User}
import Ecto.Query, only: [from: 2]

Expand Down Expand Up @@ -48,8 +48,8 @@ defmodule Bolt.Cogs.Tag.Info do

[tag] ->
creator_string =
case GuildCache.select(msg.guild_id, &Map.get(&1.members, tag.author_id)) do
{:ok, author} when author != nil -> User.mention(author.user)
case MemberCache.get(msg.guild_id, tag.author_id) do
{:ok, author} -> User.mention(author.user)
_error -> "unknown user (`#{tag.author_id}`)"
end

Expand Down
Loading

0 comments on commit 3f662c7

Please # to comment.