Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 22 additions & 31 deletions Cargo.lock

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

29 changes: 11 additions & 18 deletions src/event/event_repeater.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use log::error;
use std::{
collections::HashMap,
sync::{Arc, Weak},
sync::{Arc, OnceLock, Weak},
};
use thiserror::Error;
use tokio::{sync::Mutex, task::JoinHandle};
use uuid::Uuid;

use crate::setlock::{SetLock, SetLockError};

use super::{Event, Subscription};

#[derive(Debug, Error)]
Expand Down Expand Up @@ -53,7 +51,7 @@ where
T: Send + Sync + 'static,
{
pub event: Event<T>,
weak: Mutex<SetLock<Weak<Self>>>,
weak: OnceLock<Weak<Self>>,
subscriptions: Mutex<HashMap<Uuid, (Subscription, JoinHandle<()>)>>,
}

Expand All @@ -68,25 +66,21 @@ where
{
let event = Event::new(name);
let event_repeater = Self {
weak: Mutex::new(SetLock::new()),
weak: OnceLock::new(),
event,
subscriptions: Mutex::new(HashMap::new()),
};

let arc = Arc::new(event_repeater);
let weak = Arc::downgrade(&arc);

let result = arc.weak.lock().await.set(weak);
if let Err(err) = result {
match err {
SetLockError::AlreadySet => {
error!("Failed to set EventRepeater {}'s Weak self-reference because it was already set. This should never happen. Shutting down ungracefully to prevent further undefined behavior.", arc.event.name);
unreachable!(
"Unable to set EventRepeater {}'s Weak self-reference because it was already set.",
arc.event.name
);
}
}
let result = arc.weak.set(weak);
if result.is_err() {
error!("Failed to set EventRepeater {}'s Weak self-reference because it was already set. This should never happen. Shutting down ungracefully to prevent further undefined behavior.", arc.event.name);
unreachable!(
"Unable to set EventRepeater {}'s Weak self-reference because it was already set.",
arc.event.name
);
}

arc
Expand All @@ -97,8 +91,7 @@ where
}

pub async fn attach(&self, event: &Event<T>, buffer: usize) -> Result<(), AttachError> {
let lock = self.weak.lock().await;
let weak = match lock.get() {
let weak = match self.weak.get() {
Some(weak) => weak,
None => {
return Err(AttachError::NotInitialized {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod config;
pub mod event;
pub mod log;
pub mod service;
pub mod setlock;

pub fn is_debug() -> bool {
cfg!(debug_assertions)
Expand Down
79 changes: 44 additions & 35 deletions src/service/discord.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use crate::setlock::SetLock;

use super::{types::LifetimedPinnedBoxedFutureResult, Priority, Service, ServiceInfo, ServiceManager};
use log::{error, info};
use log::{error, info, warn};
use serenity::{
all::{GatewayIntents, Ready},
async_trait,
client::{self, Cache, Context},
framework::{standard::Configuration, StandardFramework},

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (macos-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (macos-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (windows-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (windows-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (macos-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (macos-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (windows-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 7 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (windows-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling
gateway::{ShardManager, VoiceGatewayManager},
http::Http,
prelude::TypeMap,
Client, Error,
};
use std::{sync::Arc, time::Duration};
use std::{
sync::{Arc, OnceLock},
time::Duration,
};
use tokio::{
select, spawn,
sync::{Mutex, Notify, RwLock},
Expand All @@ -24,29 +25,29 @@
pub struct DiscordService {
info: ServiceInfo,
discord_token: String,
pub ready: Arc<Mutex<SetLock<Ready>>>,
pub ready: Arc<OnceLock<Ready>>,
client_handle: Option<JoinHandle<Result<(), Error>>>,
pub cache: SetLock<Arc<Cache>>,
pub data: SetLock<Arc<RwLock<TypeMap>>>,
pub http: SetLock<Arc<Http>>,
pub shard_manager: SetLock<Arc<ShardManager>>,
pub voice_manager: SetLock<Arc<dyn VoiceGatewayManager>>,
pub ws_url: SetLock<Arc<Mutex<String>>>,
pub cache: OnceLock<Arc<Cache>>,
pub data: OnceLock<Arc<RwLock<TypeMap>>>,
pub http: OnceLock<Arc<Http>>,
pub shard_manager: OnceLock<Arc<ShardManager>>,
pub voice_manager: OnceLock<Arc<dyn VoiceGatewayManager>>,
pub ws_url: OnceLock<Arc<Mutex<String>>>,
}

impl DiscordService {
pub fn new(discord_token: &str) -> Self {
Self {
info: ServiceInfo::new("lum_builtin_discord", "Discord", Priority::Essential),
discord_token: discord_token.to_string(),
ready: Arc::new(Mutex::new(SetLock::new())),
ready: Arc::new(OnceLock::new()),
client_handle: None,
cache: SetLock::new(),
data: SetLock::new(),
http: SetLock::new(),
shard_manager: SetLock::new(),
voice_manager: SetLock::new(),
ws_url: SetLock::new(),
cache: OnceLock::new(),
data: OnceLock::new(),
http: OnceLock::new(),
shard_manager: OnceLock::new(),
voice_manager: OnceLock::new(),
ws_url: OnceLock::new(),
}
}
}
Expand All @@ -60,8 +61,8 @@
Box::pin(async move {
let client_ready_notify = Arc::new(Notify::new());

let framework = StandardFramework::new();

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-64)

use of deprecated associated function `serenity::framework::StandardFramework::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (macos-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (macos-64)

use of deprecated associated function `serenity::framework::StandardFramework::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (windows-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (windows-64)

use of deprecated associated function `serenity::framework::StandardFramework::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-64)

use of deprecated associated function `serenity::framework::StandardFramework::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (macos-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (macos-64)

use of deprecated associated function `serenity::framework::StandardFramework::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (windows-64)

use of deprecated struct `serenity::framework::StandardFramework`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 64 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (windows-64)

use of deprecated associated function `serenity::framework::StandardFramework::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling
framework.configure(Configuration::new().prefix("!"));

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-64)

use of deprecated method `serenity::framework::StandardFramework::configure`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-64)

use of deprecated associated function `serenity::framework::standard::Configuration::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-64)

use of deprecated method `serenity::framework::standard::Configuration::prefix`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (macos-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (macos-64)

use of deprecated method `serenity::framework::StandardFramework::configure`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (macos-64)

use of deprecated associated function `serenity::framework::standard::Configuration::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (macos-64)

use of deprecated method `serenity::framework::standard::Configuration::prefix`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (windows-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (windows-64)

use of deprecated method `serenity::framework::StandardFramework::configure`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (windows-64)

use of deprecated associated function `serenity::framework::standard::Configuration::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Build (windows-64)

use of deprecated method `serenity::framework::standard::Configuration::prefix`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-64)

use of deprecated method `serenity::framework::StandardFramework::configure`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-64)

use of deprecated associated function `serenity::framework::standard::Configuration::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-64)

use of deprecated method `serenity::framework::standard::Configuration::prefix`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (macos-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (macos-64)

use of deprecated method `serenity::framework::StandardFramework::configure`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (macos-64)

use of deprecated associated function `serenity::framework::standard::Configuration::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (macos-64)

use of deprecated method `serenity::framework::standard::Configuration::prefix`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (windows-64)

use of deprecated struct `serenity::framework::standard::Configuration`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (windows-64)

use of deprecated method `serenity::framework::StandardFramework::configure`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (windows-64)

use of deprecated associated function `serenity::framework::standard::Configuration::new`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

Check warning on line 65 in src/service/discord.rs

View workflow job for this annotation

GitHub Actions / Test (windows-64)

use of deprecated method `serenity::framework::standard::Configuration::prefix`: The standard framework is deprecated, and will be removed in 0.13. Please migrate to `poise` for command handling

let mut client = Client::builder(self.discord_token.as_str(), GatewayIntents::all())
.framework(framework)
Expand All @@ -71,30 +72,38 @@
))
.await?;

if let Err(error) = self.cache.set(Arc::clone(&client.cache)) {
return Err(format!("Failed to set cache SetLock: {}", error).into());
if self.cache.set(Arc::clone(&client.cache)).is_err() {
error!("Could not set cache OnceLock because it was already set. This should never happen.");
return Err("Could not set cache OnceLock because it was already set.".into());
}

if let Err(error) = self.data.set(Arc::clone(&client.data)) {
return Err(format!("Failed to set data SetLock: {}", error).into());
if self.data.set(Arc::clone(&client.data)).is_err() {
error!("Could not set data OnceLock because it was already set. This should never happen.");
return Err("Could not set data OnceLock because it was already set.".into());
}

if let Err(error) = self.http.set(Arc::clone(&client.http)) {
return Err(format!("Failed to set http SetLock: {}", error).into());
if self.http.set(Arc::clone(&client.http)).is_err() {
error!("Could not set http OnceLock because it was already set. This should never happen.");
return Err("Could not set http OnceLock because it was already set.".into());
}

if let Err(error) = self.shard_manager.set(Arc::clone(&client.shard_manager)) {
return Err(format!("Failed to set shard_manager SetLock: {}", error).into());
if self.shard_manager.set(Arc::clone(&client.shard_manager)).is_err() {
error!("Could not set shard_manager OnceLock because it was already set. This should never happen.");
return Err("Could not set shard_manager OnceLock because it was already set.".into());
}

if let Some(voice_manager) = &client.voice_manager {
if let Err(error) = self.voice_manager.set(Arc::clone(voice_manager)) {
return Err(format!("Failed to set voice_manager SetLock: {}", error).into());
if self.voice_manager.set(Arc::clone(voice_manager)).is_err() {
error!("Could not set voice_manager OnceLock because it was already set. This should never happen.");
return Err("Could not set voice_manager OnceLock because it was already set.".into());
}
} else {
warn!("Voice manager is not available");
}

if let Err(error) = self.ws_url.set(Arc::clone(&client.ws_url)) {
return Err(format!("Failed to set ws_url SetLock: {}", error).into());
if self.ws_url.set(Arc::clone(&client.ws_url)).is_err() {
error!("Could not set ws_url OnceLock because it was already set. This should never happen.");
return Err("Could not set ws_url OnceLock because it was already set.".into());
}

let client_handle = spawn(async move { client.start().await });
Expand Down Expand Up @@ -138,12 +147,12 @@
}

struct EventHandler {
client: Arc<Mutex<SetLock<Ready>>>,
client: Arc<OnceLock<Ready>>,
ready_notify: Arc<Notify>,
}

impl EventHandler {
pub fn new(client: Arc<Mutex<SetLock<Ready>>>, ready_notify: Arc<Notify>) -> Self {
pub fn new(client: Arc<OnceLock<Ready>>, ready_notify: Arc<Notify>) -> Self {
Self { client, ready_notify }
}
}
Expand All @@ -152,9 +161,9 @@
impl client::EventHandler for EventHandler {
async fn ready(&self, _ctx: Context, data_about_bot: Ready) {
info!("Connected to Discord as {}", data_about_bot.user.tag());
if let Err(error) = self.client.lock().await.set(data_about_bot) {
error!("Failed to set client SetLock: {}", error);
panic!("Failed to set client SetLock: {}", error);
if self.client.set(data_about_bot).is_err() {
error!("Could not set client OnceLock because it was already set. This should never happen.");
panic!("Could not set client OnceLock because it was already set");
}
self.ready_notify.notify_one();
}
Expand Down
Loading
Loading