update comments, formatting, and add the ability to chose not to cache when a message will never be the same

This commit is contained in:
aliasless 2020-04-20 00:08:55 +00:00
parent 5d6caf7a0a
commit ef6205a707

View File

@ -33,27 +33,29 @@ s = AsyncIOScheduler(event_loop = client.loop)
ticker_queue = asyncio.Queue() ticker_queue = asyncio.Queue()
# returns a file path from the cache # returns a file path from the cache
def to_tts(message, lang = 'ja'): def to_tts(message, lang = 'ja', cache = True):
# the file we save to is based on the hash of the message # the file we save to is based on the hash of the message
message_hash = hashlib.md5(message.encode('utf-8')).hexdigest() file_name = "dqn.mp3"
file_name = ROOT + "/cache/" + message_hash + ".mp3" if cache:
message_hash = hashlib.md5(message.encode('utf-8')).hexdigest()
file_name = ROOT + "/cache/" + message_hash + ".mp3"
print(message) print(message)
# if the hash matches and existing file, well... # the hash lets us cache
if not os.path.isfile(file_name): if not os.path.isfile(file_name) or not cache:
print("file does not exist, cacheing") print("file does not exist, cacheing")
tts = gTTS(message, lang = lang) tts = gTTS(message, lang = lang)
tts.save(file_name) tts.save(file_name)
return file_name return file_name
# coroutine responsible for handling the queue as events come in # announcer loop
async def ticker_update(): async def ticker_update():
while True: while True:
await client.wait_until_ready() await client.wait_until_ready()
# blocks until there is a message in the queue # blocks until there is an event waiting in the queue
announce_file, target_voice_channel, after = await ticker_queue.get() announce_file, target_voice_channel, after = await ticker_queue.get()
print("ticker event has been removed from queue") print("ticker event has been removed from queue")
@ -62,22 +64,17 @@ async def ticker_update():
# 2. we are connected to the wrong channel. we should MOVE to the target channel # 2. we are connected to the wrong channel. we should MOVE to the target channel
# 3. we are already in the correct channel. we should do nothing # 3. we are already in the correct channel. we should do nothing
guild = target_voice_channel.guild # the guild this ticker event is for guild = target_voice_channel.guild
voice_client = guild.voice_client # the voice client for this guild, may be None voice_client = guild.voice_client # may be none
# there is no connection, nor an existing voice client # connect vc
if not voice_client: if not (voice_client and voice_client.is_connected()):
voice_client = await target_voice_channel.connect() voice_client = await target_voice_channel.connect()
# there is a voice client, but there is no connection # switch channels
if not voice_client.is_connected():
voice_client = await target_voice_channel.connect()
# we are not already in the correct channel
if not target_voice_channel.id == voice_client.channel.id: if not target_voice_channel.id == voice_client.channel.id:
await voice_client.move_to(target_voice_channel) await voice_client.move_to(target_voice_channel)
# load and play supplied message
source = discord.FFmpegOpusAudio(announce_file) source = discord.FFmpegOpusAudio(announce_file)
voice_client.play(source) voice_client.play(source)
@ -88,7 +85,7 @@ async def ticker_update():
if after: if after:
await after(target_voice_channel) await after(target_voice_channel)
# once all ticker events have completed, dc # the queue being empty indicates all events completed
if ticker_queue.empty(): if ticker_queue.empty():
await voice_client.disconnect() await voice_client.disconnect()
@ -153,13 +150,14 @@ def bussing():
BUS_START = 1587179700 BUS_START = 1587179700
since_start = time.time() - BUS_START since_start = time.time() - BUS_START
hours_since_start = since_start / 60 / 60 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') file_name = to_tts("GIVE IT UP FOR " + str(int(hours_since_start)) + " HOURS SINCE DEPARTURE!! WOO!!", 'en', False)
return file_name return file_name
# ban league # ban league
async def league_ghetto(): async def league_ghetto():
await client.wait_until_ready() await client.wait_until_ready()
# short circuiting ands are okay, but this is still hell.
leaguers = filter(lambda member: member.activity and member.activity.type == discord.ActivityType.playing leaguers = filter(lambda member: member.activity and member.activity.type == discord.ActivityType.playing
and member.activity.name == 'League of Legends' and member.voice and member.voice.channel and member.activity.name == 'League of Legends' and member.voice and member.voice.channel
and member.voice.channel.id != GHETTO, client.get_guild(SEGUIS).members) and member.voice.channel.id != GHETTO, client.get_guild(SEGUIS).members)