Skip to content

Instantly share code, notes, and snippets.

@TAG-Epic
Last active December 5, 2022 11:22
Show Gist options
  • Select an option

  • Save TAG-Epic/68e05d98a89982bac827ad2c3a60c50a to your computer and use it in GitHub Desktop.

Select an option

Save TAG-Epic/68e05d98a89982bac827ad2c3a60c50a to your computer and use it in GitHub Desktop.
Modified version of AlexFlipnote's LavaLink fix. LICENCED UNDER THE MIT LICENSE.
"""
Restores the old "on_socket_response" event
Just load this cog and it should fix your lavalink/slash command issues
Remember to set "enable_debug_events" to True in the bot!
"""
import zlib
import discord
from discord.ext import commands
class SocketFix(commands.Cog):
def __init__(self, bot):
self.bot = bot
self._zlib = zlib.decompressobj()
self._buffer = bytearray()
@commands.Cog.listener()
async def on_socket_raw_receive(self, msg):
""" This is to replicate discord.py's 'on_socket_response' that was removed from discord.py v2 """
if type(msg) is bytes:
self._buffer.extend(msg)
if len(msg) < 4 or msg[-4:] != b'\x00\x00\xff\xff':
return
try:
msg = self._zlib.decompress(self._buffer)
except Exception:
self._buffer = bytearray() # Reset buffer on fail just in case...
return
msg = msg.decode('utf-8')
self._buffer = bytearray()
msg = discord.utils._from_json(msg)
self.bot.dispatch('socket_response', msg)
def setup(bot):
bot.add_cog(SocketFix(bot))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment