### Distillery Bot Initialization
The Distillery Bot is initialized by loading the necessary configuration files and setting up the Discord bot using the discord.py library. This includes defining intents, registering event listeners, and creating the `DistilleryCog` class.
### loading the configuration
The initialization process starts by loading the configuration from the `config.json` file:
```python
try:
CONFIG = json.load(open('config/config.json'))
except Exception as e:
print("Failed to load config.json file. Please make sure it exists and is valid. Error:", e)
exit(1)
```
The loaded configuration is then used to set up various settings and constants, such as the application name, server ID, and command-related settings:
```python
APP_NAME = CONFIG['APP_NAME']
DISTILLERY_COMMANDS = CONFIG['DISTILLERY_COMMANDS']
SECONDS_PER_TICK = CONFIG['SECONDS_PER_TICK']
MAX_RESEND_ATTEMPTS = CONFIG['MAX_RESEND_ATTEMPTS']
DISCORD_TOKEN = os.getenv('DISCORD_BOT_TOKEN')
SERVER_ID = int(CONFIG["SERVER_ID"])
```
### Bot Initialization and Discord Intents
The bot is initialized using the discord.py library [[discord.py library]], with the necessary intents and command prefix:
```python
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
intents.members = True
bot = commands.AutoShardedBot(command_prefix='/', intents=intents)
```
Detailed explanation of the above code: [[Distillery Discord Bot Intents]]
### Scheduling Periodic Tasks
```python
scheduler = AsyncIOScheduler(timezone=timezone('US/Eastern')) # Schedule periodic tasks
scheduler.add_job(reset_request_counter, CronTrigger(day_of_week='0-6', hour=0, minute=0), args=["number_of_free_generations_today"]) # Every day at midnight
scheduler.add_job(reset_request_counter, CronTrigger(day=1, hour=0, minute=0), args=["number_of_paid_generations_this_month"]) # Every first day of the month at midnight
scheduler.add_job(reset_request_counter, CronTrigger(second=0), args=["number_of_generations_this_minute"]) # Every minute at the 0th second
scheduler.add_job(reset_request_counter, CronTrigger(minute=0), args=["number_of_generations_this_hour"]) # Every hour at the 0th minute
scheduler.add_job(reset_request_counter, CronTrigger(day_of_week='0-6', hour=0, minute=0), args=["number_of_generations_this_day"]) # Every day at midnight
scheduler.add_job(reset_request_counter, CronTrigger(day_of_week=0, hour=0, minute=0), args=["number_of_generations_this_week"]) # Every week at Sunday midnight
scheduler.add_job(reset_request_counter, CronTrigger(day=1, hour=0, minute=0), args=["number_of_generations_this_month"]) # Every first day of the month at midnight
scheduler.add_job(reset_request_counter, CronTrigger(second=0), args=["number_of_trainings_this_minute"]) # TRAINING - Every minute at the 0th second
scheduler.add_job(refill_subscription_distill_credits, CronTrigger(day=1, hour=0, minute=0), args=[]) # Every first day of the month at midnight
```
During the initialization process, the bot sets up an `AsyncIOScheduler` from the `apscheduler` library to handle the scheduling of periodic tasks. The scheduler is configured with the 'US/Eastern' timezone.
Several tasks are scheduled using the `add_job` method of the scheduler:
1. `reset_request_counter` is scheduled to run every day at midnight to reset the `number_of_free_generations_today` counter.
2. `reset_request_counter` is scheduled to run every first day of the month at midnight to reset the `number_of_paid_generations_this_month` counter.
3. `reset_request_counter` is scheduled to run every minute at the 0th second to reset the `number_of_generations_this_minute` counter.
4. `reset_request_counter` is scheduled to run every hour at the 0th minute to reset the `number_of_generations_this_hour` counter.
5. `reset_request_counter` is scheduled to run every day at midnight to reset the `number_of_generations_this_day` counter.
6. `reset_request_counter` is scheduled to run every week on Sunday at midnight to reset the `number_of_generations_this_week` counter.
7. `reset_request_counter` is scheduled to run every first day of the month at midnight to reset the `number_of_generations_this_month` counter.
8. `reset_request_counter` is scheduled to run every minute at the 0th second to reset the `number_of_trainings_this_minute` counter for training.
9. `refill_subscription_distill_credits` is scheduled to run every first day of the month at midnight to refill subscription distill credits.
These scheduled tasks ensure that the relevant counters and credits are reset or refilled at the appropriate intervals, such as daily, monthly, or minute-wise.
### Starting the Bot
After scheduling the tasks, the [[DistilleryCog]] is added to the bot, and the bot is started using the Discord bot token.
```python
bot.add_cog(DistilleryCog(bot))
bot.run(DISCORD_TOKEN)
```