73 lines
2.1 KiB
Rust
73 lines
2.1 KiB
Rust
use std::{fs, hash::{DefaultHasher, Hash, Hasher}, io, path::Path, sync::Arc, vec};
|
|
|
|
use serenity::{all::{ChannelId, ChannelType, CreateMessage, GuildId, GuildRef, Message}, http::Http};
|
|
|
|
use poise::CreateReply;
|
|
use serenity::async_trait;
|
|
|
|
use crate::types::Context;
|
|
|
|
|
|
pub async fn get_system_channel(guild_id: GuildId, http: &Http) -> anyhow::Result<ChannelId> {
|
|
use anyhow::Context;
|
|
return http.get_guild(guild_id).await?.system_channel_id
|
|
.context(format!("System channel of guild: {} not found", guild_id.get()));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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 {
|
|
if channel.name.contains(&name) {
|
|
result = Some(channel.id);
|
|
break;
|
|
}
|
|
}
|
|
|
|
result
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|