Migrate twitter

This commit is contained in:
Zeva Rose 2022-02-19 20:10:05 -07:00
parent fd97e82ed4
commit 0175fce442
4 changed files with 48 additions and 27 deletions

View file

@ -47,8 +47,8 @@ class Jarvis(Snake):
self, ctx: Context, error: Exception, *args: list, **kwargs: dict self, ctx: Context, error: Exception, *args: list, **kwargs: dict
) -> None: ) -> None:
"""Lepton on_command_error override.""" """Lepton on_command_error override."""
guild = await jarvis.get_guild(DEFAULT_GUILD) guild = await jarvis.fetch_guild(DEFAULT_GUILD)
channel = guild.get_channel(DEFAULT_ERROR_CHANNEL) channel = await guild.fetch_channel(DEFAULT_ERROR_CHANNEL)
error_time = datetime.utcnow().strftime("%d-%m-%Y %H:%M-%S.%f UTC") error_time = datetime.utcnow().strftime("%d-%m-%Y %H:%M-%S.%f UTC")
timestamp = int(datetime.now().timestamp()) timestamp = int(datetime.now().timestamp())
timestamp = f"<t:{timestamp}:T>" timestamp = f"<t:{timestamp}:T>"
@ -104,7 +104,7 @@ async def on_ready() -> None:
def run() -> None: def run() -> None:
"""Run J.A.R.V.I.S.""" """Run J.A.R.V.I.S."""
connect(**jconfig.mongo["connect"], testing=jconfig.mongo["database"] == "jarvis") connect(**jconfig.mongo["connect"], testing=jconfig.mongo["database"] != "jarvis")
jconfig.get_db_config() jconfig.get_db_config()
for extension in utils.get_extensions(): for extension in utils.get_extensions():

View file

@ -2,8 +2,8 @@
import asyncio import asyncio
import tweepy import tweepy
from bson import ObjectId
from dis_snek import InteractionContext, Permissions, Scale, Snake from dis_snek import InteractionContext, Permissions, Scale, Snake
from dis_snek.client.utils.misc_utils import get
from dis_snek.models.discord.channel import GuildText from dis_snek.models.discord.channel import GuildText
from dis_snek.models.discord.components import ActionRow, Select, SelectOption from dis_snek.models.discord.components import ActionRow, Select, SelectOption
from dis_snek.models.snek.application_commands import ( from dis_snek.models.snek.application_commands import (
@ -14,7 +14,7 @@ from dis_snek.models.snek.application_commands import (
) )
from dis_snek.models.snek.command import check from dis_snek.models.snek.command import check
from jarvis_core.db import q from jarvis_core.db import q
from jarvis_core.db.models import Twitter from jarvis_core.db.models import TwitterAccount, TwitterFollow
from jarvis import jconfig from jarvis import jconfig
from jarvis.utils.permissions import admin_or_permissions from jarvis.utils.permissions import admin_or_permissions
@ -68,7 +68,7 @@ class TwitterCog(Scale):
return return
try: try:
account = (await asyncio.to_thread(self.api.get_user(screen_name=handle)))[0] account = await asyncio.to_thread(self.api.get_user, screen_name=handle)
latest_tweet = (await asyncio.to_thread(self.api.user_timeline, screen_name=handle))[0] latest_tweet = (await asyncio.to_thread(self.api.user_timeline, screen_name=handle))[0]
except Exception: except Exception:
await ctx.send( await ctx.send(
@ -76,42 +76,54 @@ class TwitterCog(Scale):
) )
return return
count = len([i async for i in Twitter.find(guild=ctx.guild.id)]) count = len([i async for i in TwitterFollow.find(q(guild=ctx.guild.id))])
if count >= 12: if count >= 12:
await ctx.send("Cannot follow more than 12 Twitter accounts", ephemeral=True) await ctx.send("Cannot follow more than 12 Twitter accounts", ephemeral=True)
return return
exists = Twitter.find_one(q(twitter_id=account.id, guild=ctx.guild.id)) exists = await TwitterFollow.find_one(q(twitter_id=account.id, guild=ctx.guild.id))
if exists: if exists:
await ctx.send("Twitter account already being followed in this guild", ephemeral=True) await ctx.send("Twitter account already being followed in this guild", ephemeral=True)
return return
t = Twitter( ta = await TwitterAccount.find_one(q(twitter_id=account.id))
handle=account.screen_name, if not ta:
ta = TwitterAccount(
handle=account.screen_name,
twitter_id=account.id,
last_tweet=latest_tweet.id,
)
await ta.commit()
tf = TwitterFollow(
twitter_id=account.id, twitter_id=account.id,
guild=ctx.guild.id, guild=ctx.guild.id,
channel=channel.id, channel=channel.id,
admin=ctx.author.id, admin=ctx.author.id,
last_tweet=latest_tweet.id,
retweets=retweets, retweets=retweets,
) )
await t.commit() await tf.commit()
await ctx.send(f"Now following `@{handle}` in {channel.mention}") await ctx.send(f"Now following `@{handle}` in {channel.mention}")
@slash_command(name="twitter", sub_cmd_name="unfollow", description="Unfollow Twitter accounts") @slash_command(name="twitter", sub_cmd_name="unfollow", description="Unfollow Twitter accounts")
@check(admin_or_permissions(Permissions.MANAGE_GUILD)) @check(admin_or_permissions(Permissions.MANAGE_GUILD))
async def _twitter_unfollow(self, ctx: InteractionContext) -> None: async def _twitter_unfollow(self, ctx: InteractionContext) -> None:
twitters = Twitter.objects(guild=ctx.guild.id) t = TwitterFollow.find(q(guild=ctx.guild.id))
twitters = []
async for twitter in t:
twitters.append(twitter)
if not twitters: if not twitters:
await ctx.send("You need to follow a Twitter account first", ephemeral=True) await ctx.send("You need to follow a Twitter account first", ephemeral=True)
return return
options = [] options = []
handlemap = {str(x.id): x.handle for x in twitters} handlemap = {}
for twitter in twitters: for twitter in twitters:
option = SelectOption(label=twitter.handle, value=str(twitter.id)) account = await TwitterAccount.find_one(q(twitter_id=twitter.twitter_id))
handlemap[str(twitter.twitter_id)] = account.handle
option = SelectOption(label=account.handle, value=str(twitter.twitter_id))
options.append(option) options.append(option)
select = Select( select = Select(
@ -119,7 +131,7 @@ class TwitterCog(Scale):
) )
components = [ActionRow(select)] components = [ActionRow(select)]
block = "\n".join(x.handle for x in twitters) block = "\n".join(x for x in handlemap.values())
message = await ctx.send( message = await ctx.send(
content=f"You are following the following accounts:\n```\n{block}\n```\n\n" content=f"You are following the following accounts:\n```\n{block}\n```\n\n"
"Please choose accounts to unfollow", "Please choose accounts to unfollow",
@ -133,7 +145,8 @@ class TwitterCog(Scale):
timeout=60 * 5, timeout=60 * 5,
) )
for to_delete in context.context.values: for to_delete in context.context.values:
_ = Twitter.objects(guild=ctx.guild.id, id=ObjectId(to_delete)).delete() follow = get(twitters, guild=ctx.guild.id, twitter_id=int(to_delete))
await follow.delete()
for row in components: for row in components:
for component in row.components: for component in row.components:
component.disabled = True component.disabled = True
@ -164,14 +177,20 @@ class TwitterCog(Scale):
@check(admin_or_permissions(Permissions.MANAGE_GUILD)) @check(admin_or_permissions(Permissions.MANAGE_GUILD))
async def _twitter_modify(self, ctx: InteractionContext, retweets: str) -> None: async def _twitter_modify(self, ctx: InteractionContext, retweets: str) -> None:
retweets = retweets == "Yes" retweets = retweets == "Yes"
twitters = Twitter.objects(guild=ctx.guild.id) t = TwitterFollow.find(q(guild=ctx.guild.id))
twitters = []
async for twitter in t:
twitters.append(twitter)
if not twitters: if not twitters:
await ctx.send("You need to follow a Twitter account first", ephemeral=True) await ctx.send("You need to follow a Twitter account first", ephemeral=True)
return return
options = [] options = []
handlemap = {}
for twitter in twitters: for twitter in twitters:
option = SelectOption(label=twitter.handle, value=str(twitter.id)) account = await TwitterAccount.find_one(q(twitter_id=twitter.id))
handlemap[str(twitter.twitter_id)] = account.handle
option = SelectOption(label=account.handle, value=str(twitter.twitter_id))
options.append(option) options.append(option)
select = Select( select = Select(
@ -179,7 +198,7 @@ class TwitterCog(Scale):
) )
components = [ActionRow(select)] components = [ActionRow(select)]
block = "\n".join(x.handle for x in twitters) block = "\n".join(x for x in handlemap.values())
message = await ctx.send( message = await ctx.send(
content=f"You are following the following accounts:\n```\n{block}\n```\n\n" content=f"You are following the following accounts:\n```\n{block}\n```\n\n"
f"Please choose which accounts to {'un' if not retweets else ''}follow retweets from", f"Please choose which accounts to {'un' if not retweets else ''}follow retweets from",
@ -193,11 +212,13 @@ class TwitterCog(Scale):
timeout=60 * 5, timeout=60 * 5,
) )
handlemap = {str(x.id): x.handle for x in twitters} handlemap = {}
for to_update in context.context.values: for to_update in context.context.values:
t = await Twitter.find_one(q(guild=ctx.guild.id, id=ObjectId(to_update)))() account = await TwitterAccount.find_one(q(twitter_id=int(to_update)))
t.retweets = retweets handlemap[str(twitter.twitter_id)] = account.handle
t.save() t = get(twitters, guild=ctx.guild.id, twitter_id=int(to_update))
t.update(q(retweets=True))
await t.commit()
for row in components: for row in components:
for component in row.components: for component in row.components:

4
poetry.lock generated
View file

@ -214,7 +214,7 @@ plugins = ["setuptools"]
[[package]] [[package]]
name = "jarvis-core" name = "jarvis-core"
version = "0.2.1" version = "0.4.1"
description = "" description = ""
category = "main" category = "main"
optional = false optional = false
@ -232,7 +232,7 @@ umongo = "^3.1.0"
type = "git" type = "git"
url = "https://git.zevaryx.com/stark-industries/jarvis/jarvis-core.git" url = "https://git.zevaryx.com/stark-industries/jarvis/jarvis-core.git"
reference = "main" reference = "main"
resolved_reference = "0e627eae725abb1e6f3766c5dc94bd80d0ac6702" resolved_reference = "20f67444579d36d508def2b47ea826af9bbdd2d7"
[[package]] [[package]]
name = "jedi" name = "jedi"

View file

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "jarvis" name = "jarvis"
version = "2.0.0a0" version = "2.0.0a1"
description = "J.A.R.V.I.S. admin bot" description = "J.A.R.V.I.S. admin bot"
authors = ["Zevaryx <zevaryx@gmail.com>"] authors = ["Zevaryx <zevaryx@gmail.com>"]