Remove log and switch to tracing

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 3 years ago
parent 4a5d1e6122
commit 9d75afc8c1
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

553
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -16,13 +16,13 @@ rustls_backend = ["serenity/rustls_backend"]
native_tls_backend = ["serenity/native_tls_backend"] native_tls_backend = ["serenity/native_tls_backend"]
[dependencies] [dependencies]
tokio = "1.4.0" tokio = "1.17.0"
thiserror = "1.0.24" thiserror = "1.0.30"
log = "0.4.14" tracing= "0.1.33"
futures = "0.3.14" futures = "0.3.21"
serde_json = "1.0.64" serde_json = "1.0.79"
[dependencies.serenity] [dependencies.serenity]
version = "0.10.5" version = "0.10.10"
default-features = false default-features = false
features = ["builder", "http", "cache", "model", "client", "gateway"] features = ["builder", "http", "cache", "model", "client", "gateway"]

@ -10,6 +10,7 @@ use std::time::Duration;
pub struct EphemeralMessage; pub struct EphemeralMessage;
impl EphemeralMessage { impl EphemeralMessage {
#[tracing::instrument(level = "debug", skip(http, message))]
/// Ensures that an already existing message is /// Ensures that an already existing message is
/// deleted after a certain amount of time /// deleted after a certain amount of time
pub async fn create_from_message( pub async fn create_from_message(
@ -17,20 +18,20 @@ impl EphemeralMessage {
message: &Message, message: &Message,
timeout: Duration, timeout: Duration,
) -> Result<()> { ) -> Result<()> {
log::debug!("Creating ephemeral message from existing message"); tracing::debug!("Creating ephemeral message from existing message");
let handle = MessageHandle::new(message.channel_id, message.id); let handle = MessageHandle::new(message.channel_id, message.id);
let http = Arc::clone(&http); let http = Arc::clone(&http);
log::debug!("Starting delete task"); tracing::debug!("Starting delete task");
tokio::spawn(async move { tokio::spawn(async move {
log::debug!("Waiting for timeout to pass"); tracing::debug!("Waiting for timeout to pass");
tokio::time::sleep(timeout).await; tokio::time::sleep(timeout).await;
log::debug!("Deleting ephemeral message"); tracing::debug!("Deleting ephemeral message");
if let Err(e) = http if let Err(e) = http
.delete_message(handle.channel_id, handle.message_id) .delete_message(handle.channel_id, handle.message_id)
.await .await
{ {
log::error!("Failed to delete ephemeral message {:?}: {}", handle, e); tracing::error!("Failed to delete ephemeral message {:?}: {}", handle, e);
} }
}); });
@ -38,6 +39,7 @@ impl EphemeralMessage {
} }
/// Creates a new message that is deleted after a certain amount of time /// Creates a new message that is deleted after a certain amount of time
#[tracing::instrument(level = "debug", skip(http, f))]
pub async fn create<'a, F>( pub async fn create<'a, F>(
http: &Arc<Http>, http: &Arc<Http>,
channel_id: ChannelId, channel_id: ChannelId,
@ -47,7 +49,7 @@ impl EphemeralMessage {
where where
F: for<'b> FnOnce(&'b mut CreateMessage<'a>) -> &'b mut CreateMessage<'a>, F: for<'b> FnOnce(&'b mut CreateMessage<'a>) -> &'b mut CreateMessage<'a>,
{ {
log::debug!("Creating new ephemeral message"); tracing::debug!("Creating new ephemeral message");
let msg = channel_id.send_message(http, f).await?; let msg = channel_id.send_message(http, f).await?;
Self::create_from_message(http, &msg, timeout).await?; Self::create_from_message(http, &msg, timeout).await?;

@ -10,6 +10,7 @@ use tokio::time::Duration;
static UPDATE_INTERVAL_SECS: u64 = 5; static UPDATE_INTERVAL_SECS: u64 = 5;
/// Starts the loop to handle message updates /// Starts the loop to handle message updates
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn start_update_loop(ctx: &Context) -> Result<()> { pub async fn start_update_loop(ctx: &Context) -> Result<()> {
let event_messages = get_listeners_from_context(ctx) let event_messages = get_listeners_from_context(ctx)
.await .await
@ -19,7 +20,7 @@ pub async fn start_update_loop(ctx: &Context) -> Result<()> {
tokio::task::spawn(async move { tokio::task::spawn(async move {
loop { loop {
{ {
log::trace!("Locking listener from update loop."); tracing::debug!("Updating messages...");
let messages = { let messages = {
let msgs_lock = event_messages.lock().await; let msgs_lock = event_messages.lock().await;
@ -28,13 +29,12 @@ pub async fn start_update_loop(ctx: &Context) -> Result<()> {
.map(|(k, v)| (*k, v.clone())) .map(|(k, v)| (*k, v.clone()))
.collect::<Vec<(MessageHandle, MessageRef)>>() .collect::<Vec<(MessageHandle, MessageRef)>>()
}; };
log::trace!("Listener locked.");
let mut frozen_messages = Vec::new(); let mut frozen_messages = Vec::new();
for (key, msg) in messages { for (key, msg) in messages {
let mut msg = msg.lock().await; let mut msg = msg.lock().await;
if let Err(e) = msg.update(&http).await { if let Err(e) = msg.update(&http).await {
log::error!("Failed to update message: {:?}", e); tracing::error!("Failed to update message: {:?}", e);
} }
if msg.is_frozen() { if msg.is_frozen() {
frozen_messages.push(key); frozen_messages.push(key);
@ -46,8 +46,8 @@ pub async fn start_update_loop(ctx: &Context) -> Result<()> {
msgs_lock.remove(&key); msgs_lock.remove(&key);
} }
} }
tracing::debug!("Messages updated");
} }
log::trace!("Listener unlocked");
tokio::time::sleep(Duration::from_secs(UPDATE_INTERVAL_SECS)).await; tokio::time::sleep(Duration::from_secs(UPDATE_INTERVAL_SECS)).await;
} }
}); });
@ -56,6 +56,7 @@ pub async fn start_update_loop(ctx: &Context) -> Result<()> {
} }
/// To be fired from the serenity handler when a message was deleted /// To be fired from the serenity handler when a message was deleted
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn handle_message_delete( pub async fn handle_message_delete(
ctx: &Context, ctx: &Context,
channel_id: ChannelId, channel_id: ChannelId,
@ -64,9 +65,7 @@ pub async fn handle_message_delete(
let mut affected_messages = Vec::new(); let mut affected_messages = Vec::new();
{ {
let listeners = get_listeners_from_context(ctx).await?; let listeners = get_listeners_from_context(ctx).await?;
log::trace!("Locking listener from handle_message_delete.");
let mut listeners_lock = listeners.lock().await; let mut listeners_lock = listeners.lock().await;
log::trace!("Listener locked.");
let handle = MessageHandle::new(channel_id, message_id); let handle = MessageHandle::new(channel_id, message_id);
if let Some(msg) = listeners_lock.get(&handle) { if let Some(msg) = listeners_lock.get(&handle) {
@ -74,7 +73,6 @@ pub async fn handle_message_delete(
listeners_lock.remove(&handle); listeners_lock.remove(&handle);
} }
} }
log::trace!("Listener unlocked");
for msg in affected_messages { for msg in affected_messages {
let mut msg = msg.lock().await; let mut msg = msg.lock().await;
msg.on_deleted(ctx).await?; msg.on_deleted(ctx).await?;
@ -84,6 +82,7 @@ pub async fn handle_message_delete(
} }
/// To be fired from the serenity handler when multiple messages were deleted /// To be fired from the serenity handler when multiple messages were deleted
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn handle_message_delete_bulk( pub async fn handle_message_delete_bulk(
ctx: &Context, ctx: &Context,
channel_id: ChannelId, channel_id: ChannelId,
@ -92,9 +91,7 @@ pub async fn handle_message_delete_bulk(
let mut affected_messages = Vec::new(); let mut affected_messages = Vec::new();
{ {
let listeners = get_listeners_from_context(ctx).await?; let listeners = get_listeners_from_context(ctx).await?;
log::trace!("Locking listener from handle_message_delete_bulk.");
let mut listeners_lock = listeners.lock().await; let mut listeners_lock = listeners.lock().await;
log::trace!("Listener locked.");
for message_id in message_ids { for message_id in message_ids {
let handle = MessageHandle::new(channel_id, *message_id); let handle = MessageHandle::new(channel_id, *message_id);
@ -104,7 +101,6 @@ pub async fn handle_message_delete_bulk(
} }
} }
} }
log::trace!("Listener unlocked");
for msg in affected_messages { for msg in affected_messages {
let mut msg = msg.lock().await; let mut msg = msg.lock().await;
msg.on_deleted(ctx).await?; msg.on_deleted(ctx).await?;
@ -114,13 +110,12 @@ pub async fn handle_message_delete_bulk(
} }
/// Fired when a reaction was added to a message /// Fired when a reaction was added to a message
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn handle_reaction_add(ctx: &Context, reaction: &Reaction) -> Result<()> { pub async fn handle_reaction_add(ctx: &Context, reaction: &Reaction) -> Result<()> {
let mut affected_messages = Vec::new(); let mut affected_messages = Vec::new();
{ {
let listeners = get_listeners_from_context(ctx).await?; let listeners = get_listeners_from_context(ctx).await?;
log::trace!("Locking listener from handle_reaction_add.");
let mut listeners_lock = listeners.lock().await; let mut listeners_lock = listeners.lock().await;
log::trace!("Listener locked.");
let handle = MessageHandle::new(reaction.channel_id, reaction.message_id); let handle = MessageHandle::new(reaction.channel_id, reaction.message_id);
@ -128,7 +123,6 @@ pub async fn handle_reaction_add(ctx: &Context, reaction: &Reaction) -> Result<(
affected_messages.push(Arc::clone(&msg)); affected_messages.push(Arc::clone(&msg));
} }
} }
log::trace!("Listener unlocked");
for msg in affected_messages { for msg in affected_messages {
let mut msg = msg.lock().await; let mut msg = msg.lock().await;
msg.on_reaction_add(ctx, reaction.clone()).await?; msg.on_reaction_add(ctx, reaction.clone()).await?;
@ -138,13 +132,12 @@ pub async fn handle_reaction_add(ctx: &Context, reaction: &Reaction) -> Result<(
} }
/// Fired when a reaction was added to a message /// Fired when a reaction was added to a message
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn handle_reaction_remove(ctx: &Context, reaction: &Reaction) -> Result<()> { pub async fn handle_reaction_remove(ctx: &Context, reaction: &Reaction) -> Result<()> {
let mut affected_messages = Vec::new(); let mut affected_messages = Vec::new();
{ {
let listeners = get_listeners_from_context(ctx).await?; let listeners = get_listeners_from_context(ctx).await?;
log::trace!("Locking listener from handle_reaction_remove.");
let mut listeners_lock = listeners.lock().await; let mut listeners_lock = listeners.lock().await;
log::trace!("Listener locked.");
let handle = MessageHandle::new(reaction.channel_id, reaction.message_id); let handle = MessageHandle::new(reaction.channel_id, reaction.message_id);
@ -152,7 +145,6 @@ pub async fn handle_reaction_remove(ctx: &Context, reaction: &Reaction) -> Resul
affected_messages.push(Arc::clone(&msg)); affected_messages.push(Arc::clone(&msg));
} }
} }
log::trace!("Listener unlocked");
for msg in affected_messages { for msg in affected_messages {
let mut msg = msg.lock().await; let mut msg = msg.lock().await;
msg.on_reaction_remove(ctx, reaction.clone()).await?; msg.on_reaction_remove(ctx, reaction.clone()).await?;

@ -49,6 +49,7 @@ pub struct RichEventHandler {
impl RichEventHandler { impl RichEventHandler {
/// Handles a generic event /// Handles a generic event
#[tracing::instrument(level = "debug", skip_all)]
async fn handle_event<T: 'static + Send + Sync>(&self, ctx: Context, value: T) { async fn handle_event<T: 'static + Send + Sync>(&self, ctx: Context, value: T) {
let callbacks = self.callbacks.clone(); let callbacks = self.callbacks.clone();
@ -58,7 +59,7 @@ impl RichEventHandler {
for callback in callbacks { for callback in callbacks {
if let Some(cb) = callback.downcast_ref::<EventCallback<T>>() { if let Some(cb) = callback.downcast_ref::<EventCallback<T>>() {
if let Err(e) = cb.run(&ctx, &value).await { if let Err(e) = cb.run(&ctx, &value).await {
log::error!("Error in event callback: {:?}", e); tracing::error!("Error in event callback: {:?}", e);
} }
} }
} }

@ -16,11 +16,11 @@ impl TypeMapKey for EventDrivenMessageContainer {
type Value = EventDrivenMessagesRef; type Value = EventDrivenMessagesRef;
} }
#[tracing::instrument(level = "trace", skip(ctx))]
pub async fn get_listeners_from_context(ctx: &Context) -> Result<EventDrivenMessagesRef> { pub async fn get_listeners_from_context(ctx: &Context) -> Result<EventDrivenMessagesRef> {
let data = ctx.data.read().await; let data = ctx.data.read().await;
let listeners = data let listeners = data
.get::<EventDrivenMessageContainer>() .get::<EventDrivenMessageContainer>()
.ok_or(Error::Uninitialized)?; .ok_or(Error::Uninitialized)?;
log::trace!("Returning listener");
Ok(listeners.clone()) Ok(listeners.clone())
} }

@ -10,8 +10,8 @@ use serenity::model::channel::Reaction;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
/// Shows the next page in the menu /// Shows the next page in the menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn next_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> { pub async fn next_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> {
log::debug!("Showing next page");
menu.current_page = (menu.current_page + 1) % menu.pages.len(); menu.current_page = (menu.current_page + 1) % menu.pages.len();
display_page(ctx, menu).await?; display_page(ctx, menu).await?;
@ -19,8 +19,8 @@ pub async fn next_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Resul
} }
/// Shows the previous page in the menu /// Shows the previous page in the menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn previous_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> { pub async fn previous_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> {
log::debug!("Showing previous page");
if menu.current_page == 0 { if menu.current_page == 0 {
menu.current_page = menu.pages.len() - 1; menu.current_page = menu.pages.len() - 1;
} else { } else {
@ -32,8 +32,8 @@ pub async fn previous_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> R
} }
/// Shows the previous page in the menu /// Shows the previous page in the menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn close_menu(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> { pub async fn close_menu(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> {
log::debug!("Closing menu");
menu.close(ctx.http()).await?; menu.close(ctx.http()).await?;
let listeners = get_listeners_from_context(&ctx).await?; let listeners = get_listeners_from_context(&ctx).await?;
let mut listeners_lock = listeners.lock().await; let mut listeners_lock = listeners.lock().await;
@ -43,8 +43,9 @@ pub async fn close_menu(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Resu
Ok(()) Ok(())
} }
/// Shows a help menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn toggle_help(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> { pub async fn toggle_help(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> {
log::debug!("Displaying help");
let show_help = menu let show_help = menu
.data .data
.get::<HelpActiveContainer>() .get::<HelpActiveContainer>()
@ -63,7 +64,7 @@ pub async fn toggle_help(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Res
.get() .get()
.await?; .await?;
let mut message = menu.get_message(ctx.http()).await?; let mut message = menu.get_message(ctx.http()).await?;
log::debug!("Building help entries"); tracing::debug!("Building help entries");
let mut help_entries = menu let mut help_entries = menu
.help_entries .help_entries
.iter() .iter()
@ -75,7 +76,7 @@ pub async fn toggle_help(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Res
.map(|(_, e, h)| format!(" - {} {}", e, h)) .map(|(_, e, h)| format!(" - {} {}", e, h))
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n"); .join("\n");
log::trace!("Help message is {}", help_message); tracing::trace!("Help message is {}", help_message);
message message
.edit(ctx, |m| { .edit(ctx, |m| {
@ -104,15 +105,16 @@ pub async fn toggle_help(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Res
m m
}) })
.await?; .await?;
log::debug!("Help message displayed"); tracing::debug!("Help message displayed");
show_help.store(true, Ordering::Relaxed); show_help.store(true, Ordering::Relaxed);
Ok(()) Ok(())
} }
/// Displays the menu page /// Displays the menu page
#[tracing::instrument(level = "debug", skip_all)]
pub async fn display_page(ctx: &Context, menu: &mut Menu<'_>) -> Result<()> { pub async fn display_page(ctx: &Context, menu: &mut Menu<'_>) -> Result<()> {
log::debug!("Displaying page {}", menu.current_page); tracing::debug!("Displaying page {}", menu.current_page);
let page = menu let page = menu
.pages .pages
.get(menu.current_page) .get(menu.current_page)
@ -126,7 +128,7 @@ pub async fn display_page(ctx: &Context, menu: &mut Menu<'_>) -> Result<()> {
e e
}) })
.await?; .await?;
log::debug!("Page displayed"); tracing::debug!("Page displayed");
Ok(()) Ok(())
} }

@ -81,8 +81,8 @@ pub struct Menu<'a> {
impl Menu<'_> { impl Menu<'_> {
/// Removes all reactions from the menu /// Removes all reactions from the menu
#[tracing::instrument(level = "debug", skip_all)]
pub(crate) async fn close(&mut self, http: &Http) -> Result<()> { pub(crate) async fn close(&mut self, http: &Http) -> Result<()> {
log::debug!("Closing menu...");
let handle = self.message.read().await; let handle = self.message.read().await;
http.delete_message_reactions(handle.channel_id, handle.message_id) http.delete_message_reactions(handle.channel_id, handle.message_id)
.await?; .await?;
@ -101,14 +101,12 @@ impl Menu<'_> {
} }
/// Recreates the message completely /// Recreates the message completely
#[tracing::instrument(level = "debug", skip_all)]
pub async fn recreate(&self, http: &Http) -> Result<()> { pub async fn recreate(&self, http: &Http) -> Result<()> {
log::debug!("Recreating message");
let old_handle = { let old_handle = {
let handle = self.message.read().await; let handle = self.message.read().await;
(*handle).clone() (*handle).clone()
}; };
log::debug!("Getting current page");
let current_page = self let current_page = self
.pages .pages
.get(self.current_page) .get(self.current_page)
@ -117,7 +115,6 @@ impl Menu<'_> {
.get() .get()
.await?; .await?;
log::debug!("Creating new message");
let message = http let message = http
.send_message( .send_message(
old_handle.channel_id, old_handle.channel_id,
@ -139,7 +136,6 @@ impl Menu<'_> {
) )
.await?; .await?;
} }
log::trace!("New message is {:?}", message);
let new_handle = { let new_handle = {
let mut handle = self.message.write().await; let mut handle = self.message.write().await;
@ -147,15 +143,14 @@ impl Menu<'_> {
(*handle).clone() (*handle).clone()
}; };
{ {
log::debug!("Changing key of message"); tracing::debug!("Changing key of message");
let mut listeners_lock = self.listeners.lock().await; let mut listeners_lock = self.listeners.lock().await;
let menu = listeners_lock.remove(&old_handle).unwrap(); let menu = listeners_lock.remove(&old_handle).unwrap();
listeners_lock.insert(new_handle, menu); listeners_lock.insert(new_handle, menu);
} }
log::debug!("Deleting original message"); tracing::debug!("Deleting original message");
http.delete_message(old_handle.channel_id, old_handle.message_id) http.delete_message(old_handle.channel_id, old_handle.message_id)
.await?; .await?;
log::debug!("Message recreated");
Ok(()) Ok(())
} }
@ -167,13 +162,14 @@ impl<'a> EventDrivenMessage for Menu<'a> {
self.closed self.closed
} }
#[tracing::instrument(level = "debug", skip_all)]
async fn update(&mut self, http: &Http) -> Result<()> { async fn update(&mut self, http: &Http) -> Result<()> {
log::trace!("Checking for menu timeout"); tracing::trace!("Checking for menu timeout");
if Instant::now() >= self.timeout { if Instant::now() >= self.timeout {
log::debug!("Menu timout reached. Closing menu."); tracing::debug!("Menu timout reached. Closing menu.");
self.close(http).await?; self.close(http).await?;
} else if self.sticky { } else if self.sticky {
log::debug!("Message is sticky. Checking for new messages in channel..."); tracing::debug!("Message is sticky. Checking for new messages in channel...");
let handle = { let handle = {
let handle = self.message.read().await; let handle = self.message.read().await;
(*handle).clone() (*handle).clone()
@ -183,9 +179,8 @@ impl<'a> EventDrivenMessage for Menu<'a> {
let messages = channel_id let messages = channel_id
.messages(http, |p| p.after(handle.message_id).limit(1)) .messages(http, |p| p.after(handle.message_id).limit(1))
.await?; .await?;
log::trace!("Messages are {:?}", messages);
if messages.len() > 0 { if messages.len() > 0 {
log::debug!("New messages in channel. Recreating..."); tracing::debug!("New messages in channel. Recreating...");
self.recreate(http).await?; self.recreate(http).await?;
} }
} }
@ -193,26 +188,28 @@ impl<'a> EventDrivenMessage for Menu<'a> {
Ok(()) Ok(())
} }
#[tracing::instrument(level = "debug", skip_all)]
async fn on_reaction_add(&mut self, ctx: &Context, reaction: Reaction) -> Result<()> { async fn on_reaction_add(&mut self, ctx: &Context, reaction: Reaction) -> Result<()> {
log::debug!("Reaction to menu added");
let current_user = ctx.http.get_current_user().await?; let current_user = ctx.http.get_current_user().await?;
if reaction.user_id.unwrap().0 == current_user.id.0 { if reaction.user_id.unwrap().0 == current_user.id.0 {
log::debug!("Reaction is from current user."); tracing::debug!("Reaction is from current user.");
return Ok(()); return Ok(());
} }
let emoji_string = reaction.emoji.as_data(); let emoji_string = reaction.emoji.as_data();
log::debug!("Deleting user reaction."); tracing::debug!("Deleting user reaction.");
reaction.delete(ctx).await?; reaction.delete(ctx).await?;
if let Some(owner) = self.owner { if let Some(owner) = self.owner {
if owner != reaction.user_id.unwrap() { if owner != reaction.user_id.unwrap() {
log::debug!("Menu has an owner and the reaction is not from the owner of the menu"); tracing::debug!(
"Menu has an owner and the reaction is not from the owner of the menu"
);
return Ok(()); return Ok(());
} }
} }
if let Some(control) = self.controls.get(&emoji_string).cloned() { if let Some(control) = self.controls.get(&emoji_string).cloned() {
log::debug!("Running control"); tracing::debug!("Running control");
control.run(ctx, self, reaction).await?; control.run(ctx, self, reaction).await?;
} }
@ -249,8 +246,8 @@ impl Default for MenuBuilder {
impl MenuBuilder { impl MenuBuilder {
/// Creates a new pagination menu /// Creates a new pagination menu
#[tracing::instrument(level = "debug", skip_all)]
pub fn new_paginator() -> Self { pub fn new_paginator() -> Self {
log::debug!("Creating new paginator");
let mut controls = HashMap::new(); let mut controls = HashMap::new();
let mut help_entries = HashMap::new(); let mut help_entries = HashMap::new();
controls.insert( controls.insert(
@ -391,12 +388,12 @@ impl MenuBuilder {
} }
/// builds the menu /// builds the menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn build( pub async fn build(
self, self,
ctx: &Context, ctx: &Context,
channel_id: ChannelId, channel_id: ChannelId,
) -> Result<Arc<RwLock<MessageHandle>>> { ) -> Result<Arc<RwLock<MessageHandle>>> {
log::debug!("Building menu...");
let mut current_page = self let mut current_page = self
.pages .pages
.get(self.current_page) .get(self.current_page)
@ -406,9 +403,8 @@ impl MenuBuilder {
.await?; .await?;
let message = channel_id.send_message(ctx, |_| &mut current_page).await?; let message = channel_id.send_message(ctx, |_| &mut current_page).await?;
log::trace!("Message is {:?}", message);
let listeners = get_listeners_from_context(ctx).await?; let listeners = get_listeners_from_context(ctx).await?;
log::debug!("Sorting controls..."); tracing::debug!("Sorting controls...");
let mut controls = self let mut controls = self
.controls .controls
.clone() .clone()
@ -416,7 +412,7 @@ impl MenuBuilder {
.collect::<Vec<(String, ActionContainer)>>(); .collect::<Vec<(String, ActionContainer)>>();
controls.sort_by_key(|(_, a)| a.position); controls.sort_by_key(|(_, a)| a.position);
log::debug!("Creating menu..."); tracing::debug!("Creating menu...");
let message_handle = MessageHandle::new(message.channel_id, message.id); let message_handle = MessageHandle::new(message.channel_id, message.id);
let handle_lock = Arc::new(RwLock::new(message_handle)); let handle_lock = Arc::new(RwLock::new(message_handle));
@ -434,20 +430,19 @@ impl MenuBuilder {
owner: self.owner, owner: self.owner,
}; };
log::debug!("Storing menu to listeners..."); tracing::debug!("Storing menu to listeners...");
{ {
let mut listeners_lock = listeners.lock().await; let mut listeners_lock = listeners.lock().await;
log::trace!("Listeners locked."); tracing::trace!("Listeners locked.");
listeners_lock.insert(message_handle, Arc::new(Mutex::new(Box::new(menu)))); listeners_lock.insert(message_handle, Arc::new(Mutex::new(Box::new(menu))));
} }
log::debug!("Adding controls..."); tracing::debug!("Adding controls...");
for (emoji, _) in controls { for (emoji, _) in controls {
message message
.react(ctx, ReactionType::Unicode(emoji.clone())) .react(ctx, ReactionType::Unicode(emoji.clone()))
.await?; .await?;
} }
log::debug!("Menu successfully created.");
Ok(handle_lock) Ok(handle_lock)
} }

Loading…
Cancel
Save