Add support for sending messages and managing webhooks in VoiceChannel

This commit is contained in:
Rapptz
2022-04-02 11:10:14 -04:00
parent b049cf77f2
commit 2aca705b95
5 changed files with 355 additions and 111 deletions

View File

@@ -25,6 +25,7 @@ DEALINGS IN THE SOFTWARE.
from __future__ import annotations
import copy
import time
import asyncio
from datetime import datetime
from typing import (
@@ -32,6 +33,7 @@ from typing import (
AsyncIterator,
Callable,
Dict,
Iterable,
List,
Optional,
TYPE_CHECKING,
@@ -81,7 +83,7 @@ if TYPE_CHECKING:
from .channel import CategoryChannel
from .embeds import Embed
from .message import Message, MessageReference, PartialMessage
from .channel import TextChannel, DMChannel, GroupChannel, PartialMessageable
from .channel import TextChannel, DMChannel, GroupChannel, PartialMessageable, VoiceChannel
from .threads import Thread
from .enums import InviteTarget
from .ui.view import View
@@ -95,7 +97,7 @@ if TYPE_CHECKING:
SnowflakeList,
)
PartialMessageableChannel = Union[TextChannel, Thread, DMChannel, PartialMessageable]
PartialMessageableChannel = Union[TextChannel, VoiceChannel, Thread, DMChannel, PartialMessageable]
MessageableChannel = Union[PartialMessageableChannel, GroupChannel]
SnowflakeTime = Union["Snowflake", datetime]
@@ -110,6 +112,69 @@ class _Undefined:
_undefined: Any = _Undefined()
async def _single_delete_strategy(messages: Iterable[Message], *, reason: Optional[str] = None):
for m in messages:
await m.delete()
async def _purge_helper(
channel: Union[Thread, TextChannel, 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()
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()
return ret
@runtime_checkable
class Snowflake(Protocol):
"""An ABC that details the common operations on a Discord model.