aboutsummaryrefslogtreecommitdiffstats
path: root/main.py
blob: 25b7b337c5d4c955872b68c605f4a69a5b0e1cd6 (plain) (blame)
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
113
114
# This file is part of Senator Sperma
# Licensed under the GPL-3.0 License
# Copyright (C) 2026 Salmonik @ salmonik@tuta.io
# https://codeberg.org/salmonik/senator-sperma

import logging
import os
import sys
import json
import asyncio
import aiofiles
import discord
from discord.ext import commands
from dotenv import load_dotenv

from cogs.voice import get_voice_manager

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(levelname)s - %(filename)s - %(name)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout)
    ]
)
logger = logging.getLogger(__name__)

# Create a new event loop and set it as the current loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

intents = discord.Intents.all()
bot = commands.Bot(intents=intents, loop=loop)

settings = "settings.json"

@bot.event
async def on_ready():
    logger.info("ŁUKASZ SPERMA AKTYWACJA")
    
    # send the message to channel ID
    channel = bot.get_channel(1081667823799111804)

    await channel.send("ŁUKASZ SPERMA AKTYWACJA")
    
    # Create settings.json if it doesn't exist
    if not os.path.exists(settings):
        default_settings = {
            'volume': 1.0,
            'file_paths': ['audio', 'music', 'sounds', 'videos', 'images'],
            'baseurl': 'http://localhost:8080'
        }
        async with aiofiles.open(settings, 'w') as f:
            await f.write(json.dumps(default_settings, indent=2))
        logger.info("settings.json created")
    
    # Validate and fix settings.json if it exists
    if os.path.exists(settings):
        try:
            async with aiofiles.open(settings, 'r') as f:
                content = await f.read()
                if not content.strip():
                    raise json.JSONDecodeError("Empty file", "", 0)
                data = json.loads(content)
                assert 'volume' in data and isinstance(data['volume'], float)
                assert 'file_paths' in data and isinstance(data['file_paths'], list)
                assert 'baseurl' in data and isinstance(data['baseurl'], str)
        except (json.JSONDecodeError, AssertionError) as error:
            logger.error(f"Error loading settings.json: {error}")
            default_settings = {
                'volume': 1.0,
                'file_paths': ['audio', 'music', 'sounds', 'videos', 'images'],
                'baseurl': 'http://localhost:8080'
            }
            async with aiofiles.open(settings, 'w') as f:
                await f.write(json.dumps(default_settings, indent=2))
            logger.info("settings.json recreated")



if __name__ == "__main__":
    load_dotenv()
    extensions = [
        "cogs.vclogs",
        "cogs.compression",
        'cogs.tts',
        'cogs.web',
        'cogs.ai',
        'cogs.conversion',
        'cogs.math_commands',
        'cogs.downloader',
        'cogs.say',
        'cogs.jarvis',
        'cogs.music.play_commands',
        'cogs.music.stop',
        'cogs.music.join',
        'cogs.search',
    ]
    
    try:
        for i, extension in enumerate(extensions, 1):
            bot.load_extension(extension)
            logger.info(f"Loaded {i}/{len(extensions)} extensions")
        
        # Run the bot with the token from environment variables
        token = os.getenv("TOKEN")
        if not token:
            raise ValueError("No token provided. Please set the TOKEN environment variable.")
            
        bot.run(token)

    except Exception as e:
        logger.error(f"Error starting the bot: {e}")
        sys.exit(1)