Compare commits
1 Commits
ImAnAgent/
...
Gnome-py/f
Author | SHA1 | Date | |
---|---|---|---|
|
c9983d0248 |
12
README.rst
12
README.rst
@@ -17,6 +17,18 @@ The Future of enhanced-discord.py
|
||||
--------------------------
|
||||
|
||||
Enhanced discord.py is a fork of Rapptz's discord.py, that went unmaintained (`gist <https://gist.github.com/Rapptz/4a2f62751b9600a31a0d3c78100287f1>`_)
|
||||
|
||||
It is currently maintained by (in alphabetical order)
|
||||
|
||||
- Chillymosh#8175
|
||||
- Daggy#9889
|
||||
- dank Had0cK#6081
|
||||
- Dutchy#6127
|
||||
- Eyesofcreeper#0001
|
||||
- Gnome!#6669
|
||||
- IAmTomahawkx#1000
|
||||
- Jadon#2494
|
||||
|
||||
An overview of added features is available on the `custom features page <https://enhanced-dpy.readthedocs.io/en/latest/index.html#custom-features>`_.
|
||||
|
||||
Key Features
|
||||
|
@@ -1434,7 +1434,7 @@ class Messageable:
|
||||
components=components,
|
||||
)
|
||||
|
||||
ret = state.create_message(channel=channel, data=data)
|
||||
ret = state.store_message(channel=channel, data=data)
|
||||
if view:
|
||||
state.store_view(view, ret.id)
|
||||
|
||||
@@ -1501,7 +1501,7 @@ class Messageable:
|
||||
|
||||
channel = await self._get_channel()
|
||||
data = await self._state.http.get_message(channel.id, id)
|
||||
return self._state.create_message(channel=channel, data=data)
|
||||
return self._state.store_message(channel=channel, data=data)
|
||||
|
||||
async def pins(self) -> List[Message]:
|
||||
"""|coro|
|
||||
@@ -1528,7 +1528,7 @@ class Messageable:
|
||||
channel = await self._get_channel()
|
||||
state = self._state
|
||||
data = await state.http.pins_from(channel.id)
|
||||
return [state.create_message(channel=channel, data=m) for m in data]
|
||||
return [state.store_message(channel=channel, data=m) for m in data]
|
||||
|
||||
def history(
|
||||
self,
|
||||
|
@@ -53,7 +53,7 @@ from .widget import Widget
|
||||
from .guild import Guild
|
||||
from .emoji import Emoji
|
||||
from .channel import _threaded_channel_factory, PartialMessageable
|
||||
from .enums import ChannelType
|
||||
from .enums import ChannelType, StickerType
|
||||
from .mentions import AllowedMentions
|
||||
from .errors import *
|
||||
from .enums import Status, VoiceRegion
|
||||
@@ -76,7 +76,8 @@ from .threads import Thread
|
||||
from .sticker import GuildSticker, StandardSticker, StickerPack, _sticker_factory
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .abc import SnowflakeTime, PrivateChannel, GuildChannel, Snowflake
|
||||
from .abc import SnowflakeTime, PrivateChannel, GuildChannel as GuildChannelABC, Snowflake
|
||||
from .guild import GuildChannel
|
||||
from .channel import DMChannel
|
||||
from .message import Message
|
||||
from .member import Member
|
||||
@@ -780,7 +781,7 @@ class Client:
|
||||
"""List[:class:`~discord.User`]: Returns a list of all the users the bot can see."""
|
||||
return list(self._connection._users.values())
|
||||
|
||||
def get_channel(self, id: int, /) -> Optional[Union[GuildChannel, Thread, PrivateChannel]]:
|
||||
def get_channel(self, id: int, /) -> Optional[Union[GuildChannelABC, Thread, PrivateChannel]]:
|
||||
"""Returns a channel or thread with the given ID.
|
||||
|
||||
Parameters
|
||||
@@ -933,7 +934,7 @@ class Client:
|
||||
"""
|
||||
return self._connection.get_sticker(id)
|
||||
|
||||
def get_all_channels(self) -> Generator[GuildChannel, None, None]:
|
||||
def get_all_channels(self) -> Generator[GuildChannelABC, None, None]:
|
||||
"""A generator that retrieves every :class:`.abc.GuildChannel` the client can 'access'.
|
||||
|
||||
This is equivalent to: ::
|
||||
@@ -1488,6 +1489,7 @@ class Client:
|
||||
"""|coro|
|
||||
|
||||
Retrieves the bot's application information.
|
||||
This will fill up :attr:`application_id` and :attr:`application_flags`.
|
||||
|
||||
Raises
|
||||
-------
|
||||
@@ -1502,6 +1504,8 @@ class Client:
|
||||
data = await self.http.application_info()
|
||||
if "rpc_origins" not in data:
|
||||
data["rpc_origins"] = None
|
||||
|
||||
self._connection.store_appinfo(data)
|
||||
return AppInfo(self._connection, data)
|
||||
|
||||
async def fetch_user(self, user_id: int, /) -> User:
|
||||
@@ -1535,10 +1539,11 @@ class Client:
|
||||
data = await self.http.get_user(user_id)
|
||||
return User(state=self._connection, data=data)
|
||||
|
||||
async def fetch_channel(self, channel_id: int, /) -> Union[GuildChannel, PrivateChannel, Thread]:
|
||||
async def fetch_channel(self, channel_id: int, /) -> Union[GuildChannelABC, PrivateChannel, Thread]:
|
||||
"""|coro|
|
||||
|
||||
Retrieves a :class:`.abc.GuildChannel`, :class:`.abc.PrivateChannel`, or :class:`.Thread` with the specified ID.
|
||||
If found, will store the Channel in the internal cache, meaning :meth:``get_channel`` will succeed afterwards.
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -1570,14 +1575,18 @@ class Client:
|
||||
|
||||
if ch_type in (ChannelType.group, ChannelType.private):
|
||||
# the factory will be a DMChannel or GroupChannel here
|
||||
channel = factory(me=self.user, data=data, state=self._connection) # type: ignore
|
||||
channel: PrivateChannel = factory(me=self.user, data=data, state=self._connection) # type: ignore
|
||||
self._connection._add_private_channel(channel) # type: ignore
|
||||
else:
|
||||
# the factory can't be a DMChannel or GroupChannel here
|
||||
guild_id = int(data["guild_id"]) # type: ignore
|
||||
guild = self.get_guild(guild_id) or Object(id=guild_id)
|
||||
# GuildChannels expect a Guild, we may be passing an Object
|
||||
channel = factory(guild=guild, state=self._connection, data=data) # type: ignore
|
||||
guild = self.get_guild(guild_id)
|
||||
|
||||
if guild is None:
|
||||
return factory(guild=Object(guild_id), state=self._connection, data=data) # type: ignore
|
||||
|
||||
channel: GuildChannel = factory(guild=guild, state=self._connection, data=data) # type: ignore
|
||||
guild._add_channel(channel)
|
||||
return channel
|
||||
|
||||
async def fetch_webhook(self, webhook_id: int, /) -> Webhook:
|
||||
@@ -1606,6 +1615,7 @@ class Client:
|
||||
"""|coro|
|
||||
|
||||
Retrieves a :class:`.Sticker` with the specified ID.
|
||||
If found, will store the sticker in the internal cache, meaning :meth:``get_sticker`` will succeed afterwards.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
|
||||
@@ -1623,7 +1633,11 @@ class Client:
|
||||
"""
|
||||
data = await self.http.get_sticker(sticker_id)
|
||||
cls, _ = _sticker_factory(data["type"]) # type: ignore
|
||||
return cls(state=self._connection, data=data) # type: ignore
|
||||
|
||||
if data["type"] == StickerType.guild: # type: ignore
|
||||
return self._connection.store_sticker(data) # type: ignore
|
||||
else:
|
||||
return cls(state=self._connection, data=data) # type: ignore
|
||||
|
||||
async def fetch_premium_sticker_packs(self) -> List[StickerPack]:
|
||||
"""|coro|
|
||||
|
@@ -28,7 +28,6 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import collections
|
||||
import collections.abc
|
||||
from functools import cached_property
|
||||
|
||||
import inspect
|
||||
import importlib.util
|
||||
@@ -73,9 +72,7 @@ from .cog import Cog
|
||||
if TYPE_CHECKING:
|
||||
import importlib.machinery
|
||||
|
||||
from discord.role import Role
|
||||
from discord.message import Message
|
||||
from discord.abc import PartialMessageableChannel
|
||||
from ._types import (
|
||||
Check,
|
||||
CoroFunc,
|
||||
@@ -97,16 +94,9 @@ CXT = TypeVar("CXT", bound="Context")
|
||||
|
||||
class _FakeSlashMessage(discord.PartialMessage):
|
||||
activity = application = edited_at = reference = webhook_id = None
|
||||
attachments = components = reactions = stickers = []
|
||||
tts = False
|
||||
|
||||
raw_mentions = discord.Message.raw_mentions
|
||||
clean_content = discord.Message.clean_content
|
||||
channel_mentions = discord.Message.channel_mentions
|
||||
raw_role_mentions = discord.Message.raw_role_mentions
|
||||
raw_channel_mentions = discord.Message.raw_channel_mentions
|
||||
|
||||
attachments = components = reactions = stickers = mentions = []
|
||||
author: Union[discord.User, discord.Member]
|
||||
tts = False
|
||||
|
||||
@classmethod
|
||||
def from_interaction(
|
||||
@@ -118,22 +108,6 @@ class _FakeSlashMessage(discord.PartialMessage):
|
||||
|
||||
return self
|
||||
|
||||
@cached_property
|
||||
def mentions(self) -> List[Union[discord.Member, discord.User]]:
|
||||
client = self._state._get_client()
|
||||
if self.guild:
|
||||
ensure_user = lambda id: self.guild.get_member(id) or client.get_user(id) # type: ignore
|
||||
else:
|
||||
ensure_user = client.get_user
|
||||
|
||||
return discord.utils._unique(filter(None, map(ensure_user, self.raw_mentions)))
|
||||
|
||||
@cached_property
|
||||
def role_mentions(self) -> List[Role]:
|
||||
if self.guild is None:
|
||||
return []
|
||||
return discord.utils._unique(filter(None, map(self.guild.get_role, self.raw_role_mentions)))
|
||||
|
||||
|
||||
def when_mentioned(bot: Union[Bot, AutoShardedBot], msg: Message) -> List[str]:
|
||||
"""A callable that implements a command prefix equivalent to being mentioned.
|
||||
@@ -1292,7 +1266,7 @@ class BotBase(GroupMixin):
|
||||
|
||||
# Make our fake message so we can pass it to ext.commands
|
||||
message: discord.Message = _FakeSlashMessage.from_interaction(interaction, channel) # type: ignore
|
||||
message.content = f"/{command_name}"
|
||||
message.content = f"/{command_name} "
|
||||
|
||||
# Add arguments to fake message content, in the right order
|
||||
ignore_params: List[inspect.Parameter] = []
|
||||
@@ -1307,7 +1281,7 @@ class BotBase(GroupMixin):
|
||||
else:
|
||||
prefix = param.annotation.__commands_flag_prefix__
|
||||
delimiter = param.annotation.__commands_flag_delimiter__
|
||||
message.content += f" {prefix}{name}{delimiter}{option['value']}" # type: ignore
|
||||
message.content += f"{prefix}{name} {option['value']}{delimiter}" # type: ignore
|
||||
continue
|
||||
|
||||
option = next((o for o in command_options if o["name"] == name), None)
|
||||
@@ -1323,9 +1297,9 @@ class BotBase(GroupMixin):
|
||||
):
|
||||
# String with space in without "consume rest"
|
||||
option = cast(_ApplicationCommandInteractionDataOptionString, option)
|
||||
message.content += f" {_quote_string_safe(option['value'])}"
|
||||
message.content += f"{_quote_string_safe(option['value'])} "
|
||||
else:
|
||||
message.content += f' {option.get("value", "")}'
|
||||
message.content += f'{option.get("value", "")} '
|
||||
|
||||
ctx = await self.get_context(message)
|
||||
ctx._ignored_params = ignore_params
|
||||
|
@@ -951,6 +951,15 @@ class MemberCacheFlags(BaseFlags):
|
||||
"""
|
||||
return 2
|
||||
|
||||
@flag_value
|
||||
def fetched(self):
|
||||
""":class:`bool`: Whether to cache members that are fetched via :meth:``Guild.fetch_member``
|
||||
or :meth:``Guild.fetch_members``
|
||||
|
||||
Members that leave the guild are no longer cached.
|
||||
"""
|
||||
return 4
|
||||
|
||||
@classmethod
|
||||
def from_intents(cls: Type[MemberCacheFlags], intents: Intents) -> MemberCacheFlags:
|
||||
"""A factory method that creates a :class:`MemberCacheFlags` based on
|
||||
@@ -968,6 +977,8 @@ class MemberCacheFlags(BaseFlags):
|
||||
"""
|
||||
|
||||
self = cls.none()
|
||||
self.fetched = True
|
||||
|
||||
if intents.members:
|
||||
self.joined = True
|
||||
if intents.voice_states:
|
||||
|
@@ -428,7 +428,7 @@ class Guild(Hashable):
|
||||
self.mfa_level: MFALevel = guild.get("mfa_level")
|
||||
self.emojis: Tuple[Emoji, ...] = tuple(map(lambda d: state.store_emoji(self, d), guild.get("emojis", [])))
|
||||
self.stickers: Tuple[GuildSticker, ...] = tuple(
|
||||
map(lambda d: state.store_sticker(self, d), guild.get("stickers", []))
|
||||
map(lambda d: state.store_sticker(d), guild.get("stickers", []))
|
||||
)
|
||||
self.features: List[GuildFeature] = guild.get("features", [])
|
||||
self._splash: Optional[str] = guild.get("splash")
|
||||
@@ -1594,6 +1594,7 @@ class Guild(Hashable):
|
||||
"""|coro|
|
||||
|
||||
Retrieves all :class:`abc.GuildChannel` that the guild has.
|
||||
Will store the Channels in the internal cache, meaning :meth:``get_channel`` will succeed afterwards.
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -1616,11 +1617,12 @@ class Guild(Hashable):
|
||||
data = await self._state.http.get_all_guild_channels(self.id)
|
||||
|
||||
def convert(d):
|
||||
factory, ch_type = _guild_channel_factory(d["type"])
|
||||
factory, _ = _guild_channel_factory(d["type"])
|
||||
if factory is None:
|
||||
raise InvalidData("Unknown channel type {type} for channel ID {id}.".format_map(d))
|
||||
|
||||
channel = factory(guild=self, state=self._state, data=d)
|
||||
self._add_channel(channel)
|
||||
return channel
|
||||
|
||||
return [convert(d) for d in data]
|
||||
@@ -1712,6 +1714,8 @@ class Guild(Hashable):
|
||||
"""|coro|
|
||||
|
||||
Retrieves a :class:`Member` from a guild ID, and a member ID.
|
||||
If found, will store the Member in the internal cache, filling up :attr:`members`
|
||||
and meaning :meth:``get_member`` will succeed afterwards.
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -1737,7 +1741,11 @@ class Guild(Hashable):
|
||||
The member from the member ID.
|
||||
"""
|
||||
data = await self._state.http.get_member(self.id, member_id)
|
||||
return Member(data=data, state=self._state, guild=self)
|
||||
member = Member(data=data, state=self._state, guild=self)
|
||||
if self._state.member_cache_flags.fetched:
|
||||
self._add_member(member)
|
||||
|
||||
return member
|
||||
|
||||
async def try_member(self, member_id: int, /) -> Optional[Member]:
|
||||
"""|coro|
|
||||
@@ -2257,7 +2265,7 @@ class Guild(Hashable):
|
||||
payload["tags"] = emoji
|
||||
|
||||
data = await self._state.http.create_guild_sticker(self.id, payload, file, reason)
|
||||
return self._state.store_sticker(self, data)
|
||||
return self._state.store_sticker(data)
|
||||
|
||||
async def delete_sticker(self, sticker: Snowflake, *, reason: Optional[str] = None) -> None:
|
||||
"""|coro|
|
||||
|
@@ -188,8 +188,8 @@ class HTTPClient:
|
||||
self.proxy_auth: Optional[aiohttp.BasicAuth] = proxy_auth
|
||||
self.use_clock: bool = not unsync_clock
|
||||
|
||||
u_agent = "DiscordBot (https://github.com/iDevision/enhanced-discord.py {0}) Python/{1[0]}.{1[1]} aiohttp/{2}"
|
||||
self.user_agent: str = u_agent.format(__version__, sys.version_info, aiohttp.__version__)
|
||||
user_agent = "DiscordBot (https://github.com/Rapptz/discord.py {0}) Python/{1[0]}.{1[1]} aiohttp/{2}"
|
||||
self.user_agent: str = user_agent.format(__version__, sys.version_info, aiohttp.__version__)
|
||||
|
||||
def recreate(self) -> None:
|
||||
if self.__session.closed:
|
||||
|
@@ -137,7 +137,7 @@ class Interaction:
|
||||
|
||||
self.message: Optional[Message]
|
||||
try:
|
||||
self.message = Message(state=self._state, channel=self.channel, data=data["message"]) # type: ignore
|
||||
self.message = self._state.store_message(channel=self.channel, data=data["message"]) # type: ignore
|
||||
except KeyError:
|
||||
self.message = None
|
||||
|
||||
|
@@ -42,22 +42,12 @@ __all__ = (
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .types.audit_log import (
|
||||
AuditLog as AuditLogPayload,
|
||||
)
|
||||
from .types.guild import (
|
||||
Guild as GuildPayload,
|
||||
)
|
||||
from .types.message import (
|
||||
Message as MessagePayload,
|
||||
)
|
||||
from .types.user import (
|
||||
PartialUser as PartialUserPayload,
|
||||
)
|
||||
|
||||
from .types.threads import (
|
||||
Thread as ThreadPayload,
|
||||
)
|
||||
from .types.member import MemberWithUser as MemberWithUserPayload
|
||||
from .types.user import PartialUser as PartialUserPayload
|
||||
from .types.audit_log import AuditLog as AuditLogPayload
|
||||
from .types.message import Message as MessagePayload
|
||||
from .types.threads import Thread as ThreadPayload
|
||||
from .types.guild import Guild as GuildPayload
|
||||
|
||||
from .member import Member
|
||||
from .user import User
|
||||
@@ -354,7 +344,7 @@ class HistoryIterator(_AsyncIterator["Message"]):
|
||||
|
||||
channel = self.channel
|
||||
for element in data:
|
||||
await self.messages.put(self.state.create_message(channel=channel, data=element))
|
||||
await self.messages.put(self.state.store_message(channel=channel, data=element))
|
||||
|
||||
async def _retrieve_messages(self, retrieve) -> List[Message]:
|
||||
"""Retrieve messages and update next parameters."""
|
||||
@@ -615,14 +605,18 @@ class MemberIterator(_AsyncIterator["Member"]):
|
||||
if isinstance(after, datetime.datetime):
|
||||
after = Object(id=time_snowflake(after, high=True))
|
||||
|
||||
self.guild = guild
|
||||
self.limit = limit
|
||||
self.guild: Guild = guild
|
||||
self.after = after or OLDEST_OBJECT
|
||||
|
||||
self.state = self.guild._state
|
||||
self.get_members = self.state.http.get_members
|
||||
self.members = asyncio.Queue()
|
||||
|
||||
self.create_member = (
|
||||
self.create_member_cache if self.state.member_cache_flags.fetched else self.create_member_no_cache
|
||||
)
|
||||
|
||||
async def next(self) -> Member:
|
||||
if self.members.empty():
|
||||
await self.fill_members()
|
||||
@@ -657,11 +651,16 @@ class MemberIterator(_AsyncIterator["Member"]):
|
||||
for element in reversed(data):
|
||||
await self.members.put(self.create_member(element))
|
||||
|
||||
def create_member(self, data):
|
||||
def create_member_no_cache(self, data: MemberWithUserPayload) -> Member:
|
||||
from .member import Member
|
||||
|
||||
return Member(data=data, guild=self.guild, state=self.state)
|
||||
|
||||
def create_member_cache(self, data: MemberWithUserPayload) -> Member:
|
||||
member = self.create_member_no_cache(data)
|
||||
self.guild._add_member(member)
|
||||
return member
|
||||
|
||||
|
||||
class ArchivedThreadIterator(_AsyncIterator["Thread"]):
|
||||
def __init__(
|
||||
|
@@ -1331,7 +1331,7 @@ class Message(Hashable):
|
||||
payload["components"] = []
|
||||
|
||||
data = await self._state.http.edit_message(self.channel.id, self.id, **payload)
|
||||
message = Message(state=self._state, channel=self.channel, data=data)
|
||||
message = self._state.store_message(channel=self.channel, data=data)
|
||||
|
||||
if view and not view.is_finished():
|
||||
self._state.store_view(view, self.id)
|
||||
@@ -1756,7 +1756,7 @@ class PartialMessage(Hashable):
|
||||
"""
|
||||
|
||||
data = await self._state.http.get_message(self.channel.id, self.id)
|
||||
return self._state.create_message(channel=self.channel, data=data)
|
||||
return self._state.store_message(channel=self.channel, data=data)
|
||||
|
||||
async def edit(self, **fields: Any) -> Optional[Message]:
|
||||
"""|coro|
|
||||
@@ -1873,7 +1873,7 @@ class PartialMessage(Hashable):
|
||||
|
||||
if fields:
|
||||
# data isn't unbound
|
||||
msg = self._state.create_message(channel=self.channel, data=data) # type: ignore
|
||||
msg = self._state.store_message(channel=self.channel, data=data) # type: ignore
|
||||
if view and not view.is_finished():
|
||||
self._state.store_view(view, self.id)
|
||||
return msg
|
||||
|
@@ -27,7 +27,6 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
from collections import deque, OrderedDict
|
||||
import copy
|
||||
import datetime
|
||||
import itertools
|
||||
import logging
|
||||
from typing import Dict, Optional, TYPE_CHECKING, Union, Callable, Any, List, TypeVar, Coroutine, Sequence, Tuple, Deque
|
||||
@@ -75,6 +74,7 @@ if TYPE_CHECKING:
|
||||
from .types.sticker import GuildSticker as GuildStickerPayload
|
||||
from .types.guild import Guild as GuildPayload
|
||||
from .types.message import Message as MessagePayload
|
||||
from .types.appinfo import AppInfo as AppInfoPayload
|
||||
|
||||
T = TypeVar("T")
|
||||
CS = TypeVar("CS", bound="ConnectionState")
|
||||
@@ -323,6 +323,13 @@ class ConnectionState:
|
||||
for vc in self.voice_clients:
|
||||
vc.main_ws = ws # type: ignore
|
||||
|
||||
def store_message(self, channel: MessageableChannel, data: MessagePayload) -> Message:
|
||||
message = Message(state=self, channel=channel, data=data)
|
||||
if self._messages is not None:
|
||||
self._messages.append(message)
|
||||
|
||||
return message
|
||||
|
||||
def store_user(self, data: UserPayload) -> User:
|
||||
user_id = int(data["id"])
|
||||
try:
|
||||
@@ -353,11 +360,20 @@ class ConnectionState:
|
||||
self._emojis[emoji_id] = emoji = Emoji(guild=guild, state=self, data=data)
|
||||
return emoji
|
||||
|
||||
def store_sticker(self, guild: Guild, data: GuildStickerPayload) -> GuildSticker:
|
||||
def store_sticker(self, data: GuildStickerPayload) -> GuildSticker:
|
||||
sticker_id = int(data["id"])
|
||||
self._stickers[sticker_id] = sticker = GuildSticker(state=self, data=data)
|
||||
return sticker
|
||||
|
||||
def store_appinfo(self, data: AppInfoPayload):
|
||||
self.application_id = utils._get_as_snowflake(data, "id")
|
||||
|
||||
flags = data.get("flags")
|
||||
if flags is not None:
|
||||
self.application_flags = ApplicationFlags._from_value(flags)
|
||||
|
||||
return data
|
||||
|
||||
def store_view(self, view: View, message_id: Optional[int] = None) -> None:
|
||||
self._view_store.add_view(view, message_id)
|
||||
|
||||
@@ -563,9 +579,7 @@ class ConnectionState:
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
self.application_id = utils._get_as_snowflake(application, "id")
|
||||
# flags will always be present here
|
||||
self.application_flags = ApplicationFlags._from_value(application["flags"]) # type: ignore
|
||||
self.store_appinfo(application)
|
||||
|
||||
for guild_data in data["guilds"]:
|
||||
self._add_guild_from_data(guild_data)
|
||||
@@ -581,8 +595,7 @@ class ConnectionState:
|
||||
# channel would be the correct type here
|
||||
message = Message(channel=channel, data=data, state=self) # type: ignore
|
||||
self.dispatch("message", message)
|
||||
if self._messages is not None:
|
||||
self._messages.append(message)
|
||||
self.store_message(channel, data)
|
||||
# we ensure that the channel is either a TextChannel or Thread
|
||||
if channel and channel.__class__ in (TextChannel, Thread):
|
||||
channel.last_message_id = message.id # type: ignore
|
||||
@@ -1032,7 +1045,7 @@ class ConnectionState:
|
||||
for emoji in before_stickers:
|
||||
self._stickers.pop(emoji.id, None)
|
||||
# guild won't be None here
|
||||
guild.stickers = tuple(map(lambda d: self.store_sticker(guild, d), data["stickers"])) # type: ignore
|
||||
guild.stickers = tuple(map(lambda d: self.store_sticker(d), data["stickers"])) # type: ignore
|
||||
self.dispatch("guild_stickers_update", guild, before_stickers, guild.stickers)
|
||||
|
||||
def _get_create_guild(self, data):
|
||||
@@ -1403,11 +1416,6 @@ class ConnectionState:
|
||||
if channel is not None:
|
||||
return channel
|
||||
|
||||
def create_message(
|
||||
self, *, channel: Union[TextChannel, Thread, DMChannel, GroupChannel, PartialMessageable], data: MessagePayload
|
||||
) -> Message:
|
||||
return Message(state=self, channel=channel, data=data)
|
||||
|
||||
|
||||
class AutoShardedConnectionState(ConnectionState):
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
|
1274
docs/api.rst
1274
docs/api.rst
File diff suppressed because it is too large
Load Diff
@@ -49,7 +49,7 @@ autodoc_typehints = "none"
|
||||
# napoleon_attr_annotations = False
|
||||
|
||||
extlinks = {
|
||||
"issue": ("https://github.com/iDevision/enhanced-discord.py/issues/%s", "GH-"),
|
||||
"issue": ("https://github.com/Rapptz/discord.py/issues/%s", "GH-"),
|
||||
}
|
||||
|
||||
# Links used for cross-referencing stuff in other documentation
|
||||
@@ -168,8 +168,9 @@ html_context = {
|
||||
|
||||
resource_links = {
|
||||
"discord": "https://discord.gg/TvqYBrGXEm",
|
||||
"issues": "https://github.com/iDevision/enhanced-discord.py/issues",
|
||||
"examples": f"https://github.com/iDevision/enhanced-discord.py/tree/{branch}/examples",
|
||||
"issues": "https://github.com/Rapptz/discord.py/issues",
|
||||
"discussions": "https://github.com/Rapptz/discord.py/discussions",
|
||||
"examples": f"https://github.com/Rapptz/discord.py/tree/{branch}/examples",
|
||||
}
|
||||
|
||||
# Theme options are theme-specific and customize the look and feel of a theme
|
||||
|
@@ -38,6 +38,7 @@ If you're having trouble with something, these resources might help.
|
||||
- Ask us and hang out with us in our :resource:`Discord <discord>` server.
|
||||
- If you're looking for something specific, try the :ref:`index <genindex>` or :ref:`searching <search>`.
|
||||
- Report bugs in the :resource:`issue tracker <issues>`.
|
||||
- Ask in our :resource:`GitHub discussions page <discussions>`.
|
||||
|
||||
Extensions
|
||||
------------
|
||||
|
@@ -1,210 +0,0 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from discord import ButtonStyle, Embed, Interaction
|
||||
from discord.ui import Button, View
|
||||
|
||||
|
||||
class Bot(commands.Bot):
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
command_prefix=commands.when_mentioned_or("$"), intents=discord.Intents(guilds=True, messages=True)
|
||||
)
|
||||
|
||||
async def on_ready(self):
|
||||
print(f"Logged in as {self.user} (ID: {self.user.id})")
|
||||
print("------")
|
||||
|
||||
|
||||
# Define 3 View subclasses that we will switch between depending on if we are viewing the first page, a page in the middle, or the last page.
|
||||
# The first page has the "left" button disabled, because there is no left page to go to
|
||||
class FirstPageComps(View):
|
||||
def __init__(
|
||||
self,
|
||||
author_id: int,
|
||||
):
|
||||
super().__init__()
|
||||
self.value = None
|
||||
self.author_id = author_id
|
||||
|
||||
async def interaction_check(
|
||||
self,
|
||||
interaction: Interaction,
|
||||
):
|
||||
return interaction.user.id == self.author_id
|
||||
|
||||
@discord.ui.button(
|
||||
style=ButtonStyle.primary,
|
||||
disabled=True,
|
||||
label="Left",
|
||||
)
|
||||
async def left(
|
||||
self,
|
||||
button: discord.ui.Button,
|
||||
interaction: Interaction,
|
||||
):
|
||||
self.value = "left"
|
||||
self.stop()
|
||||
|
||||
@discord.ui.button(
|
||||
style=ButtonStyle.primary,
|
||||
label="Right",
|
||||
)
|
||||
async def right(
|
||||
self,
|
||||
button: discord.ui.Button,
|
||||
interaction: Interaction,
|
||||
):
|
||||
self.value = "right"
|
||||
self.stop()
|
||||
|
||||
|
||||
# The middle pages have both left and right buttons available
|
||||
class MiddlePageComps(View):
|
||||
def __init__(
|
||||
self,
|
||||
author_id: int,
|
||||
):
|
||||
super().__init__()
|
||||
self.value = None
|
||||
self.author_id = author_id
|
||||
|
||||
async def interaction_check(
|
||||
self,
|
||||
interaction: Interaction,
|
||||
):
|
||||
return interaction.user.id == self.author_id
|
||||
|
||||
@discord.ui.button(
|
||||
style=ButtonStyle.primary,
|
||||
label="Left",
|
||||
)
|
||||
async def left(
|
||||
self,
|
||||
button: discord.ui.Button,
|
||||
interaction: Interaction,
|
||||
):
|
||||
self.value = "left"
|
||||
self.stop()
|
||||
|
||||
@discord.ui.button(
|
||||
style=ButtonStyle.primary,
|
||||
label="Right",
|
||||
)
|
||||
async def right(
|
||||
self,
|
||||
button: discord.ui.Button,
|
||||
interaction: Interaction,
|
||||
):
|
||||
self.value = "right"
|
||||
self.stop()
|
||||
|
||||
|
||||
# The last page has the right button disabled, because there's no right page to go to
|
||||
class LastPageComps(View):
|
||||
def __init__(
|
||||
self,
|
||||
author_id: int,
|
||||
):
|
||||
super().__init__()
|
||||
self.value = None
|
||||
self.author_id = author_id
|
||||
|
||||
async def interaction_check(
|
||||
self,
|
||||
interaction: Interaction,
|
||||
):
|
||||
return interaction.user.id == self.author_id
|
||||
|
||||
@discord.ui.button(
|
||||
style=ButtonStyle.primary,
|
||||
label="Left",
|
||||
)
|
||||
async def left(
|
||||
self,
|
||||
button: discord.ui.Button,
|
||||
interaction: Interaction,
|
||||
):
|
||||
self.value = "left"
|
||||
self.stop()
|
||||
|
||||
@discord.ui.button(
|
||||
style=ButtonStyle.primary,
|
||||
label="Right",
|
||||
disabled=True,
|
||||
)
|
||||
async def right(
|
||||
self,
|
||||
button: discord.ui.Button,
|
||||
interaction: Interaction,
|
||||
):
|
||||
self.value = "right"
|
||||
self.stop()
|
||||
|
||||
|
||||
# Now we define the function that will take care of the pagination for us. It will take a list of Embeds and allow the user to cycle between them using left/right buttons
|
||||
# There is also an optional title parameter in case you want to give your paginator a title, it will display in the embed title, for example if the title is Book
|
||||
# then the title will look like "Book | Page 1/2". This is very optional and can be removed
|
||||
async def paginate(
|
||||
ctx,
|
||||
pages: list,
|
||||
title: str = None,
|
||||
):
|
||||
|
||||
total_pages = len(pages)
|
||||
first_page = pages[0]
|
||||
last_page = pages[-1]
|
||||
current_page = first_page
|
||||
index = 0
|
||||
|
||||
embed = first_page
|
||||
if title:
|
||||
embed.title = f"{title} | Page {index+1}/{total_pages}"
|
||||
|
||||
view = FirstPageComps(ctx.author.id)
|
||||
# Here we send the message with the view of the first page and the FirstPageComps buttons
|
||||
msg = await ctx.send(
|
||||
embed=embed,
|
||||
view=view,
|
||||
)
|
||||
|
||||
# The default timeout for Views is 3 minutes, but if a user selects to scroll between pages the timer will be reset.
|
||||
# If the timer reaches 0, though, the loop will just silently stop.
|
||||
while True:
|
||||
wait = await view.wait()
|
||||
if wait:
|
||||
return
|
||||
|
||||
if view.value == "right":
|
||||
index += 1
|
||||
current_page = pages[index]
|
||||
view = MiddlePageComps(ctx.author.id) if current_page != last_page else LastPageComps(ctx.author.id)
|
||||
embed = current_page
|
||||
if title:
|
||||
embed.title = f"{title} | Page {index+1}/{total_pages}"
|
||||
|
||||
elif view.value == "left":
|
||||
index -= 1
|
||||
current_page = pages[index]
|
||||
view = MiddlePageComps(ctx.author.id) if current_page != first_page else FirstPageComps(ctx.author.id)
|
||||
embed = current_page
|
||||
if title:
|
||||
embed.title = f"{title} | Page {index+1}/{total_pages}"
|
||||
|
||||
await msg.edit(
|
||||
embed=embed,
|
||||
view=view,
|
||||
)
|
||||
|
||||
|
||||
bot = Bot()
|
||||
|
||||
# To test our new function, let's create a list of a couple Embeds and let our paginator deal with the sending and buttons
|
||||
@bot.command()
|
||||
async def sendpages(ctx):
|
||||
page1 = Embed(description="This is page 1")
|
||||
page2 = Embed(description="This is page 2")
|
||||
page3 = Embed(description="This is page 3")
|
||||
await paginate(ctx, [page1, page2, page3])
|
||||
|
||||
|
||||
bot.run("token")
|
Reference in New Issue
Block a user