Специально для Вас и для опыта писал данный код:
import discord
import json
import asyncio
from discord.ext import commands
@client.event
async def on_ready():
with open("test.json", 'r') as f:
data = json.load(f)
for i in range(len(data)):
member = await (await client.fetch_guild(data[i]["guild_id"])).fetch_member(data[i]["id"])
client.loop.create_task(pun_start(member))
print("ready")
def check_user_db(user_id:int):
with open('test.json', 'r') as f:
data = json.load(f)
for i in range(len(data)):
if data[i]["id"] == user_id: return True
return False
def add_user_db(user_id:int, guild_id:int, author:str, timeout:int, reason:str, roles:list, role_pun:int):
if check_user_db(user_id) is True: return False
else:
with open('test.json', 'r') as f:
data = json.load(f)
new_user = {
"id": user_id,
"guild_id": guild_id,
"author": f"{author.name}#{author.discriminator}",
"timeout": timeout,
"reason": reason,
"roles": roles,
"role_pun": role_pun
}
data.append(new_user)
with open('test.json', 'w') as f:
json.dump(data, f, indent=3)
return True
def remove_user_db(user_id:int):
if check_user_db(user_id) is False: return False
else:
with open('test.json', 'r') as f:
data = json.load(f)
for i in range(len(data)):
if data[i]["id"] == user_id:
del data[i]
with open('test.json', 'w') as f:
json.dump(data, f)
return True
return False
async def pun_start(user):
with open('test.json', 'r') as f:
data = json.load(f)
user_data = {}
for i in range(len(data)):
if data[i]["id"] == user.id: user_data = data[i]; break
role_pun = discord.utils.get(user.guild.roles, id=id_роли) # роль для наказания
old_roles = user_data["roles"] # старые роли участника
await asyncio.sleep(user_data["timeout"])
await user.remove_roles(role_pun)
for role in old_roles:
role_user = discord.utils.get(user.guild.roles, id=role)
await user.add_roles(role_user)
remove_user_db(user.id)
@client.command()
async def pun(ctx, member:discord.Member=None, timeout:int=None, reason:str="Не указано"):
if member is None or timeout is None:
await ctx.reply("Укажите участника и время.\nПример: !pun @участник 100 - наказываем участника на 100сек.\n**Важно:** Время указывается в секундах!")
elif check_user_db(member.id) is True:
await ctx.reply("Участника уже наказан!")
else:
role_pun = discord.utils.get(ctx.guild.roles, id=id_роли) # роль для наказания
old_roles = [] # старые роли участника
for role in member.roles:
if role.name != "@everyone":
old_roles.append(role.id)
await member.remove_roles(role)
await member.add_roles(role_pun)
add_user_db(member.id, ctx.guild.id, ctx.author, timeout, reason, list(old_roles), role_pun.id) # добавляем участника в файл
await ctx.send(f"{member.mention}, наказан!\nПричина: {reason}")
await asyncio.sleep(timeout)
await member.remove_roles(role_pun)
for role in old_roles:
role_user = discord.utils.get(ctx.guild.roles, id=role)
await member.add_roles(role_user)
remove_user_db(member.id)
await ctx.send(f"Наказания с {member.mention} снято!\nПричина: Автоматическое снятие")
Если будут ошибки пишите Xpeawey#5262