Skip to content

Commit 9aef807

Browse files
committed
changes
1 parent ee4f42b commit 9aef807

File tree

7 files changed

+24
-77
lines changed

7 files changed

+24
-77
lines changed

src/ai_message.rs

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,35 @@
11
use color_eyre::Result;
22
use r2d2_sqlite::SqliteConnectionManager;
3-
use rig::{
4-
completion::{Prompt, ToolDefinition},
5-
prelude::*,
6-
providers,
7-
tool::Tool,
8-
};
9-
use rusqlite::params;
10-
use serde::{Deserialize, Serialize};
11-
use serde_json::json;
3+
use rig::{completion::Prompt, prelude::*, providers};
124
use serde_rusqlite::from_row;
135

14-
use crate::{
15-
brave::BraveApi,
16-
database::{self, User},
17-
};
6+
use crate::{brave::BraveApi, database::User};
187

198
use std::collections::HashMap;
20-
use std::sync::{Arc, Mutex};
21-
22-
// Tool call logger to track which tools are called
23-
type ToolCallLogger = Arc<Mutex<Vec<String>>>;
24-
25-
#[derive(Debug, thiserror::Error)]
26-
#[error("Error")]
27-
pub enum ToolError {
28-
Generic(#[from] color_eyre::Report),
29-
Pool(#[from] r2d2::Error),
30-
}
319

3210
pub async fn main(
3311
database: r2d2::Pool<SqliteConnectionManager>,
3412
user_id: u64,
3513
message: &str,
3614
context: &str,
37-
brave: BraveApi,
15+
_brave: BraveApi,
3816
user_mentions: HashMap<String, u64>,
3917
) -> Result<String> {
4018
// Create OpenAI client
4119
let openai_client = providers::openai::Client::from_env();
4220

21+
// Replace user mentions with usernames in context
22+
let mut processed_context = context.to_string();
23+
for (mention, mentioned_user_id) in &user_mentions {
24+
let db = database.get()?;
25+
let mut statement = db.prepare("SELECT * FROM user WHERE id = ?").unwrap();
26+
if let Ok(mentioned_user) = statement.query_one([mentioned_user_id.to_string()], |row| {
27+
from_row::<User>(row).map_err(|_| rusqlite::Error::QueryReturnedNoRows)
28+
}) {
29+
processed_context = processed_context.replace(mention, &mentioned_user.name);
30+
}
31+
}
32+
4333
let user = {
4434
let db = database.get()?;
4535
let mut statement = db.prepare("SELECT * FROM user WHERE id = ?").unwrap();
@@ -59,12 +49,7 @@ pub async fn main(
5949
}
6050
};
6151

62-
let User {
63-
name,
64-
level,
65-
xp,
66-
..
67-
} = user;
52+
let User { name, level, xp, .. } = user;
6853

6954
// Create agent - smarter and more annoying
7055
let smart_agent = openai_client
@@ -91,7 +76,7 @@ You are replying to {name}.
9176
{name} is level: {level}, xp: {xp}.
9277
9378
message context:
94-
{context}", ).replace("\\\n", ""))
79+
{}", processed_context).replace("\\\n", ""))
9580
.max_tokens(2048)
9681
.build();
9782

src/brave.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ impl BraveApi {
5151
.json::<BraveApiResponse>()
5252
.await?;
5353

54-
Ok(resp.web.and_then(|w| Some(w.results)).unwrap_or_default())
54+
Ok(resp.web.map(|w| w.results).unwrap_or_default())
5555
}
5656
}

src/commands/level.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use std::sync::Arc;
44

5-
use num_format::{Locale, ToFormattedString};
65
use serde_rusqlite::{from_row, from_rows};
76
use tokio::sync::Mutex;
87

src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub struct Config {
3131
pub openai_api_key: Option<String>,
3232
#[arg(long, env)]
3333
pub today_i_channel: Option<u64>,
34-
#[arg(long,env)]
35-
pub brave_api: Option<String>
34+
#[arg(long, env)]
35+
pub brave_api: Option<String>,
3636
}
3737

3838
fn parse_str_array(src: &str) -> Result<Arc<Vec<String>>, io::Error> {
@@ -44,7 +44,7 @@ fn parse_invites(src: &str) -> Result<HashMap<String, String>, io::Error> {
4444
for pair in src.split(',') {
4545
let (key, value) = match pair.split_once(':') {
4646
Some(v) => v,
47-
None => return Err(io::Error::new(io::ErrorKind::Other, "Invalid invite format")),
47+
None => return Err(io::Error::other("Invalid invite format")),
4848
};
4949
map.insert(key.to_string(), value.parse().unwrap());
5050
}

src/event_handler.rs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,11 @@ use rand::Rng;
44
use tokio::{join, sync::Mutex};
55
use twilight_gateway::Event;
66
use twilight_http::{request::channel::reaction::RequestReactionType, Client as HttpClient};
7-
use twilight_model::{
8-
channel::message::AllowedMentions,
9-
gateway::presence::Status,
10-
id::{
11-
marker::{ChannelMarker, UserMarker},
12-
Id,
13-
},
14-
};
7+
use twilight_model::{channel::message::AllowedMentions, id::Id};
158
use vesper::prelude::*;
169

1710
use std::{sync::Arc, time::Duration};
1811

19-
const AETHOR_ID: Id<UserMarker> = Id::new(870383692403593226);
20-
const GENERAL_ID: Id<ChannelMarker> = Id::new(748957504666599507);
21-
const TRICKED_ID: u64 = 336465356304678913;
22-
2312
pub async fn handle_event(
2413
event: Event,
2514
http: &Arc<HttpClient>,
@@ -28,20 +17,6 @@ pub async fn handle_event(
2817
) -> color_eyre::Result<()> {
2918
let mut locked_state = state.lock().await;
3019
match event {
31-
Event::PresenceUpdate(p) => {
32-
if p.user.id() == AETHOR_ID && p.status == Status::Offline {
33-
for _ in 0..10 {
34-
http.create_message(GENERAL_ID)
35-
.content("AETHOR WENT OFFLINE <@{TRICKED_ID}> <@{TRICKED_ID}>")?
36-
.allowed_mentions(Some(&AllowedMentions {
37-
users: vec![Id::new(TRICKED_ID)],
38-
..Default::default()
39-
}))
40-
.exec()
41-
.await?;
42-
}
43-
}
44-
}
4520
Event::InteractionCreate(i) => {
4621
tracing::info!("Slash Command!");
4722
tokio::spawn(async move {
@@ -135,14 +110,6 @@ pub async fn handle_event(
135110
if rand::thread_rng().gen_range(0..100) != 1 {
136111
return Ok(());
137112
}
138-
139-
if !locked_state
140-
.config
141-
.message_indicator_channels
142-
.contains(&event.channel_id.get())
143-
{
144-
return Ok(());
145-
}
146113
if event.user_id.get() == locked_state.last_typer {
147114
return Ok(());
148115
}

src/message_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ pub async fn handle_message(
228228
Ok(Command::text(text.join(" ")).reply())
229229
}
230230

231-
_ if locked_state.rng.gen_range(0..40) == 2 && !locked_state.config.shit_reddits.is_empty() => {
231+
_ if locked_state.rng.gen_range(0..30) == 2 && !locked_state.config.shit_reddits.is_empty() => {
232232
let res = locked_state
233233
.client
234234
.get(format!(

src/structs.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ use reqwest::Client;
1010
use serde::{Deserialize, Serialize};
1111
use twilight_bucket::{Bucket, Limit};
1212
use twilight_cache_inmemory::InMemoryCache;
13-
use twilight_model::{
14-
channel::message::Embed,
15-
http::attachment::Attachment, id::Id,
16-
};
13+
use twilight_model::{channel::message::Embed, http::attachment::Attachment, id::Id};
1714
use vesper::twilight_exports::ChannelMarker;
1815

1916
use crate::{brave::BraveApi, config::Config};
@@ -76,7 +73,6 @@ impl Command {
7673
}
7774
}
7875

79-
8076
/// This struct is used to store the state of the bot.\
8177
/// It is used to store the cache, the database connection, the config and the http client.
8278
pub struct State {

0 commit comments

Comments
 (0)