forked from Top-gg-Community/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_autoposter.py
More file actions
51 lines (33 loc) · 1.35 KB
/
test_autoposter.py
File metadata and controls
51 lines (33 loc) · 1.35 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
ORIGINAL_INTERVAL_INSTRUCTION = 'await asyncio.sleep(self.interval)'
MODIFIED_INTERVAL_INSTRUCTION = 'await asyncio.sleep(2)'
def replace_autopost_file(former: str, latter: str) -> None:
autopost_file_contents = None
with open('./topgg/autopost.py', 'r') as autopost_file:
autopost_file_contents = autopost_file.read().replace(former, latter)
with open('./topgg/autopost.py', 'w') as autopost_file:
autopost_file.write(autopost_file_contents)
replace_autopost_file(ORIGINAL_INTERVAL_INSTRUCTION, MODIFIED_INTERVAL_INSTRUCTION)
import topgg
import asyncio
import os
async def run() -> None:
try:
async with topgg.DBLClient(os.getenv('TOPGG_TOKEN')) as tg:
autoposter = tg.autopost()
@autoposter.stats
def get_guild_count() -> int:
return topgg.StatsWrapper(2)
@autoposter.on_success
def success() -> None:
print('Successfully posted statistics to the Top.gg API!')
@autoposter.on_error
def error(exc: Exception) -> None:
print(f'Error: {exc!r}')
autoposter.start()
await asyncio.sleep(15)
finally:
replace_autopost_file(
MODIFIED_INTERVAL_INSTRUCTION, ORIGINAL_INTERVAL_INSTRUCTION
)
if __name__ == '__main__':
asyncio.run(run())