sparkytron3000/sparkytron3000.py

101 lines
3 KiB
Python
Raw Normal View History

2023-07-09 00:57:11 -07:00
import discord
2023-07-10 10:38:28 -07:00
from discord.ext import commands, tasks
2023-07-09 00:57:11 -07:00
from discord.utils import get
import shutil
import json
import time
import os
from dotenv import load_dotenv
import aiohttp
2023-07-13 01:10:28 -07:00
#Stable Diffusion
#Set this env variable to http://host:port or "disabled"
2023-07-13 01:10:28 -07:00
#os.getenv('stablediffusion_url')
2023-07-09 00:57:11 -07:00
#env vars START
load_dotenv()
imgflip_username = os.getenv('imgflip_username')
imgflip_password = os.getenv('imgflip_password')
discord_token = os.getenv('discord_token')
ftp_server = os.getenv('ftp_server')
ftp_username = os.getenv('ftp_username')
ftp_password = os.getenv('ftp_password')
ftp_public_html = os.getenv('ftp_public_html')
#env vars END
#discord setup START
2024-01-20 23:04:12 -08:00
intents = discord.Intents.all()
2023-07-09 00:57:11 -07:00
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
#discord setup END
2023-07-09 18:02:15 -07:00
2023-07-09 01:57:35 -07:00
async def folder_setup():
2024-01-20 00:36:56 -08:00
# Only tmp, extensions and data are supported, all other folders only exist for backwards compatibility and will be removed soon!
2024-01-21 14:26:03 -08:00
folder_names = ["tmp", "extensions", "data", "channels","channels/config", "channels/logs"]
2023-07-09 01:57:35 -07:00
for folder_name in folder_names:
if not os.path.exists(folder_name):
os.mkdir(folder_name)
return folder_names
2023-07-09 16:06:45 -07:00
async def delete_all_files(path, safe_folders=None):
2023-07-09 16:06:45 -07:00
for filename in os.listdir(path):
if os.path.isdir(path+filename) and not path+filename in safe_folders:
2023-07-09 16:06:45 -07:00
shutil.rmtree(path+filename)
elif os.path.isfile(path+filename):
os.remove(path+filename)
2023-07-10 17:44:15 -07:00
2023-07-10 10:38:28 -07:00
2023-07-10 17:44:15 -07:00
@tasks.loop(seconds=1) # Run the task every second
2023-07-10 10:38:28 -07:00
async def task_loop():
current_time = time.localtime()
2023-07-10 17:44:15 -07:00
#Run daily tasks
if current_time.tm_hour == 0 and current_time.tm_min == 0 and current_time.tm_sec == 0:
2023-07-10 11:23:05 -07:00
try:
await delete_all_files("tmp/")
except Exception as error:
2024-01-24 16:42:39 -08:00
print("Failed to delete_all_files")
async def create_session():
2023-07-25 17:13:23 -07:00
return aiohttp.ClientSession()
async def close_session(http_session):
await http_session.close()
@bot.event
async def on_connect():
bot.http_session = await create_session()
@bot.event
async def on_resumed():
bot.http_session = await create_session()
@bot.event
async def on_disconnect():
await close_session(bot.http_session)
2023-07-09 00:57:11 -07:00
@bot.event
async def on_ready():
folders_made = await folder_setup()
await delete_all_files("tmp/", folders_made)
2024-01-20 00:34:54 -08:00
# Import plugins from extensions folder
for plugin_file in os.listdir('extensions/'):
if plugin_file[0] != '_' and plugin_file[-3:] == '.py':
2024-01-20 00:34:54 -08:00
await bot.load_extension(f'extensions.{plugin_file[:-3]}')
2023-07-09 00:57:11 -07:00
print('We have logged in as {0.user}'.format(bot))
2023-07-10 10:38:28 -07:00
task_loop.start()
2023-07-09 00:57:11 -07:00
@bot.event
async def on_message(ctx):
# Don't allow commands in DMs for now
if ctx.channel.type.value != 0 and ctx.author.id != 242018983241318410:
#This used to notify the user it cannot respond in this channel, but that spammed threads
return
2024-01-03 08:44:00 -08:00
await bot.process_commands(ctx)
2023-07-09 00:57:11 -07:00
bot.run(discord_token)