fix: several improvements

This commit is contained in:
Miguel da Mota 2024-03-03 23:30:31 +01:00
parent 50202dfdf5
commit 2c82dbb019
10 changed files with 384 additions and 269 deletions

View file

@ -8,7 +8,7 @@ pub struct Config {
pub discord_token: String,
pub lavalink_address: String,
pub lavalink_password: String,
pub user_id: u64
pub user_id: u64,
}
const CONFIG_FILE: &str = "./data/config.json";
@ -17,23 +17,23 @@ pub fn load() -> Result<Config, Box<dyn Error>> {
// TODO: load config, create empty config if there is no config, stop if there is no complete config
let config_file = match fs::File::open(CONFIG_FILE) {
Ok(file) => file,
Err(_) => create_empty()
Err(_) => create_empty(),
};
let config_file = serde_json::from_reader(config_file).unwrap();
Ok(config_file)
}
fn create_empty() -> fs::File{
fn create_empty() -> fs::File {
let example_config = Config {
discord_token: "paste_your_token".to_string(),
lavalink_address: "paste_your_lavalink_address".to_string(),
lavalink_password: "paste_your_lavalink_password".to_string(),
user_id: 1
user_id: 1,
};
let mut config_file = fs::File::create(CONFIG_FILE).unwrap();
let file_content = serde_json::to_string(&example_config).unwrap();
config_file.write_all(&file_content.as_bytes()).unwrap();
config_file.write_all(file_content.as_bytes()).unwrap();
panic!("There is no config. But now there is a template.")
}
}

29
src/util/embed.rs Normal file
View file

@ -0,0 +1,29 @@
use std::fmt::Display;
use chrono::Local;
use serenity::all::{CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter};
pub struct Embed;
impl Embed {
pub fn create<
S: Into<String> + Display,
T: Into<String> + Display,
U: Into<String> + Display,
>(
username: S,
title: T,
message: U,
) -> CreateEmbed {
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
CreateEmbed::new()
.author(CreateEmbedAuthor::new(username.to_string()))
.title(title)
.description(message)
.footer(CreateEmbedFooter::new(format!(
"> {} | {}",
current_time, username
)))
}
}

View file

@ -1,2 +1,3 @@
pub mod config;
pub mod user_util;
pub mod embed;
pub mod user_util;

View file

@ -1,42 +1,38 @@
use serenity::all::{ChannelId, Context, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter, GuildId};
use serenity::all::{ChannelId, Context, Guild, GuildId};
/// Get a guild by id
pub fn get_guild(ctx: &Context, guild_id: &GuildId) -> Option<Guild> {
let guild = ctx.cache.guild(guild_id)?;
Some(guild.clone())
}
/// Get the current channel id of the bot
pub fn get_channel_id(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> {
let guild = get_guild(ctx, guild_id)?;
let channel_id = guild
.voice_states
.get(&ctx.cache.current_user().id)
.and_then(|voice_state| voice_state.channel_id);
Some(channel_id.unwrap())
}
pub fn is_self_connected_to_vc(ctx: &Context, guild_id: &GuildId) -> bool {
let self_id = &ctx.cache.current_user().id;
let (_guild_id, channel_id) = {
let guild = &ctx.cache.guild(guild_id).unwrap();
// This may be unsafe, idk not sure yet
let channel_id = guild
.voice_states
.get(self_id)
.and_then(|voice_state| voice_state.channel_id);
(guild.id, channel_id)
};
let channel_id = get_channel_id(ctx, guild_id);
// TODO: There has to be a way to improve this. This is bad code and it should be optimized.
let connect_to = match channel_id {
Some(channel) => channel,
None => {
return false
},
};
true
if channel_id.is_none() {
return false;
}
true
}
// This whole file is jank. I have to rewrite this once I know Rust better
pub fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> ChannelId {
let self_id = &ctx.cache.current_user().id;
let (guild_id, channel_id) = {
let guild = &ctx.cache.guild(guild_id).unwrap();
// This may be unsafe, idk not sure yet
let channel_id = guild
.voice_states
.get(&self_id)
.and_then(|voice_state| voice_state.channel_id);
(guild.id, channel_id)
};
channel_id.unwrap()
pub fn get_self_vc_id(ctx: &Context, guild_id: &GuildId) -> Option<ChannelId> {
let channel_id = get_channel_id(ctx, guild_id)?;
Some(channel_id)
}