ticker/ticker.py
2020-04-11 03:08:52 +00:00

118 lines
4.3 KiB
Python

import discord
from datetime import datetime
from gtts import gTTS
from forex_python.converter import CurrencyRates
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
import asyncio
import hashlib
import os.path
ROOT = "/home/bot/ticker"
SEGUIS = 665399296522321942
MATT = 150791815723876352
client = discord.Client()
c = CurrencyRates()
ticker_queue = asyncio.Queue()
# returns a file path from the cache
def to_tts(message, lang = 'ja'):
# 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"
# if the hash matches and existing file, well...
if not os.path.isfile(file_name):
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
async def ticker_update():
while True:
await client.wait_until_ready()
# blocks until there is a message in the queue
announce_file, target_voice_channel = await ticker_queue.get()
print("ticker event has been removed from queue")
# there are three scenarios here
# 1. we are not connected to a voice channel. we should CONNECT to the target voice 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
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
# there is no connection, nor an existing voice client
if not voice_client:
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()
if not target_voice_channel.id == voice_client.channel.id:
await voice_client.move_to(target_voice_channel.id)
# load and play supplied message
source = discord.FFmpegOpusAudio(announce_file)
voice_client.play(source)
# block while the message is playing
while voice_client.is_playing():
await asyncio.sleep(0.1)
# once all ticker events have completed, dc
if ticker_queue.empty():
await voice_client.disconnect()
@client.event
async def on_ready():
print(client.user.name)
@client.event
async def on_voice_state_update(member, before, after):
# only run on moves
if before.channel and after.channel:
# moves to the afk channel
if after.channel.id == member.guild.afk_channel.id:
filename = to_tts(member.display_name + "はretard dimensionに移動しました")
await ticker_queue.put((filename, before.channel))
print("ticker event has been added to queue")
client.loop.create_task(ticker_update())
# either matts channel, or the most active channel
def get_best_channel(guild_id):
guild = client.get_guild(guild_id)
matt = discord.utils.find(lambda m: m.id == MATT, guild.members)
# why the fuck does python still not have safe navigation operators?
matt_voice = ((matt.voice.channel if matt.voice.channel else None) if matt.voice else None) if matt else None
top_channel = sorted(guild.voice_channels, key = lambda chan: len(chan.members), reverse = True)[0]
return matt_voice if matt_voice else top_channel
async def ticker():
print("ticker event has been added to queue")
file_name = to_tts("The current JPY exchange rate is " + str(c.get_rate('USD', 'JPY')) + " yen to a dollar")
await ticker_queue.put((file_name, get_best_channel(SEGUIS)))
async def flatten():
print("ticker event has been added to queue")
file_name = to_tts("Matt, it is time for your 4 PM dick flattening")
await ticker_queue.put((file_name, get_best_channel(SEGUIS)))
s = AsyncIOScheduler(event_loop = client.loop)
s.add_job(ticker, CronTrigger(hour = '0-19,21-23'))
s.add_job(flatten, CronTrigger(hour = '20'))
s.start()
with open(ROOT + "/priv/token") as file:
token = file.readline()
client.run(token)