serenity-rs / serenity

A Rust library for the Discord API.
https://discord.gg/serenity-rs
ISC License
4.65k stars 571 forks source link

Messages should be truncated from cache when `Settings::max_messages` is lower than current cached messages #2884

Open jamesbt365 opened 2 months ago

jamesbt365 commented 2 months ago

I'm not sure if we had this at some point but we certainly don't now, if max messages is reduced at runtime any amount set over the limit is kept.

ivinjabraham commented 16 hours ago

I'll take this up.

I believe that this is the only method to change max_messages at runtime after creation:

    /// Sets the maximum amount of messages per channel to cache.
    ///
    /// By default, no messages will be cached.
    pub fn set_max_messages(&self, max: usize) {
        self.settings.write().max_messages = max;
    }

I'll make it check if the argument max is less than max_messages by reading the settings since checking it after assigning the new value should take more time. And if it is greater, delete the messages from Cache.messages that correspond to the front of Cache.message_queue for each channel's queue. Does that sound good?