aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils/helpers.py
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-10-05 19:17:07 -0700
committerGravatar MarkKoz <[email protected]>2020-10-05 19:21:52 -0700
commit08d4a5d33b0e5d5bb82a00617f51218a7c124a4f (patch)
tree3d34ddc273fbf1858a84c443035f9a082121d23d /bot/utils/helpers.py
parentCode block: support the "pycon" language specifier (diff)
parentMerge pull request #1215 from python-discord/joseph/verification (diff)
Merge master and fix conflicts
* Use absolute imports in the code block modules for consistency * Move has_lines utility function into the helpers module * Prefix names of code block modules with underscores so they won't get picked up as extensions by the extension manager
Diffstat (limited to '')
-rw-r--r--bot/utils/helpers.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py
new file mode 100644
index 000000000..b5c13ac9e
--- /dev/null
+++ b/bot/utils/helpers.py
@@ -0,0 +1,31 @@
+from abc import ABCMeta
+from typing import Optional
+
+from discord.ext.commands import CogMeta
+
+
+class CogABCMeta(CogMeta, ABCMeta):
+ """Metaclass for ABCs meant to be implemented as Cogs."""
+
+
+def find_nth_occurrence(string: str, substring: str, n: int) -> Optional[int]:
+ """Return index of `n`th occurrence of `substring` in `string`, or None if not found."""
+ index = 0
+ for _ in range(n):
+ index = string.find(substring, index+1)
+ if index == -1:
+ return None
+ return index
+
+
+def has_lines(string: str, count: int) -> bool:
+ """Return True if `string` has at least `count` lines."""
+ split = string.split("\n", count - 1)
+
+ # Make sure the last part isn't empty, which would happen if there was a final newline.
+ return split[-1] and len(split) == count
+
+
+def pad_base64(data: str) -> str:
+ """Return base64 `data` with padding characters to ensure its length is a multiple of 4."""
+ return data + "=" * (-len(data) % 4)