Skip to content

Commit e60fd7e

Browse files
committed
Add extract_id_from_string() to easily extract a discord id from string
1 parent 9d6f102 commit e60fd7e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/utils/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import re
2+
from typing import Union
3+
14
import discord
25
from discord.errors import Forbidden
36

@@ -61,3 +64,16 @@ def make_embed(title="", color=blue_light, name="‌", value="‌", footer=None)
6164
emb.set_footer(text=footer)
6265

6366
return emb
67+
68+
69+
def extract_id_from_string(content: str) -> Union[int, None]:
70+
"""!
71+
Scans string to extract user/guild/message id\n
72+
Can extract IDs from mentions or plaintext
73+
74+
@return extracted id as int if exists, else None
75+
"""
76+
# matching string that has 18 digits surrounded by non-digits or start/end of string
77+
match = re.match(r'(\D+|^)(\d{18})(\D+|$)', content)
78+
79+
return int(match.group(2)) if match else None

0 commit comments

Comments
 (0)