Fix twitter, update twitter info to use ids instead of handles

This commit is contained in:
Zeva Rose 2022-02-04 12:03:07 -07:00
parent 1cf4108a44
commit 56bd5edc93
3 changed files with 22 additions and 15 deletions

View file

@ -67,7 +67,8 @@ class TwitterCog(Scale):
return
try:
latest_tweet = await asyncio.to_thread(self.api.user_timeline, screen_name=handle)[0]
account = (await asyncio.to_thread(self.api.get_user(screen_name=handle)))[0]
latest_tweet = (await asyncio.to_thread(self.api.user_timeline, screen_name=handle))[0]
except Exception:
await ctx.send(
"Unable to get user timeline. Are you sure the handle is correct?", ephemeral=True
@ -79,13 +80,14 @@ class TwitterCog(Scale):
await ctx.send("Cannot follow more than 12 Twitter accounts", ephemeral=True)
return
exists = Twitter.objects(handle=handle, guild=ctx.guild.id)
exists = Twitter.objects(twitter_id=account.id, guild=ctx.guild.id)
if exists:
await ctx.send("Twitter handle already being followed in this guild", ephemeral=True)
await ctx.send("Twitter account already being followed in this guild", ephemeral=True)
return
t = Twitter(
handle=handle,
handle=account.screen_name,
twitter_id=account.id,
guild=ctx.guild.id,
channel=channel.id,
admin=ctx.author.id,
@ -125,7 +127,7 @@ class TwitterCog(Scale):
try:
context = await self.bot.wait_for_component(
check=lambda x: ctx.author.id == x.author.id,
check=lambda x: ctx.author.id == x.context.author.id,
messages=message,
timeout=60 * 5,
)

View file

@ -222,6 +222,7 @@ class Twitter(Document):
"""Twitter Follow object."""
active = BooleanField(default=True)
twitter_id = IntField(required=True)
handle = StringField(required=True)
channel = SnowflakeField(required=True)
guild = SnowflakeField(required=True)
@ -229,6 +230,7 @@ class Twitter(Document):
retweets = BooleanField(default=True)
admin = SnowflakeField(required=True)
created_at = DateTimeField(default=datetime.utcnow)
last_sync = DateTimeField()
meta = {"db_alias": "main"}

View file

@ -1,6 +1,7 @@
"""J.A.R.V.I.S. twitter background task handler."""
import logging
from asyncio import to_thread
from datetime import datetime, timedelta
import tweepy
from dis_snek.ext.tasks.task import Task
@ -23,18 +24,20 @@ async def _tweets() -> None:
guild_cache = dict()
channel_cache = dict()
twitters = Twitter.objects(active=True)
handles = Twitter.objects.distinct("handle")
twitter_data = dict()
for handle in handles:
try:
data = __api.user_timeline(screen_name=handle)
twitter_data[handle] = data
except Exception as e:
logger.error(f"Error with fetching: {e}")
for twitter in twitters:
try:
tweets = list(filter(lambda x: x.id > twitter.last_tweet, twitter_data[twitter.handle]))
if tweets:
if not twitter.twitter_id or not twitter.last_sync:
user = __api.get_user(screen_name=twitter.handle)
twitter.twitter_id = user.id
twitter.handle = user.screen_name
twitter.last_sync = datetime.now()
if twitter.last_sync + timedelta(hours=1) <= datetime.now():
user = __api.get_user(id=twitter.twitter_id)
twitter.handle = user.screen_name
twitter.last_sync = datetime.now()
if tweets := __api.user_timeline(id=twitter.twitter_id):
guild_id = twitter.guild
channel_id = twitter.channel
tweets = sorted(tweets, key=lambda x: x.id)