Skip to content

Commit c5f61a6

Browse files
committed
Move to .env
1 parent 5c80d6b commit c5f61a6

File tree

8 files changed

+58
-32
lines changed

8 files changed

+58
-32
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
TOKEN = TOKEN
2+
PREFIX = PREFX
3+
VERSION = VER
4+
OWNER_IDS = id1, id2

.github/workflows/pylint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
- name: Install dependencies
1818
run: |
1919
python -m pip install --upgrade pip
20+
pip install -r requirements.txt
2021
pip install pylint
2122
- name: Analysing the code with pylint
2223
run: |

example_config.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

main.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
"""This project is licensed under GPL-v3."""
22

3-
import json
43
import os
5-
import asyncio
4+
import json
65
import time
6+
import asyncio
77

88
import disnake
9-
109
from disnake.ext import commands
1110

12-
import config as cfg
11+
from dotenv import load_dotenv
12+
13+
load_dotenv()
14+
15+
PREFIX = os.getenv("PREFIX", "!")
16+
TOKEN = os.getenv("TOKEN")
1317

14-
client = commands.Bot(command_prefix=cfg.PREFIX, intents=disnake.Intents.all())
18+
client = commands.Bot(command_prefix=PREFIX, intents=disnake.Intents.all())
1519
DATA_FILE = "forums.json"
1620
client.remove_command("help")
1721

@@ -26,6 +30,7 @@ def bot_embed(inter):
2630
else client.user.display_avatar.url
2731
)
2832
err_embed.set_footer(text=f"{inter.author.name}", icon_url=user_avatar)
33+
return err_embed
2934

3035

3136
def load_data():
@@ -176,8 +181,8 @@ async def check_inactivity_thread(thread_id: int):
176181
COG_NAME = f"modules.{filename[:-3]}"
177182
try:
178183
client.load_extension(COG_NAME)
179-
print(f"Debug: Ког {filename} загружен")
184+
print(f"DEBUG: Ког {filename} загружен")
180185
except (disnake.ext.commands.errors.ExtensionError, ImportError) as e:
181186
print(f"ERR: Ког {filename} не удалось загрузить: {e}")
182187

183-
client.run(cfg.TOKEN)
188+
client.run(TOKEN)

modules/errors.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,42 @@ async def on_command_error(
3636

3737
err_embed.description = "Вы не разработчик бота."
3838
return await ctx.reply(embed=err_embed, delete_after=40)
39+
40+
error_id = str(uuid.uuid4())[:8]
41+
date_str = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
42+
filename = f"error_{error_id}_{date_str}.log"
43+
44+
os.makedirs("logs", exist_ok=True)
45+
full_path = os.path.join("logs", filename)
46+
47+
def ifdm(inter):
48+
return inter.guild.name if inter.guild else 'DM'
49+
50+
with open(full_path, "w", encoding="utf-8") as f:
51+
f.write(f"=== Ошибка {error_id} ({date_str}) ===\n")
52+
f.write(f"Пользователь: {ctx.author} (ID: {ctx.author.id})\n")
53+
f.write(
54+
f"Гильдия: {ifdm(ctx)} (ID: {ifdm(ctx)})\n"
55+
)
56+
f.write(f"Команда: {ctx.command.name}\n\n")
57+
traceback.print_exception(type(error), error, error.__traceback__, file=f)
58+
59+
print(
60+
f"[ERROR] ID={error_id} | {type(error).__name__}: {error} (сохранено в {full_path})"
61+
)
62+
63+
err_embed.description = (
64+
f"Произошла неизвестная ошибка.\n"
65+
f"Сообщите UUID разработчику.\n\n"
66+
f"UUID: `{error_id}"
67+
)
68+
await ctx.reply(embed=err_embed, ephemeral=True)
3969

4070
### Triggers when an error occurs in a slash command ###
4171

4272
@commands.Cog.listener()
4373
async def on_slash_command_error(
44-
self: any, inter: disnake.ApplicationCommandInteraction, error: Exception
74+
self, inter: disnake.ApplicationCommandInteraction, error: Exception
4575
) -> None:
4676
"""Handles errors in slash commands."""
4777

modules/info.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""GPL-3.0 License"""
2+
import os
23
import platform
34
import time
45

56
import disnake
67
from disnake.ext import commands
78

8-
from config import VERSION
9-
9+
VERSION = os.getenv("VERSION", "Fail")
1010

1111
class InfoModule(commands.Cog):
1212
"""Cog for providing information about the bot"""

modules/listener.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ async def on_thread_create(self, thread: disnake.Thread):
106106
self.bot.save_data(data)
107107

108108
self.bot.loop.create_task(self.bot.check_inactivity_thread(thread.id))
109+
109110

110111

111112
def setup(bot):

modules/owner.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
from disnake.ext import commands
1010
from disnake.ui import Button, View
1111

12-
from config import OWNER_IDS
1312
from modules import errors as em
1413

15-
### Check if user is bot owner ###
16-
14+
OWNER_IDS = [int(i) for i in os.getenv("OWNER_IDS", "").replace(" ", "").split(",") if i]
1715

1816
def is_bot_owner():
1917
"""Check if the user is the bot owner."""
@@ -24,29 +22,17 @@ def predicate(ctx):
2422

2523
return commands.check(predicate)
2624

27-
2825
class OwnerModule(commands.Cog):
2926
"""Cog for bot owner commands"""
3027
def __init__(self, bot):
3128
self.bot = bot
3229

33-
### Reboot command for bot owner ###
34-
3530
@commands.command()
3631
@is_bot_owner()
3732
async def reboot(self, ctx):
3833
"""Reboot command for bot owner"""
3934
bot = self.bot
40-
resp_embed = bot.err_embed(ctx)
41-
err_embed = bot.err_embed(ctx)
42-
43-
if ctx.author.id not in OWNER_IDS:
44-
resp_embed.description = "У вас нет прав на перезагрузку бота."
45-
return await ctx.send(embed=err_embed, delete_after=10)
46-
47-
resp_embed.title = "Подтверждение перезагрузки"
48-
resp_embed.description = "Вы уверены что хотите перезапустить бота?"
49-
resp_embed.color = disnake.Color.orange()
35+
err_embed = bot.bot_embed(ctx)
5036

5137
class ConfirmView(View):
5238
"""View for confirmation buttons"""
@@ -114,6 +100,11 @@ async def on_timeout(self):
114100
except (disnake.Forbidden, disnake.HTTPException, disnake.NotFound):
115101
pass
116102

103+
resp_embed = self.bot.bot_embed(ctx)
104+
resp_embed.title = "Подтверждение перезагрузки"
105+
resp_embed.description = "Вы уверены что хотите перезапустить бота?"
106+
resp_embed.color = disnake.Color.orange()
107+
117108
message = await ctx.send(embed=resp_embed, view=ConfirmView())
118109

119110

0 commit comments

Comments
 (0)