Skip to content

Commit

Permalink
forgot to actually use message.content lol
Browse files Browse the repository at this point in the history
  • Loading branch information
Inspirateur committed Dec 27, 2022
1 parent 87a4062 commit b0eff0a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/handle_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use serenity::{
Interaction,
},
gateway::Ready,
guild::Guild,
guild::Guild, prelude::Message,
},
async_trait,
prelude::*
};
use log::info;
use log::{info, trace};
use crate::handler_util::{response, is_writable};
use crate::handler::Handler;

Expand Down Expand Up @@ -46,4 +46,11 @@ impl EventHandler for Handler {
async fn guild_create(&self, ctx: Context, guild: Guild, _is_new: bool) {
self.register_guild(ctx.http, guild).await;
}

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);
}
}
}
11 changes: 7 additions & 4 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use itertools::Itertools;
use log::{warn, info};
use log::{warn, info, trace};
use image::{write_buffer_with_format, ColorType, ImageOutputFormat};
use std::{io::{Cursor, Seek, SeekFrom}, sync::Arc};
use palette::rgb::Rgb;
Expand All @@ -17,6 +17,7 @@ use serenity::{
};
use wordcloud_rs::{Token, WordCloud, Colors};
use crate::idiom::Idioms;
const READ_PAST: u64 = 10000;

fn convert_color(color: Color) -> Rgb {
Rgb::new(
Expand All @@ -38,7 +39,7 @@ impl Handler {
}

pub fn message(&self, guild_id: GuildId, channel_id: ChannelId, member_id: UserId, message: String) {
self.idioms.get_mut(&guild_id).unwrap().update(channel_id, member_id, message)
self.idioms.get_mut(&guild_id).unwrap().update(channel_id, member_id, message);
}

fn to_wc_tokens(&self, tokens: Vec<(String, f32)>) -> Vec<(Token, f32)> {
Expand All @@ -52,6 +53,7 @@ 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);
let image = WordCloud::new()
.colors(Colors::DoubleSplitCompl(convert_color(color))).generate(wc_tokens);
Expand Down Expand Up @@ -109,13 +111,14 @@ impl Handler {
tokio::spawn(async move {
for (channel_id, channel) in channels {
if let Ok(messages) = channel.messages(
&http, |retriever| retriever.limit(1000)
&http, |retriever| retriever.limit(READ_PAST)
).await {
for message in messages {
idioms.get_mut(&guild.id).unwrap().update(
channel_id, message.author.id, String::new()
channel_id, message.author.id, message.content
);
}
info!(target: "Wordy", "Read {} past messages in {}/{}", READ_PAST, guild.name, channel.name())
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn get_token(name: &str) -> Option<String> {

#[tokio::main]
async fn main() {
env_logger::builder().filter_module("Wordy", LevelFilter::Info).init();
env_logger::builder().filter_module("Wordy", LevelFilter::Trace).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 Down

0 comments on commit b0eff0a

Please sign in to comment.