decorators

This commit is contained in:
aliasless 2020-04-18 05:32:05 +00:00
parent 4c5e130058
commit 5d6caf7a0a
1 changed files with 29 additions and 20 deletions

View File

@ -6,6 +6,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from random import randint
import asyncio
import time
import hashlib
import os.path
@ -27,9 +28,9 @@ GHETTO = 699505527981473794
client = discord.Client()
c = CurrencyRates()
s = AsyncIOScheduler(event_loop = client.loop)
ticker_queue = asyncio.Queue()
tts_queue = asyncio.Queue()
# returns a file path from the cache
def to_tts(message, lang = 'ja'):
@ -124,24 +125,36 @@ def get_best_channel(guild_id):
top_channel = sorted(guild.voice_channels, key = lambda chan: len(chan.members), reverse = True)[0]
return matt_voice if matt_voice else top_channel
# helper
async def add_event(file_name, voice_channel, after = None):
print("ticker event has been added to queue")
await ticker_queue.put((file_name, voice_channel, after))
async def ticker():
voice_channel = get_best_channel(SEGUIS)
file_name = to_tts("The current JPY exchange rate is " + str(c.get_rate('USD', 'JPY')) + " yen to a dollar")
await add_event(file_name, voice_channel)
# decorator for simple scheduled announcements
def update(guild, trigger):
def decorator(func):
async def wrapper():
voice_channel = get_best_channel(guild)
await add_event(func(), voice_channel)
s.add_job(wrapper, trigger)
return wrapper
return decorator
async def flatten():
voice_channel = get_best_channel(SEGUIS)
file_name = to_tts("Matt, it is time for your 4 PM dick flattening")
await add_event(file_name, voice_channel)
@update(SEGUIS, CronTrigger(hour = '0-19,21-23'))
def ticker():
return to_tts("The current JPY exchange rate is " + str(c.get_rate('USD', 'JPY')) + " yen to a dollar")
async def cummies():
voice_channel = get_best_channel(GOONS)
file_name = to_tts("くむづりっぽぷっししりっと")
await add_event(file_name, voice_channel)
@update(SEGUIS, CronTrigger(hour = '20'))
def flatten():
return to_tts("Matt, it is time for your 4 PM dick flattening")
@update(GOONS, CronTrigger(hour = '*', minute = '15'))
def bussing():
BUS_START = 1587179700
since_start = time.time() - BUS_START
hours_since_start = since_start / 60 / 60
file_name = to_tts("GIVE IT UP FOR " + str(int(hours_since_start)) + " HOURS SINCE DEPARTURE!! WOO!!", lang = 'en')
return file_name
# ban league
async def league_ghetto():
@ -161,15 +174,11 @@ async def league_ghetto():
async def move(_):
await member.move_to(client.get_channel(GHETTO))
file_name = to_tts(member.display_name + ", you have been found in violation of the No League Act of 2020. Administrative action will now be taken against your account.", lang = 'de')
file_name = to_tts(member.display_name + ", you have been found in violation of the No League Act of 2020."
+ " Administrative action will now be taken against your account.", lang = 'de')
await add_event(file_name, client.get_channel(before_channel), move)
s = AsyncIOScheduler(event_loop = client.loop)
s.add_job(ticker, CronTrigger(hour = '0-19,21-23'))
s.add_job(flatten, CronTrigger(hour = '20'))
s.add_job(cummies, CronTrigger(hour = '*', minute = '30'))
s.add_job(league_ghetto, CronTrigger(minute = '*'))
s.add_job(league_ghetto, CronTrigger(minute = '*/5'))
s.start()
with open(ROOT + "/priv/token") as file: