diff --git a/core/discordAPI.py b/core/discordAPI.py new file mode 100644 index 0000000..3467756 --- /dev/null +++ b/core/discordAPI.py @@ -0,0 +1,23 @@ +import requests +from PyJS import JSON +from PyJS.modules import fs + +settings = JSON.parse(fs.createReadStream('settings.json')) + +params = { + "headers":{ + "Authorization": "Bot " + settings['bot_token'], + "Content-Type": "application/json" + } +} + +def get_guild(guild_id): + return requests.get("https://discord.com/api/guilds/" + str(guild_id) + "?with_counts=true", **params).json() + +def get_invite(channel_id): + data = { + "max_age": 0, + "max_uses": 0, + "unique": False + } + return requests.post("https://discord.com/api/channels/" + str(channel_id) + "/invites", json=data, **params) \ No newline at end of file diff --git a/data/routes.json b/data/routes.json index 9e26dfe..582e286 100644 --- a/data/routes.json +++ b/data/routes.json @@ -1 +1,10 @@ -{} \ No newline at end of file +{ + "view_server": { + "rule": "/server/", + "strict_slashes": false + }, + "view_server_default": { + "rule": "/server/", + "strict_slashes": false + } +} \ No newline at end of file diff --git a/main.py b/main.py index 3cb968d..1dc3f3b 100644 --- a/main.py +++ b/main.py @@ -22,19 +22,45 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from flask import Flask, render_template +from flask import Flask, render_template, redirect, url_for + +import humanfriendly from PyJS import JSON from PyJS.modules import fs +from core import discordAPI from core.database import Servers +from core.routes import route_for app = Flask(__name__) settings = JSON.parse(fs.createReadStream('settings.json')) +settings["len"] = len +settings["human"] = humanfriendly + +@app.errorhandler(404) +def _404Err(err): + return redirect(url_for('index')) @app.route("/") def index(): return render_template('index.html', servers=Servers.get_all(),**settings) +@app.route(**route_for('view_server_default'), defaults={"serverID": None}) +@app.route(**route_for('view_server')) +def view_server(serverID): + if not serverID: + redirect(url_for('index')) + + server = Servers.get_one(_id=int(serverID)) + + _data = discordAPI.get_guild(serverID) + + inv = discordAPI.get_invite(server['invite']).json() + + _data["invite"]= f"https://discord.gg/{inv['code']}" + + return render_template('view_server.html', server=server, discord=_data, **settings) + app.run(debug=True) \ No newline at end of file diff --git a/pug/base.pug b/pug/base.pug index 2b2ea8e..a1069c1 100644 --- a/pug/base.pug +++ b/pug/base.pug @@ -18,7 +18,6 @@ html(lang="en") div#particles-js script(src="{{url_for('static', filename='js/particles.js')}}") script(src="{{url_for('static', filename='js/app.js')}}") - script(src="{{url_for('static', filename='js/base.js')}}") script(src="https://cdnjs.cloudflare.com/ajax/libs/markdown-it/12.0.4/markdown-it.js") script(src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous") script(src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous") diff --git a/pug/view_server.pug b/pug/view_server.pug new file mode 100644 index 0000000..bb3e343 --- /dev/null +++ b/pug/view_server.pug @@ -0,0 +1,43 @@ +extends base.pug + +block title + title {{server['server_name']}} | {{site_name}} + +block links + link(rel="stylesheet", href="{{url_for('static', filename='styles/view_server.css')}}") + +block content + .container.shadow-lg + .card-header.bg-danger.bg-gradient.text-light.mx-auto#serverName {{server['server_name']}} + .card-body + .info + img.rounded-pill.border.border-5.border-danger(src="{{server['icon_url']}}", alt="") + + .info-column.mx-auto + a.btn.btn-success(href="{{discord['invite']}}" target="_blank") Join {{server['server_name']}} ({{discord['approximate_member_count']}}) + .card-body.bg-primary.bg-gradient.rounded.mx-auto + a.btn-warning.btn.rounded Members: {{human.format_number(discord['approximate_member_count'])}} + a.btn-warning.btn.rounded Emojis: {{len(discord['emojis'])}} + a.btn-warning.btn.rounded Roles: {{len(discord['roles'])}} + a.btn-warning.btn.rounded Boost Tier: {{discord['premium_tier']}} ({{discord['premium_subscription_count']}} Boosts) + p.renderMarkdown. + {% for line in server['description'].split('\n') %} + + {{line.replace(">", "").replace("\\", "")}} + + {% endfor %} + + +block scripts + script. + $(document).ready(() => { + + const md = window.markdownit(); + + const elements = document.getElementsByClassName("renderMarkdown"); + + for(var i = 0; i { - const currentTime = new Date(); - let hours = currentTime.getHours(); - let minutes = currentTime.getMinutes(); - let seconds = currentTime.getSeconds(); - - document.getElementById("time").innerHTML = `${hours > 9 ? hours : '0' + hours}:${minutes> 9 ? minutes : '0' + minutes}:${seconds> 9 ? seconds : '0' + seconds}`; - -}, 1000) \ No newline at end of file diff --git a/static/styles/view_server.css b/static/styles/view_server.css new file mode 100644 index 0000000..873d5d1 --- /dev/null +++ b/static/styles/view_server.css @@ -0,0 +1,47 @@ +.container { + margin-top : 10px; + background: rgb(233, 233, 233); +} + +#serverName { + text-align: center; + font-size: 20px; + font-weight: 600; + margin: 10px; +} + +.info { + display: flex; + flex-direction: row; + flex-wrap: wrap; +} + +.info-column { + display: flex; + flex-direction: column; + flex-wrap: wrap; + height: 100%; + margin-left: 30px; + width: 80%; + margin-top: 20px; +} + +.info > img { + height: 200px; + margin-bottom: 10px; +} + +.info-column > a { + height: 100%; +} + +.info-column > .card-body { + margin-top: 10px; + display: flex; + flex-direction: row; + flex-wrap: wrap; +} + +.card-body > a { + margin-right: 10px; +} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index a5a18c6..480a5fd 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1 +1 @@ -
\ No newline at end of file +
\ No newline at end of file diff --git a/templates/index.html b/templates/index.html index c24a131..9bccde4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,4 +1,4 @@ -Home | {{site_name}}
{% for server in servers %}
{{server['server_name']}}

{{server['description'][:100] + '...'}}

View Server
{% endfor %}
\ No newline at end of file