Skip to content

Commit

Permalink
Merge pull request #21 from StampyAI/dev
Browse files Browse the repository at this point in the history
refactor: move events to dedicated folder
  • Loading branch information
ProducerMatt authored Jun 27, 2024
2 parents 685373c + 2d4e64f commit d81450b
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 89 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ erl_crash.dump
/eflame

/.pre-commit-config.yaml

/.direnv
18 changes: 9 additions & 9 deletions bench/plugin_testing.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alias Stampede, as: S
require Stampede.MsgReceived
require Stampede.Events.MsgReceived
require Aja

defmodule T do
Expand Down Expand Up @@ -177,26 +177,26 @@ defmodule T do
msg =
case single_msg do
Aja.vec([:ping]) ->
S.MsgReceived.new(
MsgReceived.new(
body: "!ping",
server_id: server_id,
author_id: user_id,
channel_id: channel_id,
id: msg_id,
service: Services.Dummy
)
|> S.MsgReceived.add_context(cfg)
|> MsgReceived.add_context(cfg)

Aja.vec([:unrelated]) ->
S.MsgReceived.new(
MsgReceived.new(
body: "lololol",
server_id: server_id,
author_id: user_id,
channel_id: channel_id,
id: msg_id,
service: Services.Dummy
)
|> S.MsgReceived.add_context(cfg)
|> MsgReceived.add_context(cfg)
end

{cfg, msg}
Expand Down Expand Up @@ -230,29 +230,29 @@ defmodule T do
|> Aja.Vector.map_reduce(0, fn
:ping, i ->
{
S.MsgReceived.new(
MsgReceived.new(
body: "!ping",
server_id: server_id,
author_id: user_id,
channel_id: channel_id,
id: i,
service: Services.Dummy
)
|> S.MsgReceived.add_context(cfg),
|> MsgReceived.add_context(cfg),
i + 1
}

:unrelated, i ->
{
S.MsgReceived.new(
MsgReceived.new(
body: "lololol",
server_id: server_id,
author_id: user_id,
channel_id: channel_id,
id: i,
service: Services.Dummy
)
|> S.MsgReceived.add_context(cfg),
|> MsgReceived.add_context(cfg),
i + 1
}
end)
Expand Down
2 changes: 1 addition & 1 deletion bench/prefix_checking.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alias Stampede, as: S
require Stampede.MsgReceived
require Stampede.Events.MsgReceived
require Aja

defmodule T do
Expand Down
2 changes: 1 addition & 1 deletion bench/prefix_conflicts.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alias Stampede, as: S
require Stampede.MsgReceived
require Stampede.Events.MsgReceived
require Aja

defmodule T do
Expand Down
2 changes: 1 addition & 1 deletion bench/str_split_regex.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alias Stampede, as: S
require Stampede.MsgReceived
require Stampede.Events.MsgReceived
require Aja

defmodule T do
Expand Down
18 changes: 9 additions & 9 deletions bench/traceback_storage.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alias Stampede, as: S
require Stampede.MsgReceived
require Stampede.Events.MsgReceived
require Aja

defmodule T do
Expand Down Expand Up @@ -145,26 +145,26 @@ defmodule T do
msg =
case single_msg do
Aja.vec([:ping]) ->
S.MsgReceived.new(
MsgReceived.new(
body: "!ping",
server_id: server_id,
author_id: user_id,
channel_id: channel_id,
id: msg_id,
service: Services.Dummy
)
|> S.MsgReceived.add_context(cfg)
|> MsgReceived.add_context(cfg)

Aja.vec([:unrelated]) ->
S.MsgReceived.new(
MsgReceived.new(
body: "lololol",
server_id: server_id,
author_id: user_id,
channel_id: channel_id,
id: msg_id,
service: Services.Dummy
)
|> S.MsgReceived.add_context(cfg)
|> MsgReceived.add_context(cfg)
end

{cfg, msg}
Expand Down Expand Up @@ -198,29 +198,29 @@ defmodule T do
|> Aja.Vector.map_reduce(0, fn
:ping, i ->
{
S.MsgReceived.new(
MsgReceived.new(
body: "!ping",
server_id: server_id,
author_id: user_id,
channel_id: channel_id,
id: i,
service: Services.Dummy
)
|> S.MsgReceived.add_context(cfg),
|> MsgReceived.add_context(cfg),
i + 1
}

:unrelated, i ->
{
S.MsgReceived.new(
MsgReceived.new(
body: "lololol",
server_id: server_id,
author_id: user_id,
channel_id: channel_id,
id: i,
service: Services.Dummy
)
|> S.MsgReceived.add_context(cfg),
|> MsgReceived.add_context(cfg),
i + 1
}
end)
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
yamlfmt = enable_on_commit;
convco = {
enable = true;
stages = ["manual" "pre-commit"];
stages = ["commit-msg"];
};
deadnix.enable = true;

Expand Down
16 changes: 8 additions & 8 deletions lib/plugin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ defmodule Plugin do
require PluginCrashInfo
alias PluginCrashInfo, as: CrashInfo
alias Stampede, as: S
alias S.{MsgReceived, ResponseToPost, InteractionForm}
require InteractionForm
alias S.Events.{MsgReceived, ResponseToPost, InteractionWanted}
require InteractionWanted
require Aja
@first_response_timeout 500

Expand Down Expand Up @@ -125,7 +125,7 @@ defmodule Plugin do
@type! plugin_job_result :: {module(), job_result()}

@doc "Attempt some task, safely catch errors, and format the error report for the originating service"
@spec! get_response(S.module_function_args(), SiteConfig.t(), S.MsgReceived.t()) ::
@spec! get_response(S.module_function_args(), SiteConfig.t(), MsgReceived.t()) ::
job_result()
def get_response({m, f, a}, cfg, msg) do
# if an error occurs in process_msg, catch it and return as data
Expand Down Expand Up @@ -168,7 +168,7 @@ defmodule Plugin do
nonempty_list(module() | S.module_function_args())
| MapSet.t(module() | S.module_function_args()),
SiteConfig.t(),
S.MsgReceived.t()
MsgReceived.t()
) ::
nil | {response :: ResponseToPost.t(), interaction_id :: S.interaction_id()}
def query_plugins(call_list, cfg, msg) do
Expand Down Expand Up @@ -250,7 +250,7 @@ defmodule Plugin do
{plug, result} ->
case result do
r = {:job_ok, return} ->
if is_struct(return, S.ResponseToPost) and plug != return.origin_plug do
if is_struct(return, ResponseToPost) and plug != return.origin_plug do
raise(
"Plug #{plug} doesn't match #{return.origin_plug}. I screwed up the task running code."
)
Expand All @@ -273,7 +273,7 @@ defmodule Plugin do
# we have a response to immediately provide
%ResponseToPost{callback: nil} ->
{:ok, iid} =
S.InteractionForm.new(
InteractionWanted.new(
service: cfg.service,
plugin: chosen_response.origin_plug,
msg: msg,
Expand Down Expand Up @@ -304,7 +304,7 @@ defmodule Plugin do
@doc "Poll all enabled plugins and choose the most relevant one."
@spec! get_top_response(SiteConfig.t(), MsgReceived.t()) ::
nil | {response :: ResponseToPost.t(), interaction_id :: S.interaction_id()}
def get_top_response(cfg, msg = %S.MsgReceived{}) do
def get_top_response(cfg, msg = %MsgReceived{}) do
case S.Interact.channel_locked?(msg.channel_id) do
{{m, f, args_without_msg}, _plugin, _iid} ->
{response, iid} = query_plugins([{m, f, [msg | args_without_msg]}], cfg, msg)
Expand Down Expand Up @@ -369,7 +369,7 @@ defmodule Plugin do
@doc "Choose best response, creating a traceback along the way."
@spec! resolve_responses(nonempty_list(plugin_job_result())) :: %{
# NOTE: reversing order from 'nil | response' to 'response | nil' makes Dialyzer not count nil?
r: nil | S.ResponseToPost.t(),
r: nil | ResponseToPost.t(),
tb: S.Traceback.t()
}
def resolve_responses(tlist) do
Expand Down
11 changes: 6 additions & 5 deletions lib/plugins/help.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ defmodule Plugins.Help do
require Logger
use TypeCheck
alias Stampede, as: S
require S.ResponseToPost
alias S.Events.{ResponseToPost, MsgReceived}
require ResponseToPost
use Plugin

# TODO: make all except ping only respond to admins
Expand All @@ -22,7 +23,7 @@ defmodule Plugins.Help do
end

@impl Plugin
@spec! respond(SiteConfig.t(), S.MsgReceived.t()) :: nil | S.ResponseToPost.t()
@spec! respond(SiteConfig.t(), MsgReceived.t()) :: nil | ResponseToPost.t()
def respond(_cfg, msg) when not Plugin.is_bot_invoked(msg), do: nil

def respond(cfg, msg) when Plugin.is_bot_invoked(msg) do
Expand Down Expand Up @@ -52,7 +53,7 @@ defmodule Plugins.Help do
end)}
]

S.ResponseToPost.new(
ResponseToPost.new(
confidence: 10,
text: txt,
origin_msg_id: msg.id,
Expand All @@ -67,7 +68,7 @@ defmodule Plugins.Help do
end)
|> case do
nil ->
S.ResponseToPost.new(
ResponseToPost.new(
confidence: 10,
text: [
"Couldn't find a module named #{requested_name}. Possible modules: ",
Expand All @@ -78,7 +79,7 @@ defmodule Plugins.Help do
)

found ->
S.ResponseToPost.new(
ResponseToPost.new(
confidence: 10,
text: [
found.description_long(),
Expand Down
9 changes: 5 additions & 4 deletions lib/plugins/sentience.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule Plugins.Sentience do
@moduledoc false
use TypeCheck
alias Stampede, as: S
require S.ResponseToPost
alias S.Events.{ResponseToPost, MsgReceived}
require ResponseToPost

use Plugin

Expand All @@ -19,11 +20,11 @@ defmodule Plugins.Sentience do
end

@impl Plugin
@spec! respond(SiteConfig.t(), S.MsgReceived.t()) :: nil | S.ResponseToPost.t()
@spec! respond(SiteConfig.t(), MsgReceived.t()) :: nil | ResponseToPost.t()
def respond(_cfg, msg) when not Plugin.is_bot_invoked(msg), do: nil

def respond(_cfg, msg = %S.MsgReceived{id: msg_id}) when Plugin.is_bot_invoked(msg) do
S.ResponseToPost.new(
def respond(_cfg, msg = %MsgReceived{id: msg_id}) when Plugin.is_bot_invoked(msg) do
ResponseToPost.new(
confidence: 1,
text: S.confused_response(),
origin_msg_id: msg_id,
Expand Down
Loading

0 comments on commit d81450b

Please sign in to comment.