@ -15,6 +15,9 @@ struct Handler {
model : String ,
model : String ,
}
}
const SYSTEM_MESSAGE : & str =
"You are Smolhaj, a Discord bot made by skyevg. Your creator is skyevg, not OpenAI or any other entity. You are helpful and friendly, and answers concisely. Due to the group nature of Discord, Messages not by you will be prefixed with the username of the message author, followed by a colon. The prefix is not part of the actual message. Everything before the first colon, including special characters, is part of the username. Unlike traditional Discord bots, you don't have commands. Current date and time in UTC: " ;
#[ async_trait ]
#[ async_trait ]
impl EventHandler for Handler {
impl EventHandler for Handler {
async fn message ( & self , ctx : Context , msg : Message ) {
async fn message ( & self , ctx : Context , msg : Message ) {
@ -60,8 +63,14 @@ impl EventHandler for Handler {
msgs . reverse ( ) ;
msgs . reverse ( ) ;
for pk_msg in msgs . clone ( ) . into_iter ( ) . filter ( | message | message . webhook_id . is_some ( ) ) {
for pk_msg in msgs
if let Some ( id ) = msgs . iter ( ) . rposition ( | message | message . id < pk_msg . id & & message . content . contains ( & pk_msg . content ) ) {
. clone ( )
. into_iter ( )
. filter ( | message | message . webhook_id . is_some ( ) )
{
if let Some ( id ) = msgs . iter ( ) . rposition ( | message | {
message . id < pk_msg . id & & message . content . contains ( & pk_msg . content )
} ) {
msgs . remove ( id ) ;
msgs . remove ( id ) ;
}
}
}
}
@ -84,30 +93,44 @@ impl EventHandler for Handler {
let last_msg = msgs . clone ( ) . last ( ) . unwrap ( ) ;
let last_msg = msgs . clone ( ) . last ( ) . unwrap ( ) ;
let msgs = tokio_stream ::iter ( msgs )
let mut msgs = tokio_stream ::iter ( msgs )
. map ( | message | async move {
. map ( | message | async move {
if message . author . id = = current_id {
if message . author . id = = current_id {
openai ::Message {
openai ::Message {
content : message . content . clone ( ) ,
content : message . content . clone ( ) ,
role : openai ::Role ::Assistant ,
role : openai ::Role ::Assistant ,
}
}
} else {
} else {
openai ::Message {
openai ::Message {
content : format ! (
content : format ! (
"{}: {}" ,
"{}: {}" ,
message
message
. author_nick ( ref_ctx )
. author_nick ( ref_ctx )
. await
. await
. unwrap_or ( message . author . name . clone ( ) ) ,
. unwrap_or ( message . author . name . clone ( ) ) ,
message . content
message . content
) ,
) ,
role : openai ::Role ::User ,
role : openai ::Role ::User ,
}
}
}
}
} )
} )
. buffered ( 100 )
. buffered ( 100 )
. collect ::< Vec < _ > > ( )
. collect ::< Vec < _ > > ( )
. await ;
. await ;
msgs . insert (
0 ,
openai ::Message {
role : openai ::Role ::System ,
content : format ! (
"{}{}" ,
SYSTEM_MESSAGE ,
time ::OffsetDateTime ::now_utc ( )
. format ( & time ::format_description ::well_known ::Iso8601 ::DEFAULT )
. unwrap ( )
) ,
} ,
) ;
let response = self
let response = self
. client
. client
@ -189,7 +212,7 @@ async fn main() {
channel : ChannelId ( channel ) ,
channel : ChannelId ( channel ) ,
client : openai_client ,
client : openai_client ,
reset_time : time ::OffsetDateTime ::now_utc ( ) . into ( ) ,
reset_time : time ::OffsetDateTime ::now_utc ( ) . into ( ) ,
model
model ,
} )
} )
. await
. await
. expect ( "Err creating client" ) ;
. expect ( "Err creating client" ) ;