Skip to content

Commit

Permalink
Merge pull request #1231 from Pycord-Development/text-in-voice
Browse files Browse the repository at this point in the history
Text in voice
  • Loading branch information
BobDotCom authored Apr 8, 2022
2 parents 9d44673 + 6b6933e commit 8d52bc8
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 121 deletions.
74 changes: 73 additions & 1 deletion discord/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import asyncio
import copy
import time
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -41,6 +42,7 @@
Union,
overload,
runtime_checkable,
Iterable,
)

from . import utils
Expand Down Expand Up @@ -79,6 +81,7 @@
GroupChannel,
PartialMessageable,
TextChannel,
VoiceChannel,
)
from .client import Client
from .embeds import Embed
Expand All @@ -95,13 +98,82 @@
from .ui.view import View
from .user import ClientUser

PartialMessageableChannel = Union[TextChannel, Thread, DMChannel, PartialMessageable]
PartialMessageableChannel = Union[TextChannel, VoiceChannel, Thread, DMChannel, PartialMessageable]
MessageableChannel = Union[PartialMessageableChannel, GroupChannel]
SnowflakeTime = Union["Snowflake", datetime]

MISSING = utils.MISSING


async def _single_delete_strategy(messages: Iterable[Message], *, reason: Optional[str] = None):
for m in messages:
await m.delete(reason=reason)


async def _purge_messages_helper(
channel: Union[TextChannel, Thread, VoiceChannel],
*,
limit: Optional[int] = 100,
check: Callable[[Message], bool] = MISSING,
before: Optional[SnowflakeTime] = None,
after: Optional[SnowflakeTime] = None,
around: Optional[SnowflakeTime] = None,
oldest_first: Optional[bool] = False,
bulk: bool = True,
reason: Optional[str] = None,
) -> List[Message]:
if check is MISSING:
check = lambda m: True

iterator = channel.history(
limit=limit,
before=before,
after=after,
oldest_first=oldest_first,
around=around,
)
ret: List[Message] = []
count = 0

minimum_time = int((time.time() - 14 * 24 * 60 * 60) * 1000.0 - 1420070400000) << 22
strategy = channel.delete_messages if bulk else _single_delete_strategy

async for message in iterator:
if count == 100:
to_delete = ret[-100:]
await strategy(to_delete, reason=reason)
count = 0
await asyncio.sleep(1)

if not check(message):
continue

if message.id < minimum_time:
# older than 14 days old
if count == 1:
await ret[-1].delete(reason=reason)
elif count >= 2:
to_delete = ret[-count:]
await strategy(to_delete, reason=reason)

count = 0
strategy = _single_delete_strategy

count += 1
ret.append(message)

# Some messages remaining to poll
if count >= 2:
# more than 2 messages -> bulk delete
to_delete = ret[-count:]
await strategy(to_delete, reason=reason)
elif count == 1:
# delete a single message
await ret[-1].delete(reason=reason)

return ret


class _Undefined:
def __repr__(self) -> str:
return "see-below"
Expand Down
Loading

0 comments on commit 8d52bc8

Please sign in to comment.