2020-10-13 21:02:40 +02:00
from data import constants
2020-10-23 21:34:10 +02:00
from data . remind import Reminders
2020-10-13 21:02:40 +02:00
from discord . ext import commands , tasks
from enums . numbers import Numbers
from functions import timeFormatters
2021-01-24 22:31:09 +01:00
from functions . config import config
2020-10-13 21:02:40 +02:00
from functions . database import currency , poke , prison , birthdays , stats
2021-08-08 23:24:16 +02:00
from functions . scrapers . sporza import getMatchweek
2021-03-03 18:04:31 +01:00
from functions import ufora_notifications
2020-10-13 21:02:40 +02:00
import json
2020-10-23 22:12:05 +02:00
import random
2020-10-13 21:02:40 +02:00
import requests
import time
class Tasks ( commands . Cog ) :
def __init__ ( self , client ) :
self . client = client
self . bankInterest . start ( )
self . resetPrison . start ( )
self . resetLost . start ( )
# self.resetPoke.start()
self . checkBirthdays . start ( )
self . updateMessageCounts . start ( )
2020-10-23 20:56:20 +02:00
self . sendReminders . start ( )
2021-01-24 22:31:09 +01:00
self . updateMatchweek . start ( )
2021-03-03 18:04:31 +01:00
self . uforaAnnouncements . start ( )
2020-10-13 21:02:40 +02:00
@tasks.loop ( hours = 1.0 )
async def bankInterest ( self ) :
2020-10-23 20:56:20 +02:00
"""
Task that gives daily interest
"""
2020-10-13 21:02:40 +02:00
# Don't do it multiple times a day if bot dc's, ...
with open ( " files/lastTasks.json " , " r " ) as fp :
lastTasks = json . load ( fp )
if int ( self . getCurrentHour ( ) ) == 0 and int ( time . time ( ) ) - int ( lastTasks [ " interest " ] ) > 10000 :
users = currency . getAllRows ( )
bitcoinPrice = self . getCurrentBitcoinPrice ( )
for user in users :
# People in prison don't get interest
if len ( prison . getUser ( int ( user [ 0 ] ) ) ) != 0 :
continue
if float ( user [ 3 ] ) != 0.0 :
currency . update ( user [ 0 ] , " investeddays " , int ( user [ 4 ] ) + 1 )
profit = ( ( float ( user [ 3 ] ) + float ( user [ 5 ] ) ) * ( 1 + ( float ( user [ 2 ] ) * 0.01 ) ) ) - float ( user [ 3 ] )
# Can't exceed 1 quadrillion
# Check BC as well so they can't put everything into BC to cheat the system
if float ( user [ 1 ] ) + float ( user [ 3 ] ) + float ( user [ 5 ] ) + profit + ( float ( user [ 8 ] ) * bitcoinPrice ) > Numbers . q . value :
# In case adding profit would exceed 1q, only add the difference
profit = Numbers . q . value - float ( user [ 1 ] ) - float ( user [ 3 ] ) - float ( user [ 5 ] ) - ( float ( user [ 8 ] ) * bitcoinPrice )
# Don't reduce the current profit if Dinks were gained some other way (rob, bc, ...)
if profit > 0 :
currency . update ( user [ 0 ] , " profit " , float ( user [ 5 ] ) + profit )
await self . client . get_user ( int ( user [ 0 ] ) ) . send ( " Je hebt de invest-limiet van 1Q Didier Dinks bereikt. \n Indien je nog meer Didier Dinks wil sparen, kan je 1q Didier Dinks omruilen voor een Platinum Dink in de shop. " )
else :
currency . update ( user [ 0 ] , " profit " , float ( user [ 5 ] ) + profit )
lastTasks [ " interest " ] = int ( round ( time . time ( ) ) )
with open ( " files/lastTasks.json " , " w " ) as fp :
json . dump ( lastTasks , fp )
@bankInterest.before_loop
async def beforeBankInterest ( self ) :
await self . client . wait_until_ready ( )
@tasks.loop ( hours = 1.0 )
async def resetLost ( self ) :
2020-10-23 20:56:20 +02:00
"""
Task that resets Lost Today
"""
2020-10-13 21:02:40 +02:00
# Don't do it multiple times a day if bot dc's, ...
with open ( " files/lastTasks.json " , " r " ) as fp :
lastTasks = json . load ( fp )
if int ( self . getCurrentHour ( ) ) == 0 and int ( time . time ( ) ) - int ( lastTasks [ " lost " ] ) > 10000 :
with open ( " files/lost.json " , " r " ) as fp :
fc = json . load ( fp )
fc [ " today " ] = 0
with open ( " files/lost.json " , " w " ) as fp :
json . dump ( fc , fp )
lastTasks [ " lost " ] = round ( time . time ( ) )
with open ( " files/lastTasks.json " , " w " ) as fp :
json . dump ( lastTasks , fp )
@resetLost.before_loop
async def beforeResetLost ( self ) :
await self . client . wait_until_ready ( )
@tasks.loop ( hours = 6.0 )
async def resetPoke ( self ) :
2020-10-23 20:56:20 +02:00
"""
Task that resets Poke
"""
2020-10-13 21:02:40 +02:00
if int ( time . time ( ) ) - int ( poke . get ( ) [ 1 ] ) > 259200 :
await self . client . get_guild ( int ( self . client . constants . CallOfCode ) ) \
. get_channel ( int ( self . client . constants . DidierPosting ) ) \
. send ( " Poke is gereset door inactiviteit. <@! {} > is hem! " . format ( int ( poke . reset ( ) ) ) )
@resetPoke.before_loop
async def beforeResetPoke ( self ) :
await self . client . wait_until_ready ( )
@tasks.loop ( hours = 1.0 )
async def resetPrison ( self ) :
2020-10-23 20:56:20 +02:00
"""
Task that lowers prison time daily
"""
2020-10-13 21:02:40 +02:00
# Don't do it multiple times a day if bot dc's, ...
with open ( " files/lastTasks.json " , " r " ) as fp :
lastTasks = json . load ( fp )
if int ( self . getCurrentHour ( ) ) == 0 and int ( time . time ( ) ) - int ( lastTasks [ " prison " ] ) > 10000 :
prison . dailyLowers ( )
with open ( " files/lastTasks.json " , " w " ) as fp :
lastTasks [ " prison " ] = round ( time . time ( ) )
json . dump ( lastTasks , fp )
@resetPrison.before_loop
async def beforeResetPrison ( self ) :
await self . client . wait_until_ready ( )
@tasks.loop ( hours = 1.0 )
async def checkBirthdays ( self ) :
2020-10-23 20:56:20 +02:00
"""
Task that wishes people a happy birthday
"""
2020-10-13 21:02:40 +02:00
# Don't do it multiple times a day if bot dc's, ...
with open ( " files/lastTasks.json " , " r " ) as fp :
lastTasks = json . load ( fp )
if int ( self . getCurrentHour ( ) ) == 6 and int ( time . time ( ) ) - int ( lastTasks [ " birthdays " ] ) > 10000 :
dt = timeFormatters . dateTimeNow ( )
res = birthdays . get_users_on_date ( dt . day , dt . month )
COC = self . client . get_guild ( int ( constants . CallOfCode ) )
people = [ COC . get_member ( int ( user [ 0 ] ) ) for user in res ]
general = COC . get_channel ( int ( constants . CoCGeneral ) )
lastTasks [ " birthdays " ] = round ( time . time ( ) )
with open ( " files/lastTasks.json " , " w " ) as fp :
json . dump ( lastTasks , fp )
if not people :
return
if len ( people ) == 1 :
return await general . send ( " Gelukkige verjaardag {} ! " . format ( people [ 0 ] . mention ) )
return await general . send ( " Gelukkige verjaardag {} en {} ! " . format (
" , " . join ( user . mention for user in people [ : - 1 ] ) ,
people [ - 1 ] . mention
) )
@checkBirthdays.before_loop
async def beforecheckBirthdays ( self ) :
await self . client . wait_until_ready ( )
@tasks.loop ( hours = 1.0 )
async def updateMessageCounts ( self ) :
2020-10-23 20:56:20 +02:00
"""
Task that updates the activity counter for channels
"""
2020-10-13 21:02:40 +02:00
# Don't do it multiple times a day if bot dc's, ...
with open ( " files/lastTasks.json " , " r " ) as fp :
lastTasks = json . load ( fp )
if int ( self . getCurrentHour ( ) ) == 0 and int ( time . time ( ) ) - int ( lastTasks [ " channels " ] ) > 10000 :
channels = stats . channel_activity ( )
for channel in channels :
stats . lower_channel ( int ( channel [ 0 ] ) , 0.95 * float ( channel [ 1 ] ) )
with open ( " files/lastTasks.json " , " w " ) as fp :
lastTasks [ " channels " ] = round ( time . time ( ) )
json . dump ( lastTasks , fp )
@updateMessageCounts.before_loop
async def beforeupdateMessageCounts ( self ) :
await self . client . wait_until_ready ( )
2020-10-23 20:56:20 +02:00
@tasks.loop ( hours = 1.0 )
async def sendReminders ( self ) :
"""
Task that sends people daily reminders
"""
# Don't do it multiple times a day if bot dc's, ...
with open ( " files/lastTasks.json " , " r " ) as fp :
lastTasks = json . load ( fp )
2020-10-24 00:24:33 +02:00
if int ( self . getCurrentHour ( ) ) == 7 and int ( time . time ( ) ) - int ( lastTasks [ " remind " ] ) > 10000 :
2020-10-23 21:34:10 +02:00
reminders = Reminders ( )
2020-10-24 00:23:28 +02:00
weekday = self . getCurrentWeekday ( )
2020-10-23 21:34:10 +02:00
for category in reminders . categories :
2020-12-11 21:47:14 +01:00
# Check if this reminder is temporarily disabled
if category [ " disabled " ] :
continue
2020-10-24 00:23:28 +02:00
# Checks if this reminder can be sent on weekdays
if ( not category [ " weekends " ] ) and weekday > 4 :
continue
2021-08-10 12:02:39 +02:00
# Create embed once because this can be heavy
if " embed " in category :
embed = category [ " embed " ] ( )
else :
embed = None
2020-10-23 21:34:10 +02:00
for user in category [ " users " ] :
2020-10-24 00:23:28 +02:00
userInstance = self . client . get_user ( user )
2020-10-23 21:34:10 +02:00
# User can't be fetched for whatever reason, ignore instead of crashing
if userInstance is None :
continue
# Check if a special embed has to be attached for this reminder
if " embed " not in category :
await userInstance . send ( random . choice ( category [ " messages " ] ) )
else :
2021-08-10 12:02:39 +02:00
await userInstance . send ( random . choice ( category [ " messages " ] ) , embed = embed )
2020-10-23 20:56:20 +02:00
2020-10-23 22:12:05 +02:00
with open ( " files/lastTasks.json " , " w " ) as fp :
lastTasks [ " remind " ] = round ( time . time ( ) )
json . dump ( lastTasks , fp )
2020-10-23 20:56:20 +02:00
2020-10-23 22:15:22 +02:00
@sendReminders.before_loop
async def beforeSendReminders ( self ) :
await self . client . wait_until_ready ( )
2021-01-24 22:31:09 +01:00
@tasks.loop ( hours = 2.0 )
2021-01-24 22:00:24 +01:00
async def updateMatchweek ( self ) :
2021-01-24 22:31:09 +01:00
"""
Task that checks the current JPL matchweek & changes the dict value
"""
2021-08-08 23:24:16 +02:00
# Don't run this when testing
if self . client . user . id != int ( constants . didierId ) :
return
2021-01-24 22:31:09 +01:00
matchweek = getMatchweek ( )
if matchweek is None :
return
# Change the setting in the config
config ( " jpl_day " , int ( matchweek ) )
@updateMatchweek.before_loop
async def beforeUpdateMatchweek ( self ) :
await self . client . wait_until_ready ( )
2021-01-24 22:00:24 +01:00
2021-03-12 10:05:19 +01:00
@tasks.loop ( minutes = 10.0 )
2021-03-03 18:04:31 +01:00
async def uforaAnnouncements ( self ) :
"""
Task that checks for new Ufora announcements every few minutes
"""
2021-04-05 15:27:58 +02:00
# Don't run this when testing
2021-06-19 20:11:55 +02:00
if self . client . user . id != int ( constants . didierId ) :
2021-04-05 15:27:58 +02:00
return
2021-03-03 18:04:31 +01:00
# Get new notifications
announcements = ufora_notifications . run ( )
if announcements :
2021-03-03 18:40:31 +01:00
announcements_channel = self . client . get_channel ( 816724500136591380 )
2021-03-03 18:04:31 +01:00
for an in announcements :
await announcements_channel . send ( embed = an . to_embed ( ) )
@uforaAnnouncements.before_loop
async def beforeUforaAnnouncements ( self ) :
await self . client . wait_until_ready ( )
2020-10-13 21:02:40 +02:00
def getCurrentHour ( self ) :
return timeFormatters . dateTimeNow ( ) . hour
2020-10-24 00:11:57 +02:00
def getCurrentWeekday ( self ) :
return timeFormatters . dateTimeNow ( ) . weekday ( )
2020-10-13 21:02:40 +02:00
def getCurrentBitcoinPrice ( self ) :
result = requests . get ( " https://api.coindesk.com/v1/bpi/currentprice.json " ) . json ( )
currentPrice = result [ " bpi " ] [ " EUR " ] [ " rate_float " ]
return float ( currentPrice )
def setup ( client ) :
client . add_cog ( Tasks ( client ) )