Skip to content

Commit

Permalink
Merge pull request #69 from Humans-of-Julia/tk/reactor-updates1
Browse files Browse the repository at this point in the history
Reactor updates
  • Loading branch information
rmsrosa authored May 1, 2021
2 parents b6b070b + 365350a commit adf06d8
Showing 1 changed file with 58 additions and 8 deletions.
66 changes: 58 additions & 8 deletions src/handler/reaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,42 @@ const NO_REACTION = Char[]
# Reactors
# ----------------------------------------------------------------------

const REACT_WORDS = Dict(
:happy => [
"happy", "nice", "great", "awesome", "cheers", "yay",
"congrat", "congrats", "congratulations",
"it helped", "it helps", "appreciate", "appreciated", "noice",
"thank", "thanks",
],
:disappointed => [
"disappointed", "unhappy", "sad", "aw shucks", "yeow",
],
:excited => [
"excited", "fantastic", "fabulous", "wonderful",
"looking forward to", "love", "learned", "saved me",
"beautiful"
],
:goodbye => [
"cya", "bye", "goodbye", "ciao", "adios", "brb"
],
:dog => ["dog", "dogs", "doggie", "shiba", "corgi", "chihuahua", "retriever"],
:cat => ["cat", "cats", "feline", "kitten", "kittens"],
:snake => ["snake", "snakes", "rattle", "python", "pythons"],
:crab => ["crab", "crabs", "rust"],
)

function contains_any(s::AbstractString, words::AbstractVector{String})
is_thing(x) = x !== nothing
regexes = [Regex("\\b" * w * "\\b") for w in words]
matches = match.(regexes, lowercase(s))
return any(is_thing(x) for x in matches)
end

struct HappyReactor <: AbstractReactor end

function reactions(::HappyReactor, m::Message)
words = ["happy", "nice", "great", "awesome", "cheers", "yay", "yayy", "congratulations", "it helped", "appriciate", "noice", "thanks"]
if any(occursin.(words, m.content))
if contains_any(m.content, REACT_WORDS[:happy]) &&
!contains_any(m.content, REACT_WORDS[:disappointed])
return ['πŸ˜„']
end
return NO_REACTION
Expand All @@ -32,8 +63,9 @@ end
struct DisappointedReactor <: AbstractReactor end

function reactions(::DisappointedReactor, m::Message)
words = ["disappointed", "unhappy", "sad", "aw shucks", "yeow"]
if any(occursin.(words, m.content))
if contains_any(m.content, REACT_WORDS[:disappointed]) &&
!contains_any(m.content, REACT_WORDS[:happy]) &&
!contains_any(m.content, REACT_WORDS[:excited])
return ['😞']
end
return NO_REACTION
Expand All @@ -42,8 +74,8 @@ end
struct ExcitedReactor <: AbstractReactor end

function reactions(::ExcitedReactor, m::Message)
words = ["excited", "fantastic", "fabulous", "wonderful", "looking forward to", "love", "learn", "julia", "saved me", "beautiful"]
if any(occursin.(words, m.content))
if contains_any(m.content, REACT_WORDS[:excited]) &&
!contains_any(m.content, REACT_WORDS[:disappointed])
return ['🀩']
end
return NO_REACTION
Expand All @@ -52,13 +84,30 @@ end
struct GoodbyeReactor <: AbstractReactor end

function reactions(::GoodbyeReactor, m::Message)
words = ["cya", "bye", "goodbye", "ciao", "adios"]
if any(occursin.(words, m.content))
if contains_any(m.content, REACT_WORDS[:goodbye])
return ['πŸ‘‹']
end
return NO_REACTION
end

struct AnimalReactor <: AbstractReactor end

function reactions(::AnimalReactor, m::Message)
result = Char[]
if contains_any(m.content, REACT_WORDS[:dog])
push!(result, 'πŸ•')
end
if contains_any(m.content, REACT_WORDS[:cat])
push!(result, '🐈')
end
if contains_any(m.content, REACT_WORDS[:snake])
push!(result, '🐍')
end
if contains_any(m.content, REACT_WORDS[:crab])
push!(result, 'πŸ¦€')
end
return result
end

# ----------------------------------------------------------------------
# Main logic
Expand All @@ -69,6 +118,7 @@ const REACTORS = AbstractReactor[
DisappointedReactor(),
ExcitedReactor(),
GoodbyeReactor(),
AnimalReactor(),
]

function handler(c::Client, e::MessageCreate, ::Val{:reaction})
Expand Down

0 comments on commit adf06d8

Please sign in to comment.