moover_rust/src/utils/utilities.rs

74 lines
2.1 KiB
Rust
Raw Normal View History

2025-01-26 20:45:24 +00:00
use std::{fs, hash::{DefaultHasher, Hash, Hasher}, io, path::Path, sync::Arc, vec};
2024-12-17 14:28:52 +00:00
use serenity::{all::{ChannelId, ChannelType, CreateMessage, GuildId, GuildRef, Message}, http::Http};
2025-01-26 20:45:24 +00:00
use poise::CreateReply;
use serenity::async_trait;
use crate::types::Context;
2024-10-06 14:53:12 +00:00
pub async fn get_system_channel(guild_id: GuildId, http: &Http) -> anyhow::Result<ChannelId> {
2024-12-17 14:28:52 +00:00
use anyhow::Context;
return http.get_guild(guild_id).await?.system_channel_id
.context(format!("System channel of guild: {} not found", guild_id.get()));
2024-09-28 16:39:32 +00:00
}
pub async fn replace_msg(http: Arc<Http>, msg: Message, content: String) -> Result<Message, serenity::Error> {
msg.delete(http.clone()).await?;
return ChannelId::new(msg.channel_id.get()).send_message(http.clone(), CreateMessage::new().content(content)).await;
2024-12-09 14:40:46 +00:00
}
pub fn get_vc_names(guild: GuildRef) -> Vec<String> {
let mut result: Vec<String> = [].to_vec();
for (_, channel) in &guild.channels {
if channel.kind == ChannelType::Voice {
result.push(channel.name.clone());
}
}
result
}
pub fn get_channel_by_name(guild: GuildRef, name: String) -> Option<ChannelId> {
let mut result = None;
for (_, channel) in &guild.channels {
2024-12-17 14:28:52 +00:00
if channel.name.contains(&name) {
2024-12-09 14:40:46 +00:00
result = Some(channel.id);
break;
}
}
result
2024-12-17 14:28:52 +00:00
}
pub fn get_local_songs(partial: &str) -> io::Result<Vec<String>> {
let mut result: Vec<String> = vec![];
let path = Path::new("/home/emil/Music");
for entry in fs::read_dir(path)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
continue;
}
let name = entry.file_name();
if name.to_str().is_some_and(|name| !name.to_lowercase().contains(&partial.to_lowercase())) {
continue;
}
result.push(name.to_str().unwrap().into());
}
Ok(result)
}
2025-01-26 20:45:24 +00:00
pub fn hash_to_u32<T: Hash>(from: &T) -> u32 {
let mut hasher = DefaultHasher::new();
from.hash(&mut hasher);
let hash_val = hasher.finish();
(hash_val & 0xFFFFFF) as u32
}