Repository: interactions-py/interactions.py

Issue:
[BUG] Cannot pop from Cache during dispatch.
### Describe the bug.

When running functions leading to delete events, for example `channel.purge` the cache encounters a KeyError and kills the whole Process.

This is caused by the implementation of #482

### List the steps.

n/A

### What you expected.

n/A

### What you saw.
```
Websocket have raised an exception, closing.
Traceback (most recent call last):
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\client\bot.py", line 440, in _login
    await self._websocket.run()
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\api\gateway\client.py", line 279, in run
    await self._handle_stream(msg)
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\api\gateway\client.py", line 332, in _handle_stream
    self._dispatch_event(event, data)
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\api\gateway\client.py", line 562, in _dispatch_event
    _message_cache.pop(message_id)
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\api\cache.py", line 131, in pop
    return self.values.pop(key, default)
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\utils\dict_caches.py", line 39, in __getitem__
    self.move_to_end(key)
KeyError: Snowflake(1040316644695756912)

Process finished with exit code 0
```

### What version of the library did you use?

unstable

### Version specification

The unstable unstable version

### Code of Conduct

- [X] I agree to follow the contribution requirements.
[BUG] Cannot pop from Cache during dispatch.
### Describe the bug.

When running functions leading to delete events, for example `channel.purge` the cache encounters a KeyError and kills the whole Process.

This is caused by the implementation of #482

### List the steps.

n/A

### What you expected.

n/A

### What you saw.
```
Websocket have raised an exception, closing.
Traceback (most recent call last):
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\client\bot.py", line 440, in _login
    await self._websocket.run()
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\api\gateway\client.py", line 279, in run
    await self._handle_stream(msg)
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\api\gateway\client.py", line 332, in _handle_stream
    self._dispatch_event(event, data)
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\api\gateway\client.py", line 562, in _dispatch_event
    _message_cache.pop(message_id)
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\api\cache.py", line 131, in pop
    return self.values.pop(key, default)
  File "C:\Users\\Desktop\PycharmProjects\library\interactions\utils\dict_caches.py", line 39, in __getitem__
    self.move_to_end(key)
KeyError: Snowflake(1040316644695756912)

Process finished with exit code 0
```

### What version of the library did you use?

unstable

### Version specification

The unstable unstable version

### Code of Conduct

- [X] I agree to follow the contribution requirements.


Discussion:
This would explain *way too much.* Glad the problem's been identified.
@EdVraz Can you give some steps to consistently reproduce the bug? I'm trying to fix it, but using `channel.purge` doesn't trigger it for me
1. Send a message
2. Remove that message

Or

Remove not cached message(any object which can be cached)
> @EdVraz Can you give some steps to consistently reproduce the bug? I'm trying to fix it, but using `channel.purge` doesn't trigger it for me
@Catalyst4222 
https://github.com/interactions-py/library/blob/unstable/interactions/api/gateway/client.py#L562 change this to `.pop(id)` and then join and leave a vc. This should trigger it. At least it did for me

This would explain *way too much.* Glad the problem's been identified.
@EdVraz Can you give some steps to consistently reproduce the bug? I'm trying to fix it, but using `channel.purge` doesn't trigger it for me
1. Send a message
2. Remove that message

Or

Remove not cached message(any object which can be cached)
> @EdVraz Can you give some steps to consistently reproduce the bug? I'm trying to fix it, but using `channel.purge` doesn't trigger it for me
@Catalyst4222 
https://github.com/interactions-py/library/blob/unstable/interactions/api/gateway/client.py#L562 change this to `.pop(id)` and then join and leave a vc. This should trigger it. At least it did for me


Resolving patch:
diff --git a/interactions/utils/dict_caches.py b/interactions/utils/dict_caches.py
--- a/interactions/utils/dict_caches.py
+++ b/interactions/utils/dict_caches.py
@@ -1,6 +1,8 @@
 from collections import OrderedDict
 from typing import Generic, TypeVar
 
+from .missing import MISSING
+
 __all__ = ("FIFODict", "LRUDict")
 
 _KT = TypeVar("_KT")
@@ -45,3 +47,14 @@ def __setitem__(self, key: _KT, value: _VT):
         # Prevent buildup over time
         while len(self) > self._max_items:
             del self[next(iter(self))]
+
+    __marker = object()
+
+    def pop(self, key: _KT, default: _VT = __marker) -> _VT:
+        if key in self:
+            result = self[key]
+            del self[key]
+            return result
+        if default is MISSING:
+            raise KeyError(key)
+        return default


Test patch: