Add delete_after parameter to MessageChannel.send

This commit is contained in:
Rapptz 2016-12-25 17:50:40 -05:00
parent 20ddc9f14f
commit 4e175d36d3
2 changed files with 18 additions and 4 deletions

View File

@ -145,7 +145,7 @@ class MessageChannel(metaclass=abc.ABCMeta):
raise NotImplementedError
@asyncio.coroutine
def send(self, content=None, *, tts=False, embed=None, file=None, filename=None):
def send(self, content=None, *, tts=False, embed=None, file=None, filename=None, delete_after=None):
"""|coro|
Sends a message to the channel with the content given.
@ -185,6 +185,10 @@ class MessageChannel(metaclass=abc.ABCMeta):
The filename of the file. Defaults to ``file.name`` if it's available.
If this is provided, you must also provide the ``file`` parameter or it
is silently ignored.
delete_after: float
If provided, the number of seconds to wait in the background
before deleting the message we just sent. If the deletion fails,
then it is silently ignored.
Raises
--------
@ -219,7 +223,17 @@ class MessageChannel(metaclass=abc.ABCMeta):
else:
data = yield from state.http.send_message(channel_id, content, guild_id=guild_id, tts=tts, embed=embed)
return Message(channel=self, state=state, data=data)
ret = Message(channel=self, state=state, data=data)
if delete_after is not None:
@asyncio.coroutine
def delete():
yield from asyncio.sleep(delete_after, loop=state.loop)
try:
yield from ret.delete()
except:
pass
discord.compat.create_task(delete(), loop=state.loop)
return ret
@asyncio.coroutine
def send_typing(self):

View File

@ -53,7 +53,7 @@ log = logging.getLogger(__name__)
ReadyState = namedtuple('ReadyState', ('launch', 'guilds'))
class StateContext:
__slots__ = ('store_user', 'http', 'self_id', 'store_emoji', 'reaction_emoji')
__slots__ = ('store_user', 'http', 'self_id', 'store_emoji', 'reaction_emoji', 'loop')
def __init__(self, **kwargs):
for attr, value in kwargs.items():
@ -71,7 +71,7 @@ class ConnectionState:
self.ctx = StateContext(store_user=self.store_user,
store_emoji=self.store_emoji,
reaction_emoji=self._get_reaction_emoji,
http=http, self_id=None)
http=http, self_id=None, loop=loop)
self.clear()
def clear(self):