From b57b4127c6154751ea31a6170a1d5f757e69a851 Mon Sep 17 00:00:00 2001 From: DaXcess Date: Tue, 8 Nov 2022 09:40:18 +0100 Subject: [PATCH 1/4] Fix issue #3, add some extra cases for auto disconnect --- src/ipc/packet.rs | 2 ++ src/player.rs | 8 +++++++- src/session/mod.rs | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/ipc/packet.rs b/src/ipc/packet.rs index a8b9283..ca327d4 100644 --- a/src/ipc/packet.rs +++ b/src/ipc/packet.rs @@ -7,6 +7,8 @@ pub enum IpcPacket { Connect(String, String), Disconnect, + ConnectError(String), + StartPlayback, StopPlayback, diff --git a/src/player.rs b/src/player.rs index d85100d..cb1c76a 100644 --- a/src/player.rs +++ b/src/player.rs @@ -59,7 +59,13 @@ impl SpoticordPlayer { // Connect the session let (session, _) = match Session::connect(session_config, credentials, None, false).await { Ok((session, credentials)) => (session, credentials), - Err(why) => panic!("Failed to connect: {}", why), + Err(why) => { + self + .client + .send(IpcPacket::ConnectError(why.to_string())) + .unwrap(); + return; + } }; // Store session for later use diff --git a/src/session/mod.rs b/src/session/mod.rs index 915c931..89bb03f 100644 --- a/src/session/mod.rs +++ b/src/session/mod.rs @@ -199,6 +199,35 @@ impl SpoticordSession { trace!("Received IPC message: {:?}", msg); match msg { + // Session connect error + IpcPacket::ConnectError(why) => { + error!("Failed to connect to Spotify: {:?}", why); + + // Notify the user in the text channel + if let Err(why) = ipc_instance + .text_channel_id + .send_message(&ipc_instance.http, |message| { + message.embed(|embed| { + embed.title("Failed to connect to Spotify"); + embed.description(why); + embed.color(Status::Error as u64); + + embed + }); + + message + }) + .await + { + error!("Failed to send error message: {:?}", why); + } + + // Clean up session + ipc_instance.player_stopped().await; + + break; + } + // Sink requests playback to start/resume IpcPacket::StartPlayback => { check_result(ipc_track.play()); @@ -217,6 +246,8 @@ impl SpoticordSession { let mut instance = ipc_instance.clone(); let context = ipc_context.clone(); + // TODO: Check if this is actually needed + // The new backend may have fixed the need for this tokio::spawn(async move { if let Err(why) = instance.update_track(&context, &owner_id, track_id).await { error!("Failed to update track: {:?}", why); @@ -274,7 +305,8 @@ impl SpoticordSession { } IpcPacket::Stopped => { - ipc_instance.player_stopped().await; + check_result(ipc_track.pause()); + ipc_instance.start_disconnect_timer().await; } // Ignore other packets @@ -441,6 +473,9 @@ impl SpoticordSession { // Clear playback info let mut playback_info = self.playback_info.write().await; *playback_info = None; + + // Disconnect automatically after some time + self.start_disconnect_timer().await; } /// Internal version of disconnect, which does not abort the disconnect timer From 66e4579e5ad886f4b403632ae4eddff032ca647e Mon Sep 17 00:00:00 2001 From: DaXcess Date: Tue, 8 Nov 2022 14:13:13 +0100 Subject: [PATCH 2/4] Finish documentation, clean up commands --- CONTRIBUTING.md | 39 +++++++++++++++++++++++++++++++ src/bot/commands/core/help.rs | 10 ++++++-- src/bot/commands/core/link.rs | 4 ++-- src/bot/commands/music/join.rs | 9 +------ src/bot/commands/music/leave.rs | 1 - src/bot/commands/music/playing.rs | 34 ++++++++++++++------------- 6 files changed, 68 insertions(+), 29 deletions(-) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..0a002e0 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,39 @@ +# Contributing + +## How to contribute + +### By reporting bugs + +If you find a bug, please report it to the [issue tracker](https://github.com/SpoticordMusic/spoticord) on GitHub. When reporting bugs, it is recommended that you make use of the provided template. + +When filing your bug, please be as precise as possible. Bugs that can be reproduced by anyone are much easier to fix. If you can, please include a minimal test case that demonstrates the bug. This makes it much easier to track down the bug. + +### By suggesting new features + +If you have an idea for a new feature, please suggest it by creating a new issue on the [issue tracker][issues] on GitHub. When suggesting new features, it is recommended that you make use of the provided template. If you think that your feature is related to an existing issue, please mention it in your description. + +If you want to suggest new features more casually, rather than officially here on GitHub, you can join our [Discord server](https://discord.gg/wRCyhVqBZ5) and discuss your ideas with us there. + +### By writing code + +If you want to contribute code, you can do so through GitHub by forking the repository and sending a pull request. + +It is generally recommended that you create an issue on the [issue tracker](https://github.com/SpoticordMusic/spoticord) on GitHub before you start working on a feature. This allows us to discuss the feature and make sure that it is something that we want to add to the project. If you are not sure whether a feature is something that we want to add, you can always ask us on our [Discord server](https://discord.gg/wRCyhVqBZ5). + +The flow will look something like this: + +1. Fork the repository on GitHub +2. Create a named feature branch (like `add_component_x`) +3. Write your change +4. Test your change +5. Submit a Pull Request to the dev branch + +A member of the team will review your pull request and either merge it, request changes to it, or close it with an explanation. + +### Code style + +When writing code we ask you to code in a way that is consistent with the rest of the codebase. This means that you should use the same indentation style, naming conventions, etc. as the rest of the codebase. + +We make use of `rustfmt` to format our code. You can install it by running `rustup component add rustfmt` and then running `cargo fmt --all` to format your code. It is generally recommended to run this command before you commit your code. If you use VSCode, you can install the [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer) extension and enable the `Format on Save` option. + +Also, **please do not write code that may panic**. If you are not sure how to handle a case, please use `Result` or `Option` to handle it. \ No newline at end of file diff --git a/src/bot/commands/core/help.rs b/src/bot/commands/core/help.rs index 071113f..3efa547 100644 --- a/src/bot/commands/core/help.rs +++ b/src/bot/commands/core/help.rs @@ -19,8 +19,14 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu EmbedBuilder::new() .title("Spoticord Help") .icon_url("https://spoticord.com/img/logo-standard.webp") - .description(format!("Click **[here](https://spoticord.com/commands)** for a list of commands.\n{}", - "If you need help setting Spoticord up you can check out the **[Documentation](https://spoticord.com/documentation)** page on the Spoticord website.\n\n")) + .description(format!("**Welcome to Spoticord** + It seems you have requested some help. Not to worry, we can help you out.\n + **Not sure how the bot works?** + **[Click here](https://spoticord.com/#how-to)** for a quick overview about how to set up Spoticord and how to use it.\n + **Which commands are there?** + You can find all **[the commands](https://spoticord.com/#commands)** on the website. You may also just type `/` in Discord and see which commands are available there.\n + **Need more help?** + If you still need some help, whether you are having issues with the bot or you just want to give us some feedback, you can join our **[Discord server](https://discord.gg/wRCyhVqBZ5)**.")) .status(Status::Info) .build(), false, diff --git a/src/bot/commands/core/link.rs b/src/bot/commands/core/link.rs index bd5ab6a..9078625 100644 --- a/src/bot/commands/core/link.rs +++ b/src/bot/commands/core/link.rs @@ -44,7 +44,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu EmbedBuilder::new() .title("Link your Spotify account") .title_url(&link) - .icon_url("https://spoticord.com/img/spotify-logo.png") + .icon_url("https://spoticord.com/spotify-logo.png") .description(format!( "Go to [this link]({}) to connect your Spotify account.", link @@ -111,7 +111,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu EmbedBuilder::new() .title("Link your Spotify account") .title_url(&link) - .icon_url("https://spoticord.com/img/spotify-logo.png") + .icon_url("https://spoticord.com/spotify-logo.png") .description(format!( "Go to [this link]({}) to connect your Spotify account.", link diff --git a/src/bot/commands/music/join.rs b/src/bot/commands/music/join.rs index 593e4d5..e5205df 100644 --- a/src/bot/commands/music/join.rs +++ b/src/bot/commands/music/join.rs @@ -30,7 +30,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("Cannot join voice channel") - .icon_url("https://spoticord.com/static/image/prohibited.png") .description("You need to connect to a voice channel") .status(Status::Error) .build(), @@ -61,7 +60,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("Cannot join voice channel") - .icon_url("https://spoticord.com/static/image/prohibited.png") .description(msg) .status(Status::Error) .build(), @@ -80,7 +78,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("Cannot join voice channel") - .icon_url("https://spoticord.com/static/image/prohibited.png") .description( format!( "You are already playing music in another server ({}).\nStop playing in that server first before joining this one.", @@ -115,7 +112,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("Cannot join voice channel") - .icon_url("https://spoticord.com/static/image/prohibited.png") .description("You need to link your Spotify account. Use or go to [the accounts website](https://account.spoticord.com/) to get started.") .status(Status::Error) .build(), @@ -131,7 +127,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("Cannot join voice channel") - .icon_url("https://spoticord.com/static/image/prohibited.png") .description("An error occured while joining the channel. Please try again later.") .status(Status::Error) .build(), @@ -159,7 +154,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("Cannot join voice channel") - .icon_url("https://spoticord.com/static/image/prohibited.png") .description("You need to link your Spotify account. Use or go to [the accounts website](https://account.spoticord.com/) to get started.") .status(Status::Error) .build(), @@ -175,7 +169,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("Cannot join voice channel") - .icon_url("https://spoticord.com/static/image/prohibited.png") .description("An error occured while joining the channel. Please try again later.") .status(Status::Error) .build(), @@ -191,7 +184,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("Connected to voice channel") - .icon_url("https://spoticord.com/static/image/speaker.png") + .icon_url("https://spoticord.com/speaker.png") .description(format!("Come listen along in <#{}>", channel_id)) .footer("Spotify will automatically start playing on Spoticord") .status(Status::Info) diff --git a/src/bot/commands/music/leave.rs b/src/bot/commands/music/leave.rs index 354bd80..36802ab 100644 --- a/src/bot/commands/music/leave.rs +++ b/src/bot/commands/music/leave.rs @@ -25,7 +25,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu &command, EmbedBuilder::new() .title("Cannot disconnect bot") - .icon_url("https://tabler-icons.io/static/tabler-icons/icons/ban.svg") .description("I'm currently not connected to any voice channel") .status(Status::Error) .build(), diff --git a/src/bot/commands/music/playing.rs b/src/bot/commands/music/playing.rs index 68e2d15..87fa68e 100644 --- a/src/bot/commands/music/playing.rs +++ b/src/bot/commands/music/playing.rs @@ -149,22 +149,24 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu response .kind(InteractionResponseType::ChannelMessageWithSource) .interaction_response_data(|message| { - message - .embed(|embed| embed - .author(|author| author - .name("Currently Playing") - .icon_url("https://www.freepnglogos.com/uploads/spotify-logo-png/file-spotify-logo-png-4.png") - ) - .title(title) - .url(format!("https://open.spotify.com/{}/{}", audio_type, spotify_id.to_base62().unwrap())) - .description(description) - .footer(|footer| footer - .text(&owner.name) - .icon_url(owner.face()) - ) - .thumbnail(&thumbnail) - .color(Status::Info as u64) - ) + message.embed(|embed| { + embed + .author(|author| { + author + .name("Currently Playing") + .icon_url("https://spoticord.com/spotify-logo.png") + }) + .title(title) + .url(format!( + "https://open.spotify.com/{}/{}", + audio_type, + spotify_id.to_base62().unwrap() + )) + .description(description) + .footer(|footer| footer.text(&owner.name).icon_url(owner.face())) + .thumbnail(&thumbnail) + .color(Status::Info as u64) + }) }) }) .await From 23ef5b8ca9cf0c5a62589bf14f19721a887d7932 Mon Sep 17 00:00:00 2001 From: DaXcess <46288749+DaXcess@users.noreply.github.com> Date: Tue, 8 Nov 2022 14:14:03 +0100 Subject: [PATCH 3/4] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 460471c..56919e4 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,13 @@ Environment variables set this way take precedence over those in the `.env` file # Compiling For information about how to compile Spoticord from source, check out [COMPILING.md](COMPILING.md). +# Contributing +For information about how to contribute to Spoticord, check out [CONTRIBUTING.md](CONTRIBUTING.md). + # Contact ![Discord Shield](https://discordapp.com/api/guilds/779292533053456404/widget.png?style=shield) If you have any questions, feel free to join the [Spoticord Discord server](https://discord.gg/wRCyhVqBZ5)! # License -Spoticord is licensed under the [Apache License 2.0](LICENSE). \ No newline at end of file +Spoticord is licensed under the [Apache License 2.0](LICENSE). From c58e87ff905c1f018120dfad21852a468ca7b937 Mon Sep 17 00:00:00 2001 From: DaXcess Date: Tue, 8 Nov 2022 14:14:30 +0100 Subject: [PATCH 4/4] Add contributing to README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 460471c..c51459f 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ Environment variables set this way take precedence over those in the `.env` file # Compiling For information about how to compile Spoticord from source, check out [COMPILING.md](COMPILING.md). +# Contributing +For information about how to contribute to Spoticord, check out [CONTRIBUTING.md](CONTRIBUTING.md). + # Contact ![Discord Shield](https://discordapp.com/api/guilds/779292533053456404/widget.png?style=shield)