aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2021-04-24 08:53:36 -0400
committerRapptz <[email protected]>2021-04-24 08:53:36 -0400
commit1c312a158a585208840b220e7123ae18be85bec9 (patch)
tree4647b825ad107b77a13bc4889dd5976f77aa34bc
parentAdd Activity.buttons (diff)
downloaddiscord.py-1c312a158a585208840b220e7123ae18be85bec9.tar.xz
discord.py-1c312a158a585208840b220e7123ae18be85bec9.zip
[commands] Add FlagConverter.__iter__
-rw-r--r--discord/ext/commands/flags.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/discord/ext/commands/flags.py b/discord/ext/commands/flags.py
index 14ebb774..68a02911 100644
--- a/discord/ext/commands/flags.py
+++ b/discord/ext/commands/flags.py
@@ -40,6 +40,7 @@ from discord.utils import maybe_coroutine
from dataclasses import dataclass, field
from typing import (
Dict,
+ Iterator,
Optional,
Pattern,
Set,
@@ -443,6 +444,14 @@ class FlagConverter(metaclass=FlagsMeta):
how this converter works, check the appropriate
:ref:`documentation <ext_commands_flag_converter>`.
+ .. container:: operations
+
+ .. describe:: iter(x)
+
+ Returns an iterator of ``(flag_name, flag_value)`` pairs. This allows it
+ to be, for example, constructed as a dict or a list of pairs.
+ Note that aliases are not shown.
+
.. versionadded:: 2.0
Parameters
@@ -468,6 +477,10 @@ class FlagConverter(metaclass=FlagsMeta):
def _can_be_constructible(cls) -> bool:
return all(not flag.required for flag in cls.__commands_flags__.values())
+ def __iter__(self) -> Iterator[Tuple[str, Any]]:
+ for flag in self.__class__.__commands_flags__.values():
+ yield (flag.name, getattr(self, flag.attribute))
+
@classmethod
async def _construct_default(cls: Type[F], ctx: Context) -> F:
self: F = cls.__new__(cls)