Skip to content

Instantly share code, notes, and snippets.

@ioistired
Last active August 17, 2018 08:39
Show Gist options
  • Select an option

  • Save ioistired/ded2d8b33f29a449d4eaed0f77880adf to your computer and use it in GitHub Desktop.

Select an option

Save ioistired/ded2d8b33f29a449d4eaed0f77880adf to your computer and use it in GitHub Desktop.

Revisions

  1. ioistired revised this gist Aug 17, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions example.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@

    @global_message_send_hook
    async def attach_if_too_long(original_send, message=None, *args, **kwargs):
    if message is None: return True
    if message is None: return True,

    message = str(message)

    @@ -22,7 +22,7 @@ async def attach_if_too_long(original_send, message=None, *args, **kwargs):
    message = await original_send('Way too long. Message attached.', *args, **kwargs, file=file)
    return False, message

    return True
    return True,

    @bot.command()
    async def send_big_message(context):
  2. ioistired revised this gist Aug 17, 2018. 2 changed files with 6 additions and 6 deletions.
    6 changes: 3 additions & 3 deletions example.py
    Original file line number Diff line number Diff line change
    @@ -19,14 +19,14 @@ async def attach_if_too_long(original_send, message=None, *args, **kwargs):

    if len(message) > 2000:
    file = discord.File(fp=io.StringIO(message), filename='message.txt')
    await original_send('Way too long. Message attached.', *args, **kwargs, file=file)
    return False
    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):
    await context.send('aA'*1001)
    print((await context.send('aA'*1001)).id)

    @bot.event
    async def on_ready():
    6 changes: 3 additions & 3 deletions patch.py
    Original file line number Diff line number Diff line change
    @@ -37,9 +37,9 @@ async def send(self, *args, **kwargs):
    # by returning False
    # pass in old_send so that the user can still send
    # using the original behavior
    result = await hook(bound_old_send, *args, **kwargs):
    if not result:
    return result
    should_continue, *result = await hook(bound_old_send, *args, **kwargs)
    if not should_continue:
    return result[0]

    return await _old_send(self, *args, **kwargs)

  3. ioistired revised this gist Aug 17, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    *.swp
    *.py[cod]
    venv/
    .venv/
  4. ioistired revised this gist Aug 17, 2018. 1 changed file with 13 additions and 8 deletions.
    21 changes: 13 additions & 8 deletions patch.py
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,11 @@ def global_message_send_hook(func):
    # 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

    @@ -23,20 +28,20 @@ def _monkey_patch():

    @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
    # old_send is not a bound method.
    # "bind" it to self, so that the user doesnt have to pass in self manually
    if not await hook(functools.partial(_old_send, self), *args, **kwargs):
    return
    result = await hook(bound_old_send, *args, **kwargs):
    if not result:
    return result

    await _old_send(self, *args, **kwargs)
    return await _old_send(self, *args, **kwargs)

    discord.abc.Messageable.send = send
    _patched = True

    def restore():
    discord.abc.Messageable.send = _old_send
  5. ioistired revised this gist Aug 17, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example.py
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ async def attach_if_too_long(original_send, message=None, *args, **kwargs):

    @bot.command()
    async def send_big_message(context):
    await context.send('Aa'*1001)
    await context.send('aA'*1001)

    @bot.event
    async def on_ready():
  6. ioistired revised this gist Jul 19, 2018. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions patch.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/usr/bin/env python
    #!/usr/bin/env python3
    # encoding: utf-8

    import functools
    @@ -7,7 +7,7 @@

    _hooks = []
    _patched = False
    _old_send
    _old_send = discord.abc.Messageable.send

    def global_message_send_hook(func):
    _hooks.append(func)
    @@ -16,13 +16,11 @@ def global_message_send_hook(func):
    return func

    def _monkey_patch():
    global _patched, _old_send
    global _patched

    if _patched:
    return

    _old_send = discord.abc.Messageable.send

    @functools.wraps(_old_send)
    async def send(self, *args, **kwargs):
    for hook in _hooks:
  7. ioistired revised this gist Jul 19, 2018. 2 changed files with 11 additions and 6 deletions.
    2 changes: 1 addition & 1 deletion example.py
    Original file line number Diff line number Diff line change
    @@ -36,4 +36,4 @@ async def on_ready():
    if __name__ == '__main__':
    import os

    bot.run(os.environ['discord_bot_token'])
    bot.run(os.environ['discord_bot_token'])
    15 changes: 10 additions & 5 deletions patch.py
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@

    _hooks = []
    _patched = False
    _old_send

    def global_message_send_hook(func):
    _hooks.append(func)
    @@ -15,13 +16,14 @@ def global_message_send_hook(func):
    return func

    def _monkey_patch():
    global _patched
    global _patched, _old_send

    if _patched:
    return

    old_send = discord.abc.Messageable.send
    _old_send = discord.abc.Messageable.send

    @functools.wraps(_old_send)
    async def send(self, *args, **kwargs):
    for hook in _hooks:
    # allow the user to prevent default send behavior
    @@ -30,10 +32,13 @@ async def send(self, *args, **kwargs):
    # using the original behavior
    # old_send is not a bound method.
    # "bind" it to self, so that the user doesnt have to pass in self manually
    if not await hook(functools.partial(old_send, self), *args, **kwargs):
    if not await hook(functools.partial(_old_send, self), *args, **kwargs):
    return

    await old_send(self, *args, **kwargs)
    await _old_send(self, *args, **kwargs)

    discord.abc.Messageable.send = send
    _patched = True
    _patched = True

    def restore():
    discord.abc.Messageable.send = _old_send
  8. ioistired created this gist Jul 18, 2018.
    39 changes: 39 additions & 0 deletions example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/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')
    await original_send('Way too long. Message attached.', *args, **kwargs, file=file)
    return False

    return True

    @bot.command()
    async def send_big_message(context):
    await context.send('Aa'*1001)

    @bot.event
    async def on_ready():
    print('Ready.')


    if __name__ == '__main__':
    import os

    bot.run(os.environ['discord_bot_token'])
    39 changes: 39 additions & 0 deletions patch.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/usr/bin/env python
    # encoding: utf-8

    import functools

    import discord.abc

    _hooks = []
    _patched = False

    def global_message_send_hook(func):
    _hooks.append(func)
    _monkey_patch()
    # allow this to be used as a decorator
    return func

    def _monkey_patch():
    global _patched

    if _patched:
    return

    old_send = discord.abc.Messageable.send

    async def send(self, *args, **kwargs):
    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
    # old_send is not a bound method.
    # "bind" it to self, so that the user doesnt have to pass in self manually
    if not await hook(functools.partial(old_send, self), *args, **kwargs):
    return

    await old_send(self, *args, **kwargs)

    discord.abc.Messageable.send = send
    _patched = True