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:
parent
5d6caf7a0a
commit
ef6205a707
36
ticker.py
36
ticker.py
|
@ -33,27 +33,29 @@ s = AsyncIOScheduler(event_loop = client.loop)
|
|||
ticker_queue = asyncio.Queue()
|
||||
|
||||
# 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
|
||||
message_hash = hashlib.md5(message.encode('utf-8')).hexdigest()
|
||||
file_name = ROOT + "/cache/" + message_hash + ".mp3"
|
||||
file_name = "dqn.mp3"
|
||||
if cache:
|
||||
message_hash = hashlib.md5(message.encode('utf-8')).hexdigest()
|
||||
file_name = ROOT + "/cache/" + message_hash + ".mp3"
|
||||
|
||||
print(message)
|
||||
|
||||
# if the hash matches and existing file, well...
|
||||
if not os.path.isfile(file_name):
|
||||
# the hash lets us cache
|
||||
if not os.path.isfile(file_name) or not cache:
|
||||
print("file does not exist, cacheing")
|
||||
tts = gTTS(message, lang = lang)
|
||||
tts.save(file_name)
|
||||
|
||||
return file_name
|
||||
|
||||
# coroutine responsible for handling the queue as events come in
|
||||
# announcer loop
|
||||
async def ticker_update():
|
||||
while True:
|
||||
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()
|
||||
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
|
||||
# 3. we are already in the correct channel. we should do nothing
|
||||
|
||||
guild = target_voice_channel.guild # the guild this ticker event is for
|
||||
voice_client = guild.voice_client # the voice client for this guild, may be None
|
||||
guild = target_voice_channel.guild
|
||||
voice_client = guild.voice_client # may be none
|
||||
|
||||
# there is no connection, nor an existing voice client
|
||||
if not voice_client:
|
||||
# connect vc
|
||||
if not (voice_client and voice_client.is_connected()):
|
||||
voice_client = await target_voice_channel.connect()
|
||||
|
||||
# there is a voice client, but there is no connection
|
||||
if not voice_client.is_connected():
|
||||
voice_client = await target_voice_channel.connect()
|
||||
|
||||
# we are not already in the correct channel
|
||||
# switch channels
|
||||
if not target_voice_channel.id == voice_client.channel.id:
|
||||
await voice_client.move_to(target_voice_channel)
|
||||
|
||||
# load and play supplied message
|
||||
source = discord.FFmpegOpusAudio(announce_file)
|
||||
voice_client.play(source)
|
||||
|
||||
|
@ -88,7 +85,7 @@ async def ticker_update():
|
|||
if after:
|
||||
await after(target_voice_channel)
|
||||
|
||||
# once all ticker events have completed, dc
|
||||
# the queue being empty indicates all events completed
|
||||
if ticker_queue.empty():
|
||||
await voice_client.disconnect()
|
||||
|
||||
|
@ -153,13 +150,14 @@ 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')
|
||||
file_name = to_tts("GIVE IT UP FOR " + str(int(hours_since_start)) + " HOURS SINCE DEPARTURE!! WOO!!", 'en', False)
|
||||
return file_name
|
||||
|
||||
# ban league
|
||||
async def league_ghetto():
|
||||
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
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue