2018-06-13 00:16:19 +00:00
|
|
|
# WorldTime Discord client
|
|
|
|
|
|
|
|
import discord
|
|
|
|
import asyncio
|
2018-08-02 00:40:41 +00:00
|
|
|
import aiohttp
|
2018-06-13 00:16:19 +00:00
|
|
|
|
2018-08-24 20:27:56 +00:00
|
|
|
from common import logPrint
|
2018-08-02 00:40:41 +00:00
|
|
|
import settings
|
2018-06-13 00:16:19 +00:00
|
|
|
from userdb import UserDatabase
|
2018-08-24 20:27:56 +00:00
|
|
|
from commands import WtCommands
|
2018-06-13 00:16:19 +00:00
|
|
|
|
2020-05-02 18:43:25 +00:00
|
|
|
class WorldTime(discord.AutoShardedClient):
|
2018-06-13 00:16:19 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2018-11-02 02:05:21 +00:00
|
|
|
self.userdb = UserDatabase(settings.PgConnectionString)
|
2018-08-24 20:27:56 +00:00
|
|
|
self.commands = WtCommands(self.userdb, self)
|
2018-08-02 00:40:41 +00:00
|
|
|
self.bg_task = self.loop.create_task(self.periodic_report())
|
2018-06-13 00:16:19 +00:00
|
|
|
|
|
|
|
async def on_ready(self):
|
2018-08-24 20:27:56 +00:00
|
|
|
logPrint('Status', 'Connected as {0} ({1})'.format(self.user.name, self.user.id))
|
2019-12-26 23:39:25 +00:00
|
|
|
await self.change_presence(activity=discord.Game("tz.help"))
|
2018-06-13 00:16:19 +00:00
|
|
|
|
|
|
|
async def on_message(self, message):
|
|
|
|
# ignore bots (should therefore also ignore self)
|
|
|
|
if message.author.bot: return
|
2018-08-24 20:27:56 +00:00
|
|
|
|
2018-06-13 00:16:19 +00:00
|
|
|
if isinstance(message.channel, discord.DMChannel):
|
2018-08-24 20:27:56 +00:00
|
|
|
await self.respond_dm(message)
|
2018-06-13 00:16:19 +00:00
|
|
|
return
|
2018-08-24 20:27:56 +00:00
|
|
|
|
|
|
|
# Regular message
|
|
|
|
self.userdb.update_activity(message.guild.id, message.author.id)
|
|
|
|
cmdBase = message.content.split(' ', 1)[0].lower()
|
|
|
|
if cmdBase.startswith('tz.'): # wishlist: per-guild customizable prefix
|
|
|
|
cmdBase = cmdBase[3:]
|
|
|
|
await self.commands.dispatch(cmdBase, message)
|
|
|
|
|
|
|
|
async def respond_dm(self, message):
|
|
|
|
logPrint('Incoming DM', '{0}: {1}'.format(message.author, message.content.replace('\n', '\\n')))
|
2020-05-02 18:43:25 +00:00
|
|
|
await message.channel.send('''I don't work over DM. Refer to the `tz.help` command when in a server.''')
|
2018-08-02 00:40:41 +00:00
|
|
|
|
|
|
|
# ----------------
|
|
|
|
|
|
|
|
async def periodic_report(self):
|
|
|
|
'''
|
|
|
|
Provides a periodic update in console of how many guilds we're on.
|
2019-12-26 23:39:25 +00:00
|
|
|
Reports guild count to Discord Bots if the appropriate token has been defined.
|
2018-08-02 00:40:41 +00:00
|
|
|
'''
|
|
|
|
try:
|
|
|
|
authtoken = settings.DBotsApiKey
|
|
|
|
except AttributeError:
|
|
|
|
authtoken = ''
|
|
|
|
|
|
|
|
await self.wait_until_ready()
|
|
|
|
while not self.is_closed():
|
|
|
|
guildcount = len(self.guilds)
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
if authtoken != '':
|
2018-12-08 05:17:44 +00:00
|
|
|
rurl = "https://discord.bots.gg/api/v1/bots/{}/stats".format(self.user.id)
|
|
|
|
rdata = { "guildCount": guildcount }
|
2018-08-02 00:40:41 +00:00
|
|
|
rhead = { "Content-Type": "application/json", "Authorization": authtoken }
|
|
|
|
try:
|
|
|
|
await session.post(rurl, json=rdata, headers=rhead)
|
2018-08-24 20:27:56 +00:00
|
|
|
logPrint("Report", "Reported count to Discord Bots.")
|
2018-08-02 00:40:41 +00:00
|
|
|
except aiohttp.ClientError as e:
|
2018-08-24 20:27:56 +00:00
|
|
|
logPrint("Report", "Discord Bots API report failed: {}".format(e))
|
2018-11-02 04:26:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
logPrint("Report", "Unknown error on Discord Bots API report.")
|
2019-12-26 23:39:25 +00:00
|
|
|
logPrint("Report", "Currently in {0} guild(s).".format(guildcount))
|
2018-12-08 05:17:44 +00:00
|
|
|
await asyncio.sleep(21600) # Repeat once every six hours
|