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

@ -1,20 +1,21 @@
use chrono::Local;
use serenity::all::{CommandInteraction, Context};
use serenity::builder::{CreateCommand, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter};
use serenity::builder::{CreateCommand, CreateEmbed};
use crate::util::embed::Embed;
pub async fn run(_ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let username = command.user.name.as_str();
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.description("Botendo v7\ndeveloped by [moonleay](https://moonleay.net)\n\nCheck out the repository: https://git.moonleay.net/DiscordBots/Rustendo")
.footer(CreateEmbedFooter::new(format!(">{} | {}", current_time, username)))
}
Embed::create(
username,
"",
"Botendo v7\ndeveloped by [moonleay](https://moonleay.net)\n\nCheck out the repository: https://git.moonleay.net/DiscordBots/Rustendo",
)
}
pub fn register() -> CreateCommand {
CreateCommand::new("info").description("Infos about the bot")
}
// >18/02/2024 @ 19:01:59 - bartlo
// >2024-02-19 17:58:39 | moonleay
// >2024-02-19 17:58:39 | moonleay

View file

@ -1,93 +1,104 @@
use chrono::Local;
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, Context, ResolvedOption, ResolvedValue};
use serenity::builder::{CreateCommand, CreateCommandOption, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter};
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, Context};
use serenity::builder::{CreateCommand, CreateCommandOption, CreateEmbed};
use serenity::model::application::CommandOptionType;
use crate::util::user_util;
use crate::util::embed::Embed;
use crate::util::user_util::{self, get_guild};
pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let username = command.user.name.as_str();
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
let options = &command.data.options;
let query = if let Some(CommandDataOption {
value: CommandDataOptionValue::String(query), ..
}) = &options.first()
{
query
} else {
return CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("Error 400")
.description("There is no query provied.")
.footer(CreateEmbedFooter::new(format!(">{} | {}", current_time, username)))
};
let query = command.data.options.first().and_then(|option| {
if let CommandDataOptionValue::String(query) = &option.value {
Some(query)
} else {
None
}
});
if query.is_none() {
return Embed::create(username, "Error 400", "There is no query provided");
}
let guild_id = match &command.guild_id {
Some(guild_id) => guild_id,
None => {
return CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("GuildId not found")
.description("Could not find guild id.")
.footer(CreateEmbedFooter::new(format!("> {} | {}", current_time, username)));
return Embed::create(username, "GuildId not found", "Could not find guild id.");
}
};
let (guild_id, channel_id) = {
let guild = &ctx.cache.guild(guild_id).unwrap(); // TODO: This unwrap throws errors.
// This may be unsafe, idk not sure yet
let channel_id = guild
.voice_states
.get(&command.user.id)
.and_then(|voice_state| voice_state.channel_id);
(guild.id, channel_id)
let guild = get_guild(ctx, guild_id);
match guild {
Some(guild) => {
let channel_id = guild
.voice_states
.get(&command.user.id)
.and_then(|voice_state| voice_state.channel_id);
(guild.id, channel_id)
}
None => {
return Embed::create(username, "Guild not found", "Could not find guild.");
}
}
};
let connect_to = match channel_id {
Some(channel) => channel,
None => {
return CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("You are not in a VC.")
.description("Join one to start playing music.")
.footer(CreateEmbedFooter::new(format!("> {} | {}", current_time, username)));
},
return Embed::create(
username,
"You are not in a VC.",
"Join one to start playing music.",
);
}
};
let manager = &songbird::get(ctx)
.await
.expect("Cannot get Songbird.")
.clone();
if !user_util::is_self_connected_to_vc(&ctx, &guild_id) {
if !user_util::is_self_connected_to_vc(ctx, &guild_id) {
// self is connected to vc, check if user is in same vc
let self_channel = user_util::get_self_vc_id(&ctx, &guild_id);
let self_channel = user_util::get_self_vc_id(ctx, &guild_id);
if self_channel.is_none() {
return Embed::create(
username,
"I am not in a VC.",
"Connect me to a VC to start playing music.",
);
}
let self_channel = self_channel.unwrap();
// Check if user is in the same VC as the bot
if self_channel != connect_to {
return CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("You are not in my VC.")
.description("Connect to my VC to control the music.")
.footer(CreateEmbedFooter::new(format!("> {} | {}", current_time, username)));
return Embed::create(
username,
"You are not in my VC.",
"Connect to my VC to control the music.",
);
}
// Connect to VC
manager.join(guild_id, connect_to).await.expect("Cannot connect>...");
manager
.join(guild_id, connect_to)
.await
.expect("Cannot connect>...");
}
CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("Searching...")
.description(format!("Looking for {}", query))
.footer(CreateEmbedFooter::new(format!(">{} | {}", current_time, username)))
}
Embed::create(username, "Searching...", format!("Looking for {}", query))
}
pub fn register() -> CreateCommand {
CreateCommand::new("play")
.description("Play music")
.add_option(
CreateCommandOption::new(CommandOptionType::String, "query", "Link or search term")
.required(true)
.required(true),
)
}

View file

@ -1,60 +1,52 @@
use chrono::Local;
use serenity::all::{CommandInteraction, Context};
use serenity::builder::{CreateCommand, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter};
use serenity::builder::{CreateCommand, CreateEmbed};
use crate::util::embed::Embed;
use crate::util::user_util;
pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let username = command.user.name.as_str();
let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S");
let guild_id = match &command.guild_id {
Some(guild_id) => guild_id,
None => {
return CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("GuildId not found")
.description("Could not find guild id.")
.footer(CreateEmbedFooter::new(format!("> {} | {}", current_time, username)));
return Embed::create(username, "GuildId not found", "Could not find guild id.");
}
};
if !user_util::is_self_connected_to_vc(&ctx, guild_id) {
if !user_util::is_self_connected_to_vc(ctx, guild_id) {
// Bot is not connectd to vc; no need to dc
return CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("Bot is not connected")
.description("And therefore I cannot stop playing.")
.footer(CreateEmbedFooter::new(format!("> {} | {}", current_time, username)));
return Embed::create(
username,
"Bot is not connected",
"And therefore I cannot stop playing.",
);
}
let manager = songbird::get(&ctx)
let manager = songbird::get(ctx)
.await
.expect("Cannot get Songbird")
.clone();
let has_handler = manager.get(guild_id.clone()).is_some();
let has_handler = manager.get(*guild_id).is_some();
if has_handler {
if let Err(e) = manager.remove(guild_id.clone()).await {
return CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("There was an error")
.description(format!("Failed: {:?}", e))
.footer(CreateEmbedFooter::new(format!("> {} | {}", current_time, username)));
if let Err(e) = manager.remove(*guild_id).await {
return Embed::create(username, "There was an error", format!("Failed: {:?}", e));
}
return CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("I stopped and left\nJust like your girlfriend.")
.footer(CreateEmbedFooter::new(format!(">{} | {}", current_time, username)))
return Embed::create(
username,
"I stopped and left\nJust like your girlfriend.",
"",
);
}
CreateEmbed::new()
.author(CreateEmbedAuthor::new("Rustendo"))
.title("Bot is not connected")
.description("And therefore I cannot stop playing.\nSomething happend, which shouldn't have.")
.footer(CreateEmbedFooter::new(format!("> {} | {}", current_time, username)))
}
Embed::create(
username,
"Bot is not connected",
"And therefore I cannot stop playing.\nSomething happend, which shouldn't have.",
)
}
pub fn register() -> CreateCommand {
CreateCommand::new("stop").description("Stop playing and start leavin'")