Skip to content

Instantly share code, notes, and snippets.

@norinorin
Last active November 7, 2023 20:53
Show Gist options
  • Save norinorin/0ef021163d042b3be76b892726d76e52 to your computer and use it in GitHub Desktop.
Save norinorin/0ef021163d042b3be76b892726d76e52 to your computer and use it in GitHub Desktop.
discord.py mobile status monkey patch
"""
USE THIS AT YOUR OWN RISK. THIS IS UNDOCUMENTED, I WILL NOT BE RESPONSIBLE FOR WHAT HAPPENS WITH YOUR BOT(S).
You can import this into your main file since these are top-level statements or just copy-paste the whole thing (with credits).
What this does is patch the identify payload, the `$browser` field to be exact.
Since it's hardcoded in discord.py, we monkey-patch it.
You can change it to either `Discord Android` or `Discord iOS`—I'm not aware of any other options,
but those 2 seem to trigger the mobile indicator.
w/ Hikari: https://github.com/norinorin/nokari/blob/master/nokari/utils/monkey_patch.py
"""
import ast
import inspect
import re
import discord
# s: https://medium.com/@chipiga86/python-monkey-patching-like-a-boss-87d7ddb8098e
def source(o):
s = inspect.getsource(o).split("\n")
indent = len(s[0]) - len(s[0].lstrip())
return "\n".join(i[indent:] for i in s)
source_ = source(discord.gateway.DiscordWebSocket.identify)
patched = re.sub(
r'([\'"]\$browser[\'"]:\s?[\'"]).+([\'"])', # hh this regex
r"\1Discord Android\2", # s: https://luna.gitlab.io/discord-unofficial-docs/mobile_indicator.html
source_
)
loc = {}
exec(compile(ast.parse(patched), "<string>", "exec"), discord.gateway.__dict__, loc)
discord.gateway.DiscordWebSocket.identify = loc["identify"]
@norinorin
Copy link
Author

how do i do this for discord.py-self?

Hi there, i've just skimmed through their code. Seems like you don't need to monkeypatch like you would on discord.py. You just need to change the super_properties instance variable of HTTPClient.

class CustomClient(discord.Client):  # or commands.Bot
    async def setup_hook(self):
        self.http.super_properties["browser"] = ...
        self.http.super_properties[...] = ...  # maybe wanna change other key as well like `os`

Code above is untested as I don't self bot.

@imnew827
Copy link

thanks i actually fixed it by messing around the code in gateway.py inside discord.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment