mirror of
https://github.com/uhIgnacio/EmoteManager.git
synced 2024-08-15 02:23:13 +00:00
Merge branch 'master' into metrics
This commit is contained in:
commit
bcc4c79e64
3 changed files with 15 additions and 20 deletions
|
@ -76,7 +76,7 @@ class Emotes(commands.Cog):
|
||||||
+ self.bot.http.user_agent
|
+ self.bot.http.user_agent
|
||||||
})
|
})
|
||||||
|
|
||||||
self.emote_client = EmoteClient(token=self.bot.config['tokens']['discord'])
|
self.emote_client = EmoteClient(self.bot)
|
||||||
|
|
||||||
with open('data/ec-emotes-final.json') as f:
|
with open('data/ec-emotes-final.json') as f:
|
||||||
self.ec_emotes = json.load(f)
|
self.ec_emotes = json.load(f)
|
||||||
|
@ -444,7 +444,7 @@ class Emotes(commands.Cog):
|
||||||
image_data = await utils.image.resize_in_subprocess(image_data)
|
image_data = await utils.image.resize_in_subprocess(image_data)
|
||||||
if reason is None:
|
if reason is None:
|
||||||
reason = 'Created by ' + utils.format_user(self.bot, author_id)
|
reason = 'Created by ' + utils.format_user(self.bot, author_id)
|
||||||
return await self.emote_client.create(guild_id=guild.id, name=name, image=image_data, reason=reason)
|
return await self.emote_client.create(guild=guild, name=name, image=image_data, reason=reason)
|
||||||
|
|
||||||
@commands.command(aliases=('delete', 'delet', 'rm'))
|
@commands.command(aliases=('delete', 'delet', 'rm'))
|
||||||
async def remove(self, context, emote, *emotes):
|
async def remove(self, context, emote, *emotes):
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
aioec>=0.6.0
|
|
||||||
aiohttp_socks
|
aiohttp_socks
|
||||||
bot_bin>=1.5.0,<2.0.0
|
bot_bin>=1.5.0,<2.0.0
|
||||||
discord.py>=1.5.0,<2.0.0
|
discord.py>=1.5.0,<2.0.0
|
||||||
|
|
|
@ -53,22 +53,18 @@ class EmoteClient:
|
||||||
HTTPStatus.SERVICE_UNAVAILABLE: DiscordServerError,
|
HTTPStatus.SERVICE_UNAVAILABLE: DiscordServerError,
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, *, token):
|
def __init__(self, bot):
|
||||||
self.guild_rls: Dict[GuildId, GuildRetryTimes] = {}
|
self.guild_rls: Dict[GuildId, GuildRetryTimes] = {}
|
||||||
user_agent = (
|
|
||||||
'EmoteManager-EmoteClient; '
|
|
||||||
f'aiohttp/{aiohttp.__version__}; '
|
|
||||||
f'{platform.python_implementation()}/{".".join(map(str, sys.version_info))}'
|
|
||||||
)
|
|
||||||
self.http = aiohttp.ClientSession(headers={
|
self.http = aiohttp.ClientSession(headers={
|
||||||
'User-Agent': user_agent,
|
'User-Agent': bot.config['user_agent'] + ' ' + bot.http.user_agent,
|
||||||
'Authorization': 'Bot ' + token,
|
'Authorization': 'Bot ' + bot.config['tokens']['discord'],
|
||||||
'X-Ratelimit-Precision': 'millisecond',
|
'X-Ratelimit-Precision': 'millisecond',
|
||||||
})
|
})
|
||||||
|
# internals 🤫
|
||||||
|
self._connection_state = bot._connection
|
||||||
|
|
||||||
async def request(self, method, path, guild_id, **kwargs):
|
async def request(self, method, path, guild_id, **kwargs):
|
||||||
self._check_rl(method, guild_id)
|
self._check_rl(method, guild_id)
|
||||||
print('post check rl')
|
|
||||||
|
|
||||||
headers = {}
|
headers = {}
|
||||||
# Emote Manager shouldn't use walrus op until Debian adopts 3.8 :(
|
# Emote Manager shouldn't use walrus op until Debian adopts 3.8 :(
|
||||||
|
@ -80,7 +76,6 @@ class EmoteClient:
|
||||||
# TODO handle OSError and 500/502, like dpy does
|
# TODO handle OSError and 500/502, like dpy does
|
||||||
async with self.http.request(method, self.BASE_URL + path, **kwargs) as resp:
|
async with self.http.request(method, self.BASE_URL + path, **kwargs) as resp:
|
||||||
if resp.status == HTTPStatus.TOO_MANY_REQUESTS:
|
if resp.status == HTTPStatus.TOO_MANY_REQUESTS:
|
||||||
print('handling rl')
|
|
||||||
return await self._handle_rl(resp, method, path, guild_id, **kwargs)
|
return await self._handle_rl(resp, method, path, guild_id, **kwargs)
|
||||||
|
|
||||||
data = await json_or_text(resp)
|
data = await json_or_text(resp)
|
||||||
|
@ -94,17 +89,14 @@ class EmoteClient:
|
||||||
try:
|
try:
|
||||||
rls = self.guild_rls[guild_id]
|
rls = self.guild_rls[guild_id]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print('guild not found', repr(guild_id))
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if not rls.validate():
|
if not rls.validate():
|
||||||
print('guild rls invalid')
|
|
||||||
del self.guild_rls[guild_id]
|
del self.guild_rls[guild_id]
|
||||||
return
|
return
|
||||||
|
|
||||||
retry_at = getattr(rls, method, None)
|
retry_at = getattr(rls, method, None)
|
||||||
if retry_at:
|
if retry_at:
|
||||||
print('retry later')
|
|
||||||
raise RateLimitedError(retry_at)
|
raise RateLimitedError(retry_at)
|
||||||
|
|
||||||
async def _handle_rl(self, resp, method, path, guild_id, **kwargs):
|
async def _handle_rl(self, resp, method, path, guild_id, **kwargs):
|
||||||
|
@ -134,13 +126,17 @@ class EmoteClient:
|
||||||
def check_delete(self, guild_id):
|
def check_delete(self, guild_id):
|
||||||
self._check_rl('DELETE', guild_id)
|
self._check_rl('DELETE', guild_id)
|
||||||
|
|
||||||
async def create(self, *, guild_id, name, image: bytes, role_ids=(), reason=None):
|
async def create(self, *, guild, name, image: bytes, role_ids=(), reason=None):
|
||||||
return await self.request(
|
data = await self.request(
|
||||||
'POST', f'/guilds/{guild_id}/emojis',
|
'POST', f'/guilds/{guild.id}/emojis',
|
||||||
guild_id,
|
guild.id,
|
||||||
json=dict(name=name, image=image_utils.image_to_base64_url(image), roles=role_ids),
|
json=dict(name=name, image=image_utils.image_to_base64_url(image), roles=role_ids),
|
||||||
reason=reason,
|
reason=reason,
|
||||||
)
|
)
|
||||||
|
# internals 🤫
|
||||||
|
# this is A) so we can return a bona-fide, authentic, Emoji object,
|
||||||
|
# and B) because it's what dpy does to keep the emoji cache up to date for s
|
||||||
|
return self._connection_state.store_emoji(guild, data)
|
||||||
|
|
||||||
async def delete(self, *, guild_id, emote_id, reason=None):
|
async def delete(self, *, guild_id, emote_id, reason=None):
|
||||||
return await self.request('DELETE', f'/guilds/{guild_id}/emojis/{emote_id}', guild_id, reason=reason)
|
return await self.request('DELETE', f'/guilds/{guild_id}/emojis/{emote_id}', guild_id, reason=reason)
|
||||||
|
|
Loading…
Reference in a new issue