@ -15,6 +15,9 @@ struct Handler {
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 ]
impl EventHandler for Handler {
async fn message ( & self , ctx : Context , msg : Message ) {
@ -60,8 +63,14 @@ impl EventHandler for Handler {
msgs . reverse ( ) ;
for pk_msg in msgs . 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 ) ) {
for pk_msg in msgs
. 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 ) ;
}
}
@ -84,30 +93,44 @@ impl EventHandler for Handler {
let last_msg = msgs . clone ( ) . last ( ) . unwrap ( ) ;
let msgs = tokio_stream ::iter ( msgs )
. map ( | message | async move {
if message . author . id = = current_id {
openai ::Message {
content : message . content . clone ( ) ,
role : openai ::Role ::Assistant ,
}
} else {
openai ::Message {
content : format ! (
"{}: {}" ,
message
. author_nick ( ref_ctx )
. await
. unwrap_or ( message . author . name . clone ( ) ) ,
message . content
) ,
role : openai ::Role ::User ,
let mut msgs = tokio_stream ::iter ( msgs )
. map ( | message | async move {
if message . author . id = = current_id {
openai ::Message {
content : message . content . clone ( ) ,
role : openai ::Role ::Assistant ,
}
} else {
openai ::Message {
content : format ! (
"{}: {}" ,
message
. author_nick ( ref_ctx )
. await
. unwrap_or ( message . author . name . clone ( ) ) ,
message . content
) ,
role : openai ::Role ::User ,
}
}
}
} )
. buffered ( 100 )
. collect ::< Vec < _ > > ( )
. await ;
} )
. buffered ( 100 )
. collect ::< Vec < _ > > ( )
. 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
. client
@ -189,7 +212,7 @@ async fn main() {
channel : ChannelId ( channel ) ,
client : openai_client ,
reset_time : time ::OffsetDateTime ::now_utc ( ) . into ( ) ,
model
model ,
} )
. await
. expect ( "Err creating client" ) ;