implemented DM when it's time to BeReal.

This commit is contained in:
Showdown76 2024-03-02 23:18:11 +01:00
parent 540a61fb45
commit 7de887038d
Signed by: showdown
GPG Key ID: 062A80AA93C13988
5 changed files with 44 additions and 26 deletions

View File

@ -1,3 +1,4 @@
import time
import discord, asyncio import discord, asyncio
import os import os
from discord.ext import commands from discord.ext import commands
@ -11,13 +12,22 @@ intents.dm_messages = True
app = commands.Bot(command_prefix=",", intents=intents) app = commands.Bot(command_prefix=",", intents=intents)
app.db = database.controller.Database("database.db") app.db = database.controller.Database("database.db")
# load cogs # load cogs
async def load_cogs(): async def load_cogs():
for filename in os.listdir("bot/commands"): for filename in os.listdir("bot/commands"):
if filename.endswith(".py"): if filename.endswith(".py"):
await app.load_extension(f"bot.commands.{filename[:-3]}") await app.load_extension(f"bot.commands.{filename[:-3]}")
async def time_to_bereal():
# Get subscribers
subscribers = app.db.get_subscribers()
# Send message to all subscribers
for subscriber in subscribers:
user = await app.fetch_user(subscriber.user_id)
await user.send(f"# :warning: It's time to BeReal.\nOpen BeReal. to post what you are doing right now.\n<t:{int(time.time())}:R>")
app.time_to_bereal = time_to_bereal
@app.event @app.event
async def on_ready(): async def on_ready():

View File

@ -24,7 +24,7 @@ class Database:
def get_subscribers(self): def get_subscribers(self):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("SELECT user_id FROM subscribers") cursor.execute("SELECT * FROM subscribers")
result = cursor.fetchall() result = cursor.fetchall()
return [Subscriber(id=row[0], user_id=row[1], since=row[2]) for row in result] return [Subscriber(id=row[0], user_id=row[1], since=row[2]) for row in result]

View File

@ -4,14 +4,15 @@ from dotenv import load_dotenv
load_dotenv() load_dotenv()
from bot.app import app from bot.app import app
from webserver.webapp import webapp from webserver.webapp import WebServer
PORT = os.environ.get("PORT") PORT = os.environ.get("PORT")
@app.listen() @app.listen()
async def on_ready(): async def on_ready():
app.loop.create_task(webapp.run_task("0.0.0.0", PORT)) webapp = WebServer(app)
webapp.start(loop=app.loop)
app.run(os.environ.get("DISCORD_TOKEN")) app.run(os.environ.get("DISCORD_TOKEN"))

View File

@ -1,19 +0,0 @@
from quart import Blueprint, render_template, jsonify, request
bereal_time = Blueprint("bereal_time", __name__, template_folder="templates")
@bereal_time.route("/post", methods=["POST"])
async def time_to_bereal():
# May be coming from those IPs:
# 66.228.40.136 (us-east1.dbsrv.net)
# 2600:3c03::f03c:92ff:febe:a263/64
# fe80::f03c:92ff:febe:a263/64
data = await request.get_json()
return {"error": 200, "message": "Success"}
@bereal_time.errorhandler(405)
async def method_not_allowed(e):
return jsonify({"error": 405, "message": "Method not allowed"}), 405

View File

@ -1,6 +1,32 @@
from quart import Quart from quart import Quart
from discord.ext import commands
import asyncio
import os, dotenv
dotenv.load_dotenv()
from quart import request, jsonify
class WebServer:
def __init__(self, bot: commands.Bot):
self.bot = bot
webserver = Quart(__name__)
@webserver.route("/time_to_bereal/post", methods=["POST"])
async def time_to_bereal():
# May be coming from those IPs:
# 66.228.40.136 (us-east1.dbsrv.net)
# 2600:3c03::f03c:92ff:febe:a263/64
# fe80::f03c:92ff:febe:a263/64
data = await request.get_json()
task = asyncio.create_task(self.bot.time_to_bereal()) # Start asynchronous processing
return jsonify({"error": 200, "message": "Success"})
self.webserver = webserver
def start(self, loop):
loop.create_task(self.webserver.run_task("0.0.0.0", os.environ.get('PORT'), False))
from webserver.page.bereal_time import bereal_time
webapp = Quart(__name__)
webapp.register_blueprint(bereal_time, url_prefix="/time_to_bereal")