Ошибка в команде варна
with open('warning.json', 'w') as f:
json.dump({}, f)
f.close()
with open('warning.json', 'r') as f:
warnings = json.load(f)
def save_warnings():
with open('warning.json', 'w') as f:
json.dump(warnings, f)
def check_warnings(user_id):
count = 0
for warn_time, warn_user_id in warnings.items():
warn_date = datetime.datetime.fromtimestamp(int(warn_time)).strftime('%Y-%m-%d %H:%M:%S.%f')
week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
if datetime.datetime.strptime(warn_date, '%Y-%m-%d %H:%M:%S.%f') >= week_ago:
# проверка, что это предупреждение было выдано пользователю с заданным user_id
if warn_user_id == user_id:
count += 1
return count >= 3
@bot.command()
async def warn(ctx, user: discord.User, *, reason=None):
if not check_warnings(str(user.id)):
await ctx.send(f'{user.mention}, ты получил предупреждение за {reason}. Это твое предупреждение номер {warnings.get(str(user.id), {}).get("count", 1)}')
warnings[str(user.id)] = {
'count': warnings.get(str(user.id), {}).get('count', 0) + 1,
'last_warning': datetime.datetime.now().timestamp(),
}
save_warnings()
else:
await ctx.send(f'{user.mention}, у тебя уже есть 3 предупреждения за последнюю неделю. Ты забанен на 7 дней')
await ctx.guild.ban(user, reason=f'3 предупреждения за последнюю неделю. Причина: {reason}', delete_message_days=0)
При попытке выдать какому-либо пользователю больше одного варна возникает ошибка, к тому же варны сбрасываются каждый раз, а без первого блока бот не находит файл джсон...
Вот ошибка:
[2023-03-11 10:15:29] [ERROR ] discord.ext.commands.bot: Ignoring exception in command warn
Traceback (most recent call last):
File "C:\Python311\Lib\site-packages\discord\ext\commands\core.py", line 229, in wrapped
ret = await coro(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\Desktop\Server\botadmin.py", line 69, in warn
if not check_warnings(str(user.id)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\Desktop\Server\botadmin.py", line 59, in check_warnings
warn_date = datetime.datetime.fromtimestamp(int(warn_time)).strftime('%Y-%m-%d %H:%M:%S.%f')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python311\Lib\site-packages\discord\ext\commands\bot.py", line 1349, in invoke
await ctx.command.invoke(ctx)
File "C:\Python311\Lib\site-packages\discord\ext\commands\core.py", line 1023, in invoke
await injected(*ctx.args, **ctx.kwargs) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\discord\ext\commands\core.py", line 238, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OSError: [Errno 22] Invalid argument