Last active
August 17, 2018 08:39
-
-
Save ioistired/ded2d8b33f29a449d4eaed0f77880adf to your computer and use it in GitHub Desktop.
Revisions
-
ioistired revised this gist
Aug 17, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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, 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, @bot.command() async def send_big_message(context): -
ioistired revised this gist
Aug 17, 2018 . 2 changed files with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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') 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(): This file contains hidden or 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 charactersOriginal 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 should_continue, *result = await hook(bound_old_send, *args, **kwargs) if not should_continue: return result[0] return await _old_send(self, *args, **kwargs) -
ioistired revised this gist
Aug 17, 2018 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ *.swp *.py[cod] venv/ .venv/ -
ioistired revised this gist
Aug 17, 2018 . 1 changed file with 13 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 result = await hook(bound_old_send, *args, **kwargs): if not result: return result return await _old_send(self, *args, **kwargs) discord.abc.Messageable.send = send _patched = True -
ioistired revised this gist
Aug 17, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) @bot.event async def on_ready(): -
ioistired revised this gist
Jul 19, 2018 . 1 changed file with 3 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #!/usr/bin/env python3 # encoding: utf-8 import functools @@ -7,7 +7,7 @@ _hooks = [] _patched = False _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 if _patched: return @functools.wraps(_old_send) async def send(self, *args, **kwargs): for hook in _hooks: -
ioistired revised this gist
Jul 19, 2018 . 2 changed files with 11 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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']) This file contains hidden or 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 charactersOriginal 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, _old_send if _patched: return _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): return await _old_send(self, *args, **kwargs) discord.abc.Messageable.send = send _patched = True def restore(): discord.abc.Messageable.send = _old_send -
ioistired created this gist
Jul 18, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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']) This file contains hidden or 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 charactersOriginal 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