Last active
August 17, 2018 08:39
-
-
Save ioistired/ded2d8b33f29a449d4eaed0f77880adf to your computer and use it in GitHub Desktop.
discord.py@rewrite tool to monkey patch functions that run before every invocation of Messageble.send
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.swp | |
*.py[cod] | |
venv/ | |
.venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# encoding: utf-8 | |
import discord | |
from discord.ext import commands | |
import io | |
from patch import global_message_send_hook | |
bot = commands.Bot(command_prefix=commands.when_mentioned) | |
@global_message_send_hook | |
async def attach_if_too_long(original_send, message=None, *args, **kwargs): | |
if message is None: return True, | |
message = str(message) | |
if len(message) > 2000: | |
file = discord.File(fp=io.StringIO(message), filename='message.txt') | |
message = await original_send('Way too long. Message attached.', *args, **kwargs, file=file) | |
return False, message | |
return True, | |
@bot.command() | |
async def send_big_message(context): | |
print((await context.send('aA'*1001)).id) | |
@bot.event | |
async def on_ready(): | |
print('Ready.') | |
if __name__ == '__main__': | |
import os | |
bot.run(os.environ['discord_bot_token']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# encoding: utf-8 | |
import functools | |
import discord.abc | |
_hooks = [] | |
_patched = False | |
_old_send = discord.abc.Messageable.send | |
def global_message_send_hook(func): | |
_hooks.append(func) | |
_monkey_patch() | |
# allow this to be used as a decorator | |
return func | |
unregister = _hooks.remove | |
def restore(): | |
discord.abc.Messageable.send = _old_send | |
def _monkey_patch(): | |
global _patched | |
if _patched: | |
return | |
@functools.wraps(_old_send) | |
async def send(self, *args, **kwargs): | |
# old_send is not a bound method. | |
# "bind" it to self, so that the user doesnt have to pass in self manually | |
bound_old_send = functools.partial(_old_send, self) | |
for hook in _hooks: | |
# allow the user to prevent default send behavior | |
# by returning False | |
# pass in old_send so that the user can still send | |
# using the original behavior | |
should_continue, *result = await hook(bound_old_send, *args, **kwargs) | |
if not should_continue: | |
return result[0] | |
return await _old_send(self, *args, **kwargs) | |
discord.abc.Messageable.send = send | |
_patched = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment