-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTopGG.py
126 lines (110 loc) · 5.43 KB
/
TopGG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import datetime
import random
import dbl
import aiohttp
import discord
from discord.ext import commands, tasks
import logging
import Config
import Utils
class TopGG(commands.Cog):
"""Handles interactions with the top.gg API"""
def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession()
self.token = "!!!! TOPGG TOKEN !!!!"
self.dblpy = dbl.DBLClient(self.bot, self.token, webhook_path=#PATH,
webhook_auth="!!!! ENCHANTED SECRET !!!!", webhook_port=#PORT)
self.headers = {
"Authorization": self.token,
"Content-Type": "application/json",
"User-Agent": "!!! USER AGENT !!!!",
}
self.update_stats.start()
def cog_unload(self):
self.update_stats.cancel()
@tasks.loop(minutes=30.0)
async def update_stats(self):
"""This function runs every 30 minutes to automatically update your server count"""
Config.LOGGING.info('Attempting to post server count')
try:
await self.dblpy.post_guild_count()
Config.LOGGING.info('Posted server count ({})'.format(self.dblpy.guild_count()))
except Exception as e:
Config.LOGGING.exception('Failed to post server count\n{}: {}'.format(type(e).__name__, e))
@commands.Cog.listener()
async def on_dbl_vote(self, data):
weekend = False
data['user'] = int(data['user'])
account = Utils.get_account(data['user'])
print(data)
decide_reward = random.randint(1, 3)
async with self.session.get("https://top.gg/api/weekend", headers=self.headers) as r:
weekend = bool((await r.json())["is_weekend"])
if decide_reward == 1:
chest_amount = 1
embed = discord.Embed(title="Vote Redeemed",
description="Thanks for voting. You have redeemed:\n\n`1` Chest",
color=Config.MAINCOLOR,
timestamp=datetime.datetime.utcnow() + datetime.timedelta(hours=12))
embed.set_footer(text="You can vote again at")
if weekend:
chest_amount = 2
embed = discord.Embed(title="Vote Redeemed",
description="Thanks for voting. You have redeemed:\n\n`2` Chests",
color=Config.MAINCOLOR,
timestamp=datetime.datetime.utcnow() + datetime.timedelta(hours=12))
embed.set_footer(text="2x Vote | You can vote again at")
Config.USERS.update_one({'user_id': data['user']}, {'$inc': {'chests': chest_amount}})
elif decide_reward == 2:
amount = random.randint(20, 50)
if weekend:
amount *= 2
embed = discord.Embed(title="Vote Redeemed",
description="Thanks for voting. You have redeemed:\n\n`"+str(amount)+"` Rubies",
color=Config.MAINCOLOR,
timestamp=datetime.datetime.utcnow() + datetime.timedelta(hours=12))
embed.set_footer(text="You can vote again at")
if weekend:
embed.set_footer(text="2x Vote | You can vote again at")
Config.USERS.update_one({'user_id': data['user']}, {'$inc': {'rubies': amount}})
elif decide_reward == 3:
amount = random.randint(20, 70)
if weekend:
amount *= 2
embed = discord.Embed(title="Vote Redeemed",
description="Thanks for voting. You have redeemed:\n\n`"+str(amount)+"` Coins",
color=Config.MAINCOLOR,
timestamp=datetime.datetime.utcnow() + datetime.timedelta(hours=12))
embed.set_footer(text="You can vote again at")
if weekend:
embed.set_footer(text="2x Vote | You can vote again at")
Config.USERS.update_one({'user_id': data['user']}, {'$inc': {'coins': amount}})
if Config.EVENT_ACTIVE == "tower":
# Let user know they got tickets
amount = 1
if weekend:
amount *= 2
embed.description += "\n" + str(amount) + " Event Tickets"
# Add Tickets
try:
event_data = account['event']
event_data['tower']['tickets'] += amount
except KeyError:
event_data = {'tower': {'attempts': 0, 'high': 0, 'tickets': 3 + amount}}
Config.USERS.update_one({'user_id': data['user']}, {'$set': {'event': event_data}})
try:
user = await self.bot.fetch_user(int(data['user']))
await user.send(embed=embed)
except Exception as e:
await Config.LOGGING.error('Error occured in voting. Could not find user, or could not DM user. '
'Ignore this as it is not a big deal. But kinda is idk. '
'Its just not the best outcome.')
Config.LOGGING.info('Received a (' + data['type'] + ') action from : ' + str(data['user']))
@commands.Cog.listener()
async def on_dbl_test(self, data):
Config.LOGGING.info('Received a (' + data['type'] + ') action from : ' + str(data['user']))
def setup(bot):
global logger
logger = logging.getLogger('bot')
bot.add_cog(TopGG(bot))