Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text in voice #1231

Merged
merged 4 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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