forked from replit-discord/all-seeing-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background_tasks.py
113 lines (94 loc) · 2.52 KB
/
background_tasks.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sys
import asyncio
import discord
import datetime
import traceback
from tools.read_write import read, write
from utils import get_muted_role, error_log
from Moderation.spamchart import check_expire
async def log(text, guild, title='Automatic'):
log_embed = discord.Embed(
title=title,
description=text,
color=0x00e0f5
)
log_dict = await read('al')
try:
action_log_id = log_dict[guild.id]
print(action_log_id)
log_channel = discord.utils.get(guild.text_channels, id=action_log_id)
await log_channel.send(embed=log_embed)
except KeyError:
pass
except AttributeError:
pass
async def check_ban():
global client
banList = await read('banList')
delList = []
for guild_list in banList:
for userId in banList[guild_list]:
date = banList[guild_list][userId]
date = datetime.datetime.strptime(date, "%Y-%m-%w-%W %H:%M:%S")
if datetime.datetime.now() >= date:
delList.append([guild_list, userId])
for a in delList:
guild = client.get_guild(a[0])
banEntry = await guild.bans()
for each in banEntry:
if each.user.id == a[1]:
user = each.user
break
if not user:
print(f'user {a[1]} not found')
return
print('unbanning')
username = user.display_name
await log(
f'`{username}` has been unbanned because their time is up',
guild
)
await guild.unban(user=user, reason='User\'s time was up')
del banList[a[0]][a[1]]
await write('banList', banList)
async def check_mute():
global client
mute_list = await read('muteList')
del_list = []
for guild_list in mute_list:
for userId in mute_list[guild_list]:
date = mute_list[guild_list][userId]["timeup"]
date = datetime.datetime.strptime(date, "%Y-%m-%w-%W %H:%M:%S")
if datetime.datetime.now() >= date:
del_list.append([guild_list, userId])
for a in del_list:
guild = client.get_guild(a[0])
member_id_list = [member.id for member in guild.members]
if a[1] not in member_id_list:
del mute_list[a[0]][a[1]]
else:
user = guild.get_member(a[1])
username = user.display_name
await log(
f'`{username}` has been unmuted because their time is up',
guild
)
del mute_list[a[0]][a[1]]
await user.remove_roles(await get_muted_role(guild))
await write('muteList', mute_list)
async def bg_tasks(bot):
tasks = [
check_expire,
check_ban,
check_mute
]
while True:
for task in tasks:
try:
await task()
except Exception as e:
traceback_message = traceback.format_exc()
out = sys.exc_info()
await error_log(traceback_message, out, bot)
raise e
await asyncio.sleep(1)