-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrequest_loop.py
86 lines (73 loc) · 2.71 KB
/
request_loop.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
74
75
76
77
78
79
80
81
82
83
84
85
86
"""GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (c) 2021 gunyu1019
PUBG BOT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PUBG BOT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PUBG BOT. If not, see <http://www.gnu.org/licenses/>.
"""
import asyncio
import discord
import datetime
from discord.ext import interaction
from typing import Coroutine, Any, Callable, TypeVar, Optional
from pytz import timezone
from config.config import get_config
from module import pubgpy
from utils.location import comment
T = TypeVar("T")
parser = get_config()
color = int(parser.get("Color", "default"), 16)
error_color = int(parser.get("Color", "error"), 16)
warning_color = int(parser.get("Color", "warning"), 16)
async def request_loop(
context: interaction.ApplicationContext,
call: Callable[..., Coroutine[Any, Any, T]],
maximum_loop: int = 5,
language: str = None,
*args,
**kwargs,
) -> Optional[T]:
if language is None:
language = context.locale
for index in range(maximum_loop):
try:
result = await call(*args, **kwargs)
break
except pubgpy.TooManyRequests as error:
timer = (
error.reset
- datetime.datetime.now(tz=timezone("Asia/Seoul")).replace(tzinfo=None)
).total_seconds()
if timer < 0:
continue
v = int(timer / 5)
if timer % 5 >= 1:
v += 1
for count in range(v):
embed = discord.Embed(
title=comment("request_loop", "wait_list_title", language),
description=comment(
"request_loop", "wait_list_description", language
).format((v - count) * 5),
color=warning_color,
)
if index >= 1:
embed.description += " (재시도: {0}/5회)".format(index + 1)
await context.edit(embed=embed)
await asyncio.sleep(5)
else:
embed = discord.Embed(
title=comment("basic", "error", language),
description=comment("request_loop", "wait_list_failed", language),
color=error_color,
)
await context.edit(embed=embed)
return None
return result