Skip to content
This repository was archived by the owner on Nov 22, 2023. It is now read-only.

Commit 7b0b657

Browse files
committed
Initial commit
0 parents  commit 7b0b657

File tree

7 files changed

+151
-0
lines changed

7 files changed

+151
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.vercel
2+
.env
3+
.venv
4+
__pycache__
5+
.pyc

.vercelignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.env
2+
__pycache__
3+
.vercel
4+
.venv

main.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from flask import Flask, request, abort
2+
from utils.discord import create_dm, send_dm, send_webhook
3+
from dotenv import load_dotenv
4+
import os
5+
import pymongo
6+
import datetime
7+
8+
load_dotenv()
9+
app = Flask(__name__)
10+
app.mongo = pymongo.MongoClient(os.environ['MONGO_URL'])
11+
app.db = app.mongo.tgk_database
12+
app.outh = app.db.outh
13+
app.votes = app.db.Votes
14+
15+
@app.route('/')
16+
def index():
17+
abort(404)
18+
19+
@app.route('/api/vote', methods=['POST'])
20+
def vote():
21+
if request.method != 'POST' or request.content_type != 'application/json' or request.headers['Authorization'] != os.environ['VOTE_PASSWORD']:
22+
return "Unauthorized", 401
23+
24+
user_data = app.votes.find_one({'_id': int(request.json['user'])})
25+
if user_data is None:
26+
user_data = {'_id': int(request.json['user']),'dm_id': None, 'votes': 0,'streak': 0,'last_vote': datetime.datetime.now(),'reminded': False}
27+
dm_id = create_dm(user_data['_id'])
28+
user_data['dm_id'] = int(dm_id)
29+
app.votes.insert_one(user_data)
30+
31+
user_data['votes'] += 1
32+
if (datetime.datetime.now() - user_data['last_vote']).total_seconds() > 108000:
33+
user_data['streak'] = 0
34+
else:
35+
user_data['streak'] += 1
36+
37+
user_data['last_vote'] = datetime.datetime.now()
38+
user_data['reminded'] = False
39+
40+
app.votes.update_one({'_id': int(request.json['user'])}, {'$set': user_data})
41+
if user_data['dm_id'] is None:
42+
dm_id = create_dm(user_data['_id'])
43+
user_data['dm_id'] = int(dm_id)
44+
app.votes.update_one({'_id': int(request.json['user'])}, {'$set': user_data})
45+
send_dm(dm_id=user_data['dm_id'], total_votes=user_data['votes'], streak=user_data['streak'])
46+
send_webhook(user_data['_id'], user_data['votes'], user_data['streak'])
47+
return "OK", 200

requirements.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
certifi==2022.9.24
2+
charset-normalizer==2.1.1
3+
click==8.1.3
4+
colorama==0.4.6
5+
dnspython==2.2.1
6+
Flask==2.2.2
7+
idna==3.4
8+
itsdangerous==2.1.2
9+
Jinja2==3.1.2
10+
MarkupSafe==2.1.1
11+
pymongo==4.3.3
12+
requests==2.28.1
13+
urllib3==1.26.13
14+
Werkzeug==2.2.2

utils/__init__.py

Whitespace-only changes.

utils/discord.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import requests
2+
import json
3+
import os
4+
import datetime
5+
6+
7+
headers = {
8+
'Authorization': f"Bot {os.environ['DISCORD_TOKEN']}",
9+
'Content-Type': 'application/json'
10+
}
11+
12+
webhook_headers = {
13+
'Content-Type': 'application/json'
14+
}
15+
16+
base_url = 'https://discord.com/api/v10'
17+
18+
def create_dm(user_id):
19+
data = {
20+
'recipient_id': user_id
21+
}
22+
r = requests.post(f'{base_url}/users/@me/channels', headers=headers, data=json.dumps(data))
23+
return r.json()['id']
24+
25+
def send_dm(dm_id:int, total_votes:int, streak:int):
26+
embed = {
27+
'type': 'rich',
28+
'title': '<a:tgk_redcrown:1005473874693079071> Thank you for voting!',
29+
'description': f"You have voted for [The Gambler's Kingdom](https://discord.gg/tgk) and have gotten the `࿔・゚♡ TGK Voter's ♡ ࿔・゚` role for 12 hours!" +"\nYou will be reminded to vote again in 12 hours (<t:" + str(int(datetime.datetime.timestamp(datetime.datetime.now() + datetime.timedelta(hours=12)))) + ":R>).",
30+
'color': 8257405,
31+
'url': 'https://discord.gg/yEPYYDZ3dD',
32+
'footer':{
33+
'text': "The Gambler's Kingdom - We appreciate your kind support!💖", 'icon_url': 'https://cdn.discordapp.com/icons/785839283847954433/a_23007c59f65faade4c973506d9e66224.gif?size=1024',
34+
},
35+
'fields': [
36+
{'name': "Total Votes: ", 'value': f"{total_votes}", 'inline': True},
37+
{'name': "Streak: ", 'value': f"{streak}", 'inline': True}
38+
]
39+
}
40+
payload = {'embeds': [embed]}
41+
42+
r = requests.post(f'{base_url}/channels/{dm_id}/messages', headers=headers, data=json.dumps(payload))
43+
return r.status_code
44+
45+
def send_webhook(user_id, total_votes, streak):
46+
user = requests.get(f'{base_url}/users/{user_id}', headers=headers).json()
47+
user_name = user['username'] + '#' + user['discriminator']
48+
embed = {
49+
'type': 'rich',
50+
'title': f'<a:tgk_redcrown:1005473874693079071> Thank you for voting! {user_name}',
51+
"description": f"You have voted for [The Gambler's Kingdom](https://discord.gg/tgk) and have gotten the <@&786884615192313866> role for 12 hours!" +"\nYou will be reminded to vote again in 12 hours (<t:" + str(int(datetime.datetime.timestamp(datetime.datetime.now() + datetime.timedelta(hours=12)))) + ":R>).",
52+
"color": int("7dff7d", 16),
53+
"url": "https://discord.gg/yEPYYDZ3dD",
54+
"thumbnail": {
55+
"url": "https://cdn.discordapp.com/emojis/830519601384128523.gif?v=1"
56+
},
57+
"fields": [
58+
{"name": "Total Votes: ", "value": f"{total_votes}", "inline": True},
59+
{"name": "Streak: ", "value": f"{streak}", "inline": True}
60+
],
61+
"footer": {"text": "The Gambler's Kingdom - We appreciate your kind support!💖", "icon_url": "https://cdn.discordapp.com/icons/785839283847954433/a_23007c59f65faade4c973506d9e66224.gif?size=1024"},
62+
}
63+
payload = {'embeds': [embed], 'username': f'OCT∆NΞ Logging', 'avatar_url': 'https://cdn.discordapp.com/avatars/816699167824281621/1bf01631b86f25cb052d64b69759e8d4.png?size=4096'}
64+
65+
r = requests.post(os.environ['WEBHOOK_URL'], headers=webhook_headers, data=json.dumps(payload))
66+
return r.status_code

vercel.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": 2,
3+
"builds": [
4+
{
5+
"src": "./main.py",
6+
"use": "@vercel/python"
7+
}
8+
],
9+
"routes": [
10+
{
11+
"src": "/(.*)",
12+
"dest": "/"
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)