Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Inspirateur/wordy
Browse files Browse the repository at this point in the history
  • Loading branch information
Inspirateur committed May 1, 2023
2 parents d2cb74a + d292f34 commit fa21e94
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 53 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# wordy
Improved word cloud discord bot

[demo incoming]
134 changes: 81 additions & 53 deletions src/wordy_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ const READ_PAST: u64 = 1000;
const DAYS: i64 = 100;

impl Wordy {
pub async fn cloud_command(&self, ctx: Context, command: ApplicationCommandInteraction) -> Result<()> {
pub async fn cloud_command(
&self,
ctx: Context,
command: ApplicationCommandInteraction,
) -> Result<()> {
if command.guild_id.is_none() {
bail!("Command wasn't invoked in a Guild.");
}
let member = command.member.as_ref().ok_or(anyhow!("Couldn't get member."))?;
let member = command
.member
.as_ref()
.ok_or(anyhow!("Couldn't get member."))?;
let image = self.cloud(&ctx, &member).await;
let mut img_file = Cursor::new(Vec::new());
write_buffer_with_format(
Expand All @@ -30,72 +37,87 @@ impl Wordy {
.unwrap();
img_file.seek(SeekFrom::Start(0)).unwrap();
let img_vec = img_file.into_inner();
ctx.http.answer(
&command,
"",
vec![Attachment {
file: img_vec,
filename: format!("WordCloud_{}.png", member.display_name())
}]
).await
ctx.http
.answer(
&command,
"",
vec![Attachment {
file: img_vec,
filename: format!("WordCloud_{}.png", member.display_name()),
}],
)
.await
}

pub async fn emojis_command(&self, ctx: Context, command: ApplicationCommandInteraction) -> Result<()> {
let guild_id = command.guild_id.as_ref().ok_or(anyhow!("Couldn't get member."))?;
pub async fn emojis_command(
&self,
ctx: Context,
command: ApplicationCommandInteraction,
) -> Result<()> {
let guild_id = command
.guild_id
.as_ref()
.ok_or(anyhow!("Couldn't get member."))?;
let emoji_rankings = self.emojis(*guild_id)?;
let png_msg = "Static emoji ranking:\n".to_string()
+ &emo_ranking_msg(emoji_rankings.png);
let gif_msg = "Animated emoji ranking:\n".to_string()
+ &emo_ranking_msg(emoji_rankings.gif);
let png_msg = "Static emoji ranking:\n".to_string() + &emo_ranking_msg(emoji_rankings.png);
let gif_msg =
"Animated emoji ranking:\n".to_string() + &emo_ranking_msg(emoji_rankings.gif);
ctx.http.answer(&command, &png_msg, vec![]).await?;
ctx.http.followup(&command, &gif_msg, vec![]).await
}

pub async fn info_command(&self, ctx: Context, command: ApplicationCommandInteraction) -> Result<()> {
ctx.http.answer(
&command,
"Made with ❤️ by Inspi#8989\n
Repository: <https://github.com/Inspirateur/wordy>",
vec![]
).await
pub async fn info_command(
&self,
ctx: Context,
command: ApplicationCommandInteraction,
) -> Result<()> {
ctx.http
.answer(
&command,
"Made with ❤️ by Inspi#8989\n
Repository: <https://github.com/Inspirateur/wordy>",
vec![],
)
.await
}

pub async fn register_commands(&self, http: Arc<Http>, guild_id: GuildId) {
trace!("Registering slash commands for Guild {}", guild_id);
if let Err(why) =
GuildId::set_application_commands(&guild_id, http, |commands| {
commands
.create_application_command(|command| {
command.name("cloud").description(
"Discover the word cloud that defines you !",
)
})
.create_application_command(|command| {
command.name("emojis").description(
"Recent emoji usage stats.",
)
})
.create_application_command(|command| {
command
.name("info")
.description("Information about this bot.")
})
}).await {
println!("Couldn't register slash commmands: {}", why);
trace!(target: "wordy", "Registering slash commands for Guild {}", guild_id);
if let Err(why) = GuildId::set_application_commands(&guild_id, http, |commands| {
commands
.create_application_command(|command| {
command
.name("cloud")
.description("Discover the word cloud that defines you !")
})
.create_application_command(|command| {
command
.name("emojis")
.description("Recent emoji usage stats.")
})
.create_application_command(|command| {
command
.name("info")
.description("Information about this bot.")
})
})
.await
{
warn!(target: "wordy", "Couldn't register slash commmands: {}", why);
};
}

pub async fn register_guild(&self, http: Arc<Http>, guild: Guild) {
// only read messages that are less than 100 days old
let cutoff_date = Timestamp::from_unix_timestamp(
Timestamp::now().unix_timestamp() - 3600*24*DAYS
).unwrap();
let cutoff_date =
Timestamp::from_unix_timestamp(Timestamp::now().unix_timestamp() - 3600 * 24 * DAYS)
.unwrap();
if let Ok(channels) = guild.channels(&http).await {
if !register_guild(
&guild,
self.idioms.clone(),
self.recents_emos.clone(),
self.servers_emos.clone()
&guild,
self.idioms.clone(),
self.recents_emos.clone(),
self.servers_emos.clone(),
) {
return;
}
Expand All @@ -108,7 +130,13 @@ impl Wordy {
let messages = read_past(&http, &channel, READ_PAST, cutoff_date).await;
let len = messages.len();
for message in messages {
read_message(guild.id, message, idioms.clone(), recents_emos.clone(), servers_emos.clone());
read_message(
guild.id,
message,
idioms.clone(),
recents_emos.clone(),
servers_emos.clone(),
);
}
if len > 0 {
info!(target: "wordy", "Read {} past messages in {}/{}", len, guild.name, channel.name())
Expand All @@ -117,4 +145,4 @@ impl Wordy {
});
}
}
}
}

0 comments on commit fa21e94

Please sign in to comment.