@@ -19,19 +19,26 @@ use crate::{
1919#[ derive( Default ) ]
2020pub struct DiscordClient {
2121 channel_message_event_handlers : HashMap < Snowflake , Vec < broadcast:: Sender < DiscordMessage > > > ,
22+ client : Option < serenity:: Client >
2223}
2324
2425impl DiscordClient {
2526 pub fn new ( token : String ) -> Arc < RwLock < DiscordClient > > {
2627 let client = Arc :: new ( RwLock :: new ( DiscordClient :: default ( ) ) ) ;
2728 let remote = RemoteDiscordClient ( client. clone ( ) ) ;
29+ let async_client = client. clone ( ) ;
2830
29- tokio:: spawn ( async {
30- let mut client = serenity:: Client :: builder ( token, GatewayIntents :: all ( ) ) . event_handler ( remote) . await . expect ( "Error creating client" ) ;
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" ) ;
3136
3237 if let Err ( why) = client. start ( ) . await {
3338 panic ! ( "Client error: {why:?}" ) ;
3439 }
40+
41+ async_client. write ( ) . await . client = Some ( client) ;
3542 } ) ;
3643
3744 client
@@ -40,6 +47,11 @@ impl DiscordClient {
4047 pub async fn add_channel_message_sender ( & mut self , channel : Snowflake , sender : broadcast:: Sender < DiscordMessage > ) {
4148 self . channel_message_event_handlers . entry ( channel) . or_default ( ) . push ( sender) ;
4249 }
50+
51+ pub async fn send_message ( & mut self , channel_id : Snowflake , content : String , nonce : String ) {
52+ 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 ( ) ;
54+ }
4355}
4456
4557struct RemoteDiscordClient ( Arc < RwLock < DiscordClient > > ) ;
@@ -68,8 +80,12 @@ impl EventHandler for RemoteDiscordClient {
6880 icon : msg. author . avatar_url ( ) . unwrap_or ( msg. author . default_avatar_url ( ) ) ,
6981 } ,
7082 content : DiscordMessageContent {
71- content : msg. content . clone ( ) ,
83+ content : msg. content . clone ( )
7284 } ,
85+ nonce : msg. nonce . clone ( ) . map ( |n| match n {
86+ Nonce :: Number ( n) => n. to_string ( ) ,
87+ Nonce :: String ( s) => s,
88+ } )
7389 } ) ;
7490 }
7591 }
0 commit comments