From e27b138170ec321695af7dc0525c1cc78a09dd9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Mon, 6 Aug 2018 18:24:39 +0200 Subject: [PATCH] Implement announcement tweeting --- clients/discord.py | 26 +++++++++++++++++++++++--- requirements.txt | 1 + settings.cfg.example | 7 +++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/clients/discord.py b/clients/discord.py index 67d3773..55b9242 100644 --- a/clients/discord.py +++ b/clients/discord.py @@ -3,10 +3,20 @@ import re import time import discord +import twitter from commands import CommandError +# @everyone Lilia is Live! Playing $game ! Come and say hi at $url ! +ANNOUNCEMENT_PATTERN = re.compile(r'''^ + @everyone\s+ + Lilia\s+is\s+Live!\s+ + Playing\s+(?P.+)\s+!\s+ + Come\s+and\s+say\s+hi\s+at\s+(?P.+)\s+! + $''', re.VERBOSE) + + def cooldown(retries, timeout, failure): def do_cooldown(function): def wrapper(self, server, user, *args, **kwargs): @@ -37,6 +47,11 @@ class DiscordClient(discord.Client): self.logger = logger self.commands = commands self.extra_commands = extra_commands + self.twitter_api = twitter.Api( + consumer_key=self.config['Twitter'].get('consumer_key'), + consumer_secret=self.config['Twitter'].get('consumer_secret'), + access_token_key=self.config['Twitter'].get('access_token_key'), + access_token_secret=self.config['Twitter'].get('access_token_secret')) self.supported_commands = [ (re.compile(r'^!lastquote$'), self._do_lastquote), (re.compile(r'^!findquote\s+(?P")?(?P.+)(?(q)")$'), self._do_findquote), @@ -74,9 +89,14 @@ class DiscordClient(discord.Client): async for msg in self.logs_from(channel, after=after): if msg.content == message.content: return - embed = next(iter(message.embeds or []), None) - await self.send_message(channel, message.content, tts=message.tts, embed=embed) - # TODO: twitter + try: + await self.send_message(channel, message.content) + except discord.errors.Forbidden: + pass + announcement = self.config['Twitter'].get('announcement_pattern') + m = ANNOUNCEMENT_PATTERN.match(message.content) + if m: + self.twitter_api.PostUpdate(announcement.format(**m.groupdict())) async def _cooldown_failure(self, server, user, message, **kwargs): await self.send_message(message.channel, diff --git a/requirements.txt b/requirements.txt index c9313fb..225de6b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ fuzzywuzzy[speedup] google-api-python-client irc python-dateutil +python-twitter requests diff --git a/settings.cfg.example b/settings.cfg.example index 6b39b55..576d0f8 100644 --- a/settings.cfg.example +++ b/settings.cfg.example @@ -11,6 +11,13 @@ announcer = 369539730989252609 # #cheesenouncements announcement_channels = 383778221004554249,352145992256061440 +[Twitter] +consumer_key = __TWITTER_CONSUMER_KEY__ +consumer_secret = __TWITTER_CONSUMER_SECRET__ +access_token_key = __TWITTER_ACCESS_TOKEN_KEY__ +access_token_secret = __TWITTER_ACCESS_TOKEN_SECRET__ +announcement_pattern = Live now! Playing {game} - Bring Cheese! {url} + [Twitch] cache_api_url = https://ladylilia.com/twitch-cache/api token = oauth:__TWITCH_OAUTH_TOKEN__