Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the bot follow multiple users #32

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
name = "aoede"
version = "0.6.0"
authors = ["Max Isom <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
librespot = {version = "0.4.1", default-features = false}
librespot = {version = "0.4.2", default-features = false}
songbird = "0.3.0"
tracing = "0.1"
tracing-subscriber = "0.2"
tracing-futures = "0.2"
tokio = { version = "1.20.1", features = ["default"] }
tokio = { version = "1.23.0", features = ["default"] }
byteorder = "1.4.3"
serde = "1.0"
figment = { version = "0.10", features = ["toml", "env"] }
rubato = "0.12.0"

[dependencies.serenity]
version = "0.11.2"
features = ["client"]
version = "0.11.5"
features = ["client", "utils"]

[profile.dev]
split-debuginfo = "unpacked"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ services:
- DISCORD_TOKEN=
- SPOTIFY_USERNAME=
- SPOTIFY_PASSWORD=
- DISCORD_USER_ID= # Discord user ID of the user you want Aoede to follow
- DISCORD_ADMINS= # List (wrapped in single quotes) of users u want the bot to keep track of, eg value: '["100","200"]'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I don't like the name DISCORD_ADMINS. Someone could think that he must list the discord server admins / owners here. Why not DISCORD_USERS?

```

### Docker:
Expand All @@ -52,7 +52,7 @@ services:
DISCORD_TOKEN=
SPOTIFY_USERNAME=
SPOTIFY_PASSWORD=
DISCORD_USER_ID=
DISCORD_ADMINS=
```

```bash
Expand Down
2 changes: 1 addition & 1 deletion config.sample.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DISCORD_TOKEN="the discord bot token"
SPOTIFY_USERNAME="your spotify email"
SPOTIFY_PASSWORD="your spotify password"
DISCORD_USER_ID="your discord id here"
DISCORD_ADMINS=["list of ids who can control the bot"]
4 changes: 2 additions & 2 deletions src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub struct Config {
pub spotify_username: String,
#[serde(alias = "SPOTIFY_PASSWORD")]
pub spotify_password: String,
#[serde(alias = "DISCORD_USER_ID")]
pub discord_user_id: u64,
#[serde(alias = "DISCORD_ADMINS")]
pub discord_admins: Vec<String>,
}

impl Config {
Expand Down
57 changes: 38 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,22 @@ impl EventHandler for Handler {
.guild(guild_id)
.expect("Could not find guild in cache.");

let channel_id = guild
.voice_states
.get(&config.discord_user_id.into())
.and_then(|voice_state| voice_state.channel_id);
drop(guild);
// Check if any of the admins are in the VC
let user_list = guild.voice_states;

let mut channel_ids: Vec<Option<id::ChannelId>> = Vec::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need one channel_id later on. Just use an Option<id::ChannelId> and move the is_some() check into the loop in line 71.

Copy link
Contributor

@sibartel sibartel Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to store it at all, just connect directly if a valid channel is found (and then break).


for (key, value) in user_list {
if config.discord_admins.contains(&key.to_string()) {
channel_ids.push(value.channel_id);
}
}

if channel_id.is_some() {
// Enable casting
player.lock().await.enable_connect().await;
for i in channel_ids {
if i.is_some() {
// Enable casting
player.lock().await.enable_connect().await;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break here to prevent multiple connect attempts.

}
}

let c = ctx.clone();
Expand Down Expand Up @@ -114,17 +121,29 @@ impl EventHandler for Handler {
.guild(guild_id)
.expect("Could not find guild in cache.");

let channel_id = match guild
.voice_states
.get(&config.discord_user_id.into())
.and_then(|voice_state| voice_state.channel_id)
{
Some(channel_id) => channel_id,
None => {
println!("Could not find user in VC.");
continue;
// Check if any of the admins are in the VC
let user_list = guild.voice_states;

let mut channel_ids: Vec<Option<id::ChannelId>> = Vec::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use here an vector if in fact you only need one single channel_id later on?

Add the is_some check of line 142 as a condition to the if in 130 and only assign if a valid channel was found.
The var channel_id should then be an Option<id::ChannelId> initialized to None.

Then you can check if it is still None after your loop, if that's the case log that the 'admin' was not found. If it is some, join the channel.


for (key, value) in user_list {
if config.discord_admins.contains(&key.to_string()) {
channel_ids.push(value.channel_id);
}
};
}

if channel_ids.len() == 0 {
println!("Admin not in VC!");
continue;
}

let mut channel_id = id::ChannelId(0);
for i in channel_ids {
if i.is_some() {
// Choose the first channel we encounter which is non-null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment doesn't match your code. You are taking the last channel which is some. Later loop iterations are overwriting your channel_id.

An early break would make sense here.

Copy link
Contributor

@sibartel sibartel Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments above / below, this extra loop should not be necessary at all.

channel_id = i.unwrap();
}
}

let _handler = manager.join(guild_id, channel_id).await;

Expand Down Expand Up @@ -195,7 +214,7 @@ impl EventHandler for Handler {

let config = data.get::<ConfigKey>().unwrap();

if new.user_id.to_string() != config.discord_user_id.to_string() {
if ! config.discord_admins.contains(&new.user_id.to_string()) {
return;
}

Expand Down