1- use std:: { collections:: HashMap , rc:: Rc , sync:: Arc } ;
1+ use std:: {
2+ collections:: HashMap , rc:: Rc , sync:: { Arc , OnceLock }
3+ } ;
24
35use serenity:: {
46 all:: { Context , EventHandler , GatewayIntents , Message } ,
@@ -18,47 +20,48 @@ use crate::{
1820
1921#[ derive( Default ) ]
2022pub struct DiscordClient {
21- channel_message_event_handlers : HashMap < Snowflake , Vec < broadcast:: Sender < DiscordMessage > > > ,
22- client : Option < serenity:: Client >
23+ channel_message_event_handlers : RwLock < HashMap < Snowflake , Vec < broadcast:: Sender < DiscordMessage > > > > ,
24+ client : OnceLock < serenity:: Client > ,
2325}
2426
2527impl DiscordClient {
26- pub fn new ( token : String ) -> Arc < RwLock < DiscordClient > > {
27- let client = Arc :: new ( RwLock :: new ( DiscordClient :: default ( ) ) ) ;
28- let remote = RemoteDiscordClient ( client. clone ( ) ) ;
29- let async_client = client. clone ( ) ;
30-
31- tokio:: spawn ( async move {
32- let mut client = serenity:: Client :: builder ( token, GatewayIntents :: all ( ) )
33- . event_handler ( remote)
34- . await
35- . expect ( "Error creating client" ) ;
36-
37- if let Err ( why) = client. start ( ) . await {
38- panic ! ( "Client error: {why:?}" ) ;
39- }
28+ pub async fn new ( token : String ) -> Arc < DiscordClient > {
29+ let client = Arc :: new ( DiscordClient :: default ( ) ) ;
30+
31+ let mut discord = serenity:: Client :: builder ( token, GatewayIntents :: all ( ) ) . event_handler_arc ( client. clone ( ) ) . await . expect ( "Error creating client" ) ;
32+
33+ if let Err ( why) = discord. start ( ) . await {
34+ panic ! ( "Client error: {why:?}" ) ;
35+ }
4036
41- async_client. write ( ) . await . client = Some ( client) ;
42- } ) ;
37+ let _ = client. client . set ( discord) ;
4338
4439 client
4540 }
4641
47- pub async fn add_channel_message_sender ( & mut self , channel : Snowflake , sender : broadcast:: Sender < DiscordMessage > ) {
48- self . channel_message_event_handlers . entry ( channel) . or_default ( ) . push ( sender) ;
42+ fn discord ( & self ) -> & serenity:: Client {
43+ self . client . get ( ) . unwrap ( )
44+ }
45+
46+ pub async fn add_channel_message_sender ( & self , channel : Snowflake , sender : broadcast:: Sender < DiscordMessage > ) {
47+ self . channel_message_event_handlers . write ( ) . await . entry ( channel) . or_default ( ) . push ( sender) ;
4948 }
5049
51- pub async fn send_message ( & mut self , channel_id : Snowflake , content : String , nonce : String ) {
50+ pub async fn send_message ( & self , channel_id : Snowflake , content : String , nonce : String ) {
5251 println ! ( "All the way to discord~! {:?} {:?}" , channel_id, content) ;
53- ChannelId :: new ( channel_id. content ) . send_message ( self . client . as_ref ( ) . unwrap ( ) . http . clone ( ) , CreateMessage :: new ( ) . content ( content) . enforce_nonce ( true ) . nonce ( serenity:: all:: Nonce :: String ( nonce) ) ) . await . unwrap ( ) ;
52+ ChannelId :: new ( channel_id. content )
53+ . send_message (
54+ self . discord ( ) . http . clone ( ) ,
55+ CreateMessage :: new ( ) . content ( content) . enforce_nonce ( true ) . nonce ( serenity:: all:: Nonce :: String ( nonce) ) ,
56+ )
57+ . await
58+ . unwrap ( ) ;
5459 }
5560}
5661
57- struct RemoteDiscordClient ( Arc < RwLock < DiscordClient > > ) ;
58-
5962#[ async_trait]
60- impl EventHandler for RemoteDiscordClient {
61- async fn ready ( & self , ctx : Context , data_about_bot : serenity:: model:: prelude:: Ready ) {
63+ impl EventHandler for DiscordClient {
64+ async fn ready ( & self , _ : Context , data_about_bot : serenity:: model:: prelude:: Ready ) {
6265 println ! ( "Ready! {:?}" , data_about_bot) ;
6366 }
6467
@@ -69,7 +72,7 @@ impl EventHandler for RemoteDiscordClient {
6972 content : msg. channel_id . get ( ) ,
7073 } ;
7174
72- if let Some ( vec) = self . 0 . read ( ) . await . channel_message_event_handlers . get ( & snowflake) {
75+ if let Some ( vec) = self . channel_message_event_handlers . read ( ) . await . get ( & snowflake) {
7376 for sender in vec {
7477 println ! ( "Sending to sender!" ) ;
7578
@@ -86,7 +89,7 @@ impl EventHandler for RemoteDiscordClient {
8689 nonce : msg. nonce . clone ( ) . map ( |n| match n {
8790 Nonce :: Number ( n) => n. to_string ( ) ,
8891 Nonce :: String ( s) => s,
89- } )
92+ } ) ,
9093 } ) ;
9194 }
9295 }
0 commit comments