Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cat-disruptor-6500
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Scarlett M
cat-disruptor-6500
Commits
67be0641
Commit
67be0641
authored
2 years ago
by
Stephen D
Browse files
Options
Downloads
Patches
Plain Diff
edit existing starboard messages
parent
c00d64cd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/handlers/mod.rs
+9
-2
9 additions, 2 deletions
src/handlers/mod.rs
src/handlers/starboard.rs
+119
-69
119 additions, 69 deletions
src/handlers/starboard.rs
with
128 additions
and
71 deletions
src/handlers/mod.rs
+
9
−
2
View file @
67be0641
...
...
@@ -35,7 +35,8 @@ pub(crate) trait LineHandler: Send + Sync {
#[async_trait]
pub
(
crate
)
trait
ReactionHandler
:
Send
+
Sync
{
async
fn
reaction
(
&
self
,
_ctx
:
&
Context
,
reaction
:
&
Reaction
);
async
fn
reaction_add
(
&
self
,
ctx
:
&
Context
,
reaction
:
&
Reaction
);
async
fn
reaction_del
(
&
self
,
ctx
:
&
Context
,
reaction
:
&
Reaction
);
}
pub
(
crate
)
struct
Dispatcher
{
...
...
@@ -53,7 +54,13 @@ impl EventHandler for Dispatcher {
async
fn
reaction_add
(
&
self
,
ctx
:
Context
,
reaction
:
Reaction
)
{
for
r
in
&
self
.reacts
{
r
.reaction
(
&
ctx
,
&
reaction
)
.await
;
r
.reaction_add
(
&
ctx
,
&
reaction
)
.await
;
}
}
async
fn
reaction_remove
(
&
self
,
ctx
:
Context
,
reaction
:
Reaction
)
{
for
r
in
&
self
.reacts
{
r
.reaction_del
(
&
ctx
,
&
reaction
)
.await
;
}
}
...
...
This diff is collapsed.
Click to expand it.
src/handlers/starboard.rs
+
119
−
69
View file @
67be0641
...
...
@@ -8,7 +8,9 @@ use bigdecimal::{BigDecimal, FromPrimitive, ToPrimitive};
use
diesel
::{
ExpressionMethods
,
PgConnection
,
QueryDsl
,
RunQueryDsl
};
use
serenity
::
async_trait
;
use
serenity
::
builder
::
CreateEmbed
;
use
serenity
::
model
::
prelude
::{
ChannelId
,
Emoji
,
EmojiId
,
Message
,
Reaction
,
ReactionType
};
use
serenity
::
model
::
prelude
::{
ChannelId
,
Emoji
,
EmojiId
,
Message
,
MessageReaction
,
Reaction
,
ReactionType
,
};
use
serenity
::
prelude
::
*
;
use
std
::
sync
::
Arc
;
use
tokio
::
sync
::
Mutex
;
...
...
@@ -53,80 +55,93 @@ impl SingleMessageHandler {
.context
(
"Could not convert emoji id to u64"
)
?
,
);
if
Self
::
emoji_match
(
&
reaction
.emoji
,
emoji
)
{
let
msg
=
reaction
.message
(
ctx
)
.await
?
;
for
mr
in
&
msg
.reactions
{
if
Self
::
emoji_match
(
&
mr
.reaction_type
,
emoji
)
{
let
guild_id
=
guild
.guild_id
;
let
emoji
=
guild_id
.emoji
(
ctx
,
emoji
)
.await
.context
(
"Could not get emoji from guild"
)
?
;
let
name
=
msg
.author
.nick_in
(
ctx
,
guild_id
)
.await
.unwrap_or_else
(||
msg
.author
.tag
());
let
image
=
msg
.attachments
.iter
()
.filter
(|
a
|
a
.width
.is_some
())
.map
(|
a
|
&
a
.url
)
.next
()
.cloned
();
let
handler
=
Self
{
conn
,
server_settings
:
gs
,
reaction_count
:
mr
.count
,
msg
,
emoji
,
name
,
image
,
};
handler
.process_match
(
ctx
)
.await
?
;
break
;
}
}
let
msg
=
reaction
.message
(
ctx
)
.await
?
;
// reaction from event handler must match
// otherwise we'll update the repost (and add an "edited"
// to the message)
// whenever someone reacts with any reaction
if
!
Self
::
emoji_match
(
&
reaction
.emoji
,
emoji
)
{
return
Ok
(());
}
let
reaction_count
=
Self
::
find_emoji_match
(
msg
.reactions
.iter
(),
emoji
);
let
guild_id
=
guild
.guild_id
;
let
emoji
=
guild_id
.emoji
(
ctx
,
emoji
)
.await
.context
(
"Could not get emoji from guild"
)
?
;
let
name
=
msg
.author
.nick_in
(
ctx
,
guild_id
)
.await
.unwrap_or_else
(||
msg
.author
.tag
());
let
image
=
msg
.attachments
.iter
()
.filter
(|
a
|
a
.width
.is_some
())
.map
(|
a
|
&
a
.url
)
.next
()
.cloned
();
let
handler
=
Self
{
conn
,
server_settings
:
gs
,
reaction_count
,
msg
,
emoji
,
name
,
image
,
};
handler
.process_match
(
ctx
)
.await
?
;
Ok
(())
}
async
fn
process_match
(
&
self
,
ctx
:
&
Context
)
->
anyhow
::
Result
<
()
>
{
if
self
.reaction_count
>=
self
.server_settings.starboard_threshold
as
u64
{
let
original_id
=
BigDecimal
::
from
(
self
.msg.id
.0
);
diesel
::
insert_into
(
starboard_mappings
::
dsl
::
starboard_mappings
)
.values
(
starboard_mappings
::
columns
::
original_id
.eq
(
&
original_id
))
.returning
(
starboard_mappings
::
columns
::
repost_id
)
.on_conflict_do_nothing
()
.execute
(
&*
self
.conn
.lock
()
.await
)
?
;
let
repost_id
=
starboard_mappings
::
dsl
::
starboard_mappings
.filter
(
starboard_mappings
::
columns
::
original_id
.eq
(
&
original_id
))
.select
(
starboard_mappings
::
columns
::
repost_id
)
.limit
(
1
)
.get_results
(
&*
self
.conn
.lock
()
.await
)
?
;
let
repost_id
:
&
Option
<
BigDecimal
>
=
repost_id
.get
(
0
)
.context
(
"Insert of mapping failed"
)
?
;
if
repost_id
.is_none
()
{
// post to repost
let
repost
=
self
.post_new_starboard_message
(
ctx
)
.await
?
;
// update the DB
let
repost_id
=
BigDecimal
::
from_u64
(
repost
);
diesel
::
update
(
starboard_mappings
::
dsl
::
starboard_mappings
.filter
(
starboard_mappings
::
columns
::
original_id
.eq
(
original_id
)),
let
original_id
=
BigDecimal
::
from
(
self
.msg.id
.0
);
diesel
::
insert_into
(
starboard_mappings
::
dsl
::
starboard_mappings
)
.values
(
starboard_mappings
::
columns
::
original_id
.eq
(
&
original_id
))
.returning
(
starboard_mappings
::
columns
::
repost_id
)
.on_conflict_do_nothing
()
.execute
(
&*
self
.conn
.lock
()
.await
)
?
;
let
repost_id
=
starboard_mappings
::
dsl
::
starboard_mappings
.filter
(
starboard_mappings
::
columns
::
original_id
.eq
(
&
original_id
))
.select
(
starboard_mappings
::
columns
::
repost_id
)
.limit
(
1
)
.get_results
(
&*
self
.conn
.lock
()
.await
)
?
;
let
repost_id
:
&
Option
<
BigDecimal
>
=
repost_id
.get
(
0
)
.context
(
"Insert of mapping failed"
)
?
;
match
repost_id
{
Some
(
id
)
=>
{
self
.edit_existing_starboard_message
(
ctx
,
id
.to_u64
()
.context
(
"Could not convert repost message id to a u64"
)
?
,
)
.set
(
starboard_mappings
::
columns
::
repost_id
.eq
(
repost_id
))
.execute
(
&*
self
.conn
.lock
()
.await
)
?
;
.await
?
;
}
None
=>
{
if
self
.reaction_count
>=
self
.server_settings.starboard_threshold
as
u64
{
// post to repost
let
repost
=
self
.post_new_starboard_message
(
ctx
)
.await
?
;
// update the DB
let
repost_id
=
BigDecimal
::
from_u64
(
repost
);
diesel
::
update
(
starboard_mappings
::
dsl
::
starboard_mappings
.filter
(
starboard_mappings
::
columns
::
original_id
.eq
(
original_id
)),
)
.set
(
starboard_mappings
::
columns
::
repost_id
.eq
(
repost_id
))
.execute
(
&*
self
.conn
.lock
()
.await
)
?
;
}
}
}
...
...
@@ -146,6 +161,25 @@ impl SingleMessageHandler {
Ok
(
repost
.id
.0
)
}
async
fn
edit_existing_starboard_message
(
&
self
,
ctx
:
&
Context
,
message_id
:
u64
,
)
->
anyhow
::
Result
<
()
>
{
let
channel_id
=
ChannelId
(
self
.server_settings
.starboard_channel
.to_u64
()
.context
(
"Could not convert starboard channel to a u64"
)
?
,
);
let
mut
msg
=
channel_id
.message
(
ctx
,
message_id
)
.await
?
;
msg
.edit
(
ctx
,
|
m
|
m
.embed
(|
e
|
self
.clone
()
.starboard_message
(
e
)))
.await
?
;
Ok
(())
}
fn
starboard_message
(
self
,
e
:
&
mut
CreateEmbed
)
->
&
mut
CreateEmbed
{
let
mut
e
=
e
.description
(
format!
(
...
...
@@ -164,6 +198,16 @@ impl SingleMessageHandler {
e
}
fn
find_emoji_match
<
'a
,
I
:
Iterator
<
Item
=
&
'a
MessageReaction
>>
(
iter
:
I
,
em
:
EmojiId
)
->
u64
{
for
mr
in
iter
{
if
Self
::
emoji_match
(
&
mr
.reaction_type
,
em
)
{
return
mr
.count
;
}
}
0
}
fn
emoji_match
(
rt
:
&
ReactionType
,
em
:
EmojiId
)
->
bool
{
matches!
(
rt
,
ReactionType
::
Custom
{
id
,
..
}
if
*
id
==
em
)
}
...
...
@@ -185,7 +229,13 @@ impl StarboardHandler {
#[async_trait]
impl
ReactionHandler
for
StarboardHandler
{
async
fn
reaction
(
&
self
,
ctx
:
&
Context
,
reaction
:
&
Reaction
)
{
async
fn
reaction_add
(
&
self
,
ctx
:
&
Context
,
reaction
:
&
Reaction
)
{
if
let
Err
(
e
)
=
self
.handle_reaction
(
ctx
,
reaction
)
.await
{
eprintln!
(
"Error in starboard: {:?}"
,
e
);
}
}
async
fn
reaction_del
(
&
self
,
ctx
:
&
Context
,
reaction
:
&
Reaction
)
{
if
let
Err
(
e
)
=
self
.handle_reaction
(
ctx
,
reaction
)
.await
{
eprintln!
(
"Error in starboard: {:?}"
,
e
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment