This repository was archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (59 loc) · 2 KB
/
utils.py
File metadata and controls
65 lines (59 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import aiohttp
import discord
from discord.ext import commands
from discord import utils
async def get_webhook(bot,channelid,webhookid):
'''
Allows you to get webhook
:param ctx: Context object
:param channelid: Id of the channel
:param webhookid: Id of the webhook
:return: The corresponding Webhook object, None if not found
'''
webhook = list(filter(lambda x: x.id == webhookid, (await bot.get_channel(channelid).webhooks())))
if webhook == []:
return None
return webhook[0]
def partition(text: str, length: int)->list:
"""
split a text according to its length
:param text: input text
:param length: maximum length of each substring
:return: a list of substrings
"""
res=[]
while len(text)>0:
res.append(text[:length])
text=text[length:]
return res
def tdm(td):
"""
converting a datetime.datetime object to a timestamp
:param td: A datetime.datetime instance
:return: time in milliseconds
"""
return ((td.days * 86400000) + (td.seconds * 1000)) + (td.microseconds / 1000)
def find(substring, string):
"""
gets the occurrences of the substring in the main string
:param substring: the text to find
:param string: the main search string
:return: the indices of occurrence
"""
last_found = -1 # Begin at -1 so the next position to search from is 0
while True:
# Find next index of substring, by starting after its last known position
last_found = string.find(substring, last_found + 1)
if last_found == -1:
break # All occurrences have been found
yield last_found
async def getjson(url):
"""
gets json content from url if supported
:param url: the url used
:return: the json output
"""
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
f = await response.json(encoding='utf8')
return f