Skip to content

Commit

Permalink
now resolving user tags and channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Inspirateur committed Jan 21, 2023
1 parent ae6b93d commit b785f00
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/handle_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl EventHandler for Handler {
}

async fn ready(&self, _ctx: Context, ready: Ready) {
info!(target: "Wordy", "{} is connected!", ready.user.name);
info!(target: "wordy", "{} is connected!", ready.user.name);
}

async fn guild_create(&self, ctx: Context, guild: Guild, _is_new: bool) {
Expand All @@ -49,6 +49,7 @@ impl EventHandler for Handler {

async fn message(&self, _ctx: Context, message: Message) {
if let Some(guild_id) = message.guild_id {
trace!(target: "wordy", "Read a new message from {}", message.author.name);
self.message(guild_id, message.channel_id, message.author.id, message.content);
}
}
Expand Down
45 changes: 32 additions & 13 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ use serenity::{
application_command::ApplicationCommandInteraction,
InteractionResponseType::ChannelMessageWithSource,
},
id::GuildId, prelude::{UserId, ChannelId, Guild}, Timestamp
id::GuildId, prelude::{UserId, ChannelId, Guild, Channel}, Timestamp
},
prelude::*, utils::Color
};
use futures::future::join_all;
use lazy_static::lazy_static;
use wordcloud_rs::{Token, WordCloud, Colors};
use crate::{idiom::{Idioms, tokenize, self}, discord_emojis::DiscordEmojis, handler_util::read_past};
use crate::{idiom::{Idioms, tokenize}, discord_emojis::DiscordEmojis, handler_util::read_past};
const READ_PAST: u64 = 1000;
const DAYS: i64 = 100;

lazy_static! {
static ref RE_EMO: Regex = Regex::new(r"<a?:(\w*):(\d*)>").unwrap();
static ref RE_TAG: Regex = Regex::new(r"<@(\d*)>").unwrap();
static ref RE_CHAN: Regex = Regex::new(r"<#(\d*)>").unwrap();
static ref RE_ROLE: Regex = Regex::new(r"<@&(\d*)>").unwrap();
}

fn convert_color(color: Color) -> Rgb {
Expand Down Expand Up @@ -52,20 +55,36 @@ impl Handler {
if let Some(mut idiom) = self.idioms.get_mut(&guild_id) {
idiom.update(channel_id, member_id, tokenize(message));
} else {
warn!(target: "Wordy", "Guild {} isn't registered yet.", guild_id);
warn!(target: "wordy", "Guild {} isn't registered yet.", guild_id);
}
}

async fn to_wc_tokens(&self, tokens: Vec<(String, f32)>) -> Vec<(Token, f32)> {
async fn to_wc_tokens(
&self, tokens: Vec<(String, f32)>, http: &Arc<Http>
) -> Vec<(Token, f32)> {
join_all(tokens.into_iter().map(|(str, v)| async move {
if let Some(capts) = RE_EMO.captures(&str) {
let id = capts.get(2).unwrap().as_str();
if let Ok(img) = self.emojis.get(id).await {
let emo_id = capts.get(2).unwrap().as_str();
if let Ok(img) = self.emojis.get(emo_id).await {
(Token::Img(img), v)
} else {
let name = capts.get(1).unwrap().as_str();
(Token::Text(name.to_string()), v)
}
} else if let Some(capts) = RE_TAG.captures(&str) {
let user_id = capts.get(1).unwrap().as_str().parse().unwrap();
if let Ok(member) = http.get_user(user_id).await {
(Token::Text(format!("@{}", member.name)), v)
} else {
(Token::Text("@deleted_user".to_string()), v)
}
} else if let Some(capts) = RE_CHAN.captures(&str) {
let chan_id = capts.get(1).unwrap().as_str().parse().unwrap();
match http.get_channel(chan_id).await {
Ok(Channel::Guild(channel)) => (Token::Text(format!("#{}", channel.name)), v),
Ok(Channel::Category(channel)) => (Token::Text(format!("#{}", channel.name)), v),
_ => (Token::Text("#deleted_channel".to_string()), v)
}
} else {
(Token::Text(str), v)
}
Expand All @@ -78,8 +97,8 @@ impl Handler {
if let Some(guild_id) = command.guild_id {
let member_id = member.user.id;
let tokens = self.idioms.get(&guild_id).unwrap().idiom(member_id);
trace!(target: "Wordy", "/cloud: retrieved {} tokens for {}", tokens.len(), member.user.name);
let wc_tokens = self.to_wc_tokens(tokens).await;
trace!(target: "wordy", "/cloud: retrieved {} tokens for {}", tokens.len(), member.user.name);
let wc_tokens = self.to_wc_tokens(tokens, &ctx.http).await;
let image = WordCloud::new()
.colors(Colors::DoubleSplitCompl(convert_color(color))).generate(wc_tokens);
let mut img_file = Cursor::new(Vec::new());
Expand Down Expand Up @@ -108,13 +127,13 @@ impl Handler {
})
.await
{
warn!(target: "Wordy", "/cloud: Response failed with `{}`", why);
warn!(target: "wordy", "/cloud: Response failed with `{}`", why);
};
} else {
warn!(target: "Wordy", "/cloud: Couldn't get guild");
warn!(target: "wordy", "/cloud: Couldn't get guild");
}
} else {
warn!(target: "Wordy", "/cloud: Couldn't get member");
warn!(target: "wordy", "/cloud: Couldn't get member");
}
}

Expand All @@ -129,7 +148,7 @@ impl Handler {
).unwrap();
if let Ok(channels) = guild.channels(&http).await {
if !self.idioms.contains_key(&guild.id) {
info!(target: "Wordy", "Registering {} (id {})", guild.name, guild.id);
info!(target: "wordy", "Registering {} (id {})", guild.name, guild.id);
self.idioms.insert(guild.id, Idioms::new());
let http = Arc::clone(&http);
let idioms = Arc::clone(&self.idioms);
Expand All @@ -143,7 +162,7 @@ impl Handler {
);
}
if len > 0 {
info!(target: "Wordy", "Read {} past messages in {}/{}", len, guild.name, channel.name())
info!(target: "wordy", "Read {} past messages in {}/{}", len, guild.name, channel.name())
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/idiom/idiom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<P: Hash+Eq, U: Hash+Eq> Idioms<P, U> {
}
};
place_voc.add(idx, value);
let inctx_value = (-place_voc.get(&idx)).exp()*100.;
let inctx_value = (-place_voc.get(&idx)).exp()*50.;
user_voc.add(idx, inctx_value);
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ fn get_token(name: &str) -> Option<String> {
if let Ok(token) = env::var(name) {
Some(token)
} else {
warn!(target: "Wordy", "Couldn't find the 'WORDY_TOKEN' environment variable, using token.txt as fallback");
warn!(target: "wordy", "Couldn't find the 'WORDY_TOKEN' environment variable, using token.txt as fallback");
if let Ok(content) = read_to_string("token.txt") {
Some(content)
} else {
warn!(target: "Wordy", "Couldn't access token.txt");
warn!(target: "wordy", "Couldn't access token.txt");
None
}
}
}

#[tokio::main]
async fn main() {
env_logger::builder().filter_module("Wordy", LevelFilter::Trace).init();
env_logger::builder()
.filter_module("wordy", LevelFilter::Trace)
.filter_module("wordcloud", LevelFilter::Warn)
.init();
// Configure the client with your Discord bot token in the environment.
let token = get_token("WORDY_TOKEN").unwrap();
let http = Http::new(&token);
Expand All @@ -57,6 +60,6 @@ async fn main() {
// Shards will automatically attempt to reconnect, and will perform
// exponential backoff until it reconnects.
if let Err(why) = client.start().await {
error!(target: "Wordy", "Client error: {:?}", why);
error!(target: "wordy", "Client error: {:?}", why);
}
}

0 comments on commit b785f00

Please sign in to comment.