-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
apple_util.py
73 lines (58 loc) · 1.99 KB
/
apple_util.py
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
66
67
68
69
70
71
72
73
import time
import datetime
import discord
# AppleUtil - utility tool made by apple502j.
class RateLimiter(object):
"""rate: int, per: seconds"""
def __init__(self, rate, per):
self.rate = rate
self.per = per
self._token_left = rate
self._last_updated = time.time()
def use(self):
_upd = time.time()
if self._last_updated + self.per < _upd:
self._last_updated = _upd
self._token_left = self.rate - 1
return True
if self._token_left > 0:
self._token_left -= 1
return True
return False
class AppleUtil(object):
def __init__(self, bot):
self.bot = bot
@staticmethod
def has_all_perms(member, channel, *perms):
"""Check if someone has all permissions. channel may be None for global permissions."""
if not isinstance(member, discord.Member):
raise TypeError(
"has_all_perms take a Member as its first argument")
if channel:
perm = channel.permissions_for(member)
else:
perm = member.guild_permissions
for p in perms:
if not getattr(perm, p, False):
return False
return True
@staticmethod
def create_throttle(rate, per):
return RateLimiter(rate, per)
async def get_as_binary(self, url):
async with self.bot.session.get(url) as resp:
resp.raise_for_status()
return await resp.read()
async def get_as_json(self, url):
async with self.bot.session.get(url) as resp:
resp.raise_for_status()
return await resp.json()
async def get_as_text(self, url):
async with self.bot.session.get(url) as resp:
resp.raise_for_status()
return await resp.text()
@staticmethod
def within(dt, minutes):
return (datetime.datetime.now(datetime.timezone.utc) - dt) < datetime.timedelta(minutes=minutes)
def setup(bot):
pass