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
|
2023-07-16 22:52:43 -07:00
|
|
|
#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
|
|
|
|
|
|
2024-01-17 18:09:45 -08:00
|
|
|
#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
|
2024-01-20 21:55:19 -08:00
|
|
|
bot = commands.Bot(command_prefix='!', intents=intents)
|
2024-01-17 18:09:45 -08:00
|
|
|
#discord setup END
|
2023-12-27 20:48:57 -08:00
|
|
|
|
2023-07-09 18:02:15 -07:00
|
|
|
|
2023-07-09 01:57:35 -07:00
|
|
|
async def folder_setup():
|
2024-01-25 00:13:16 -08:00
|
|
|
folder_names = ["tmp", "extensions", "data"]
|
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)
|
2024-01-17 22:35:46 -08:00
|
|
|
return folder_names
|
2023-07-09 16:06:45 -07:00
|
|
|
|
2024-01-25 00:13:16 -08:00
|
|
|
async def delete_all_files(path):
|
2023-07-09 16:06:45 -07:00
|
|
|
for filename in os.listdir(path):
|
2024-01-25 00:13:16 -08:00
|
|
|
if os.path.isdir(path+filename):
|
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
|
2024-01-20 01:33:35 -08:00
|
|
|
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")
|
2023-07-16 21:36:42 -07:00
|
|
|
|
2023-07-19 15:02:50 -07:00
|
|
|
async def create_session():
|
2023-07-25 17:13:23 -07:00
|
|
|
return aiohttp.ClientSession()
|
2023-07-19 15:02:50 -07:00
|
|
|
|
|
|
|
|
async def close_session(http_session):
|
|
|
|
|
await http_session.close()
|
|
|
|
|
|
|
|
|
|
@bot.event
|
|
|
|
|
async def on_connect():
|
|
|
|
|
bot.http_session = await create_session()
|
|
|
|
|
|
2023-07-21 23:25:55 -07:00
|
|
|
@bot.event
|
|
|
|
|
async def on_resumed():
|
|
|
|
|
bot.http_session = await create_session()
|
|
|
|
|
|
2023-07-19 15:02:50 -07:00
|
|
|
@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():
|
2024-01-25 00:13:16 -08:00
|
|
|
await delete_all_files("tmp/")
|
2024-01-20 00:34:54 -08:00
|
|
|
# Import plugins from extensions folder
|
2024-01-19 17:57:41 -08:00
|
|
|
for plugin_file in os.listdir('extensions/'):
|
2024-01-24 16:35:59 -08:00
|
|
|
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()
|
2024-01-09 12:10:21 -08:00
|
|
|
|
|
|
|
|
|
2023-07-09 00:57:11 -07:00
|
|
|
@bot.event
|
|
|
|
|
async def on_message(ctx):
|
2024-01-24 16:35:59 -08:00
|
|
|
# Don't allow commands in DMs for now
|
2024-01-06 09:18:25 -08:00
|
|
|
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
|
|
|
|
2024-01-24 16:35:59 -08:00
|
|
|
await bot.process_commands(ctx)
|
2023-07-09 00:57:11 -07:00
|
|
|
|
|
|
|
|
bot.run(discord_token)
|