Skip to content

Commit

Permalink
[Discord] Handle no longer valid Twitter handles
Browse files Browse the repository at this point in the history
[Config] Add Twitter.test_handle
[Discord] Add Bot.twitter_test_handle property
  • Loading branch information
Harmon758 committed Nov 16, 2023
1 parent 291c258 commit 137634d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 56 deletions.
4 changes: 4 additions & 0 deletions Discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ def mock_location(self):
def stream_url(self):
return self.config["Discord"]["stream_url"]

@property
def twitter_test_handle(self):
return self.config["Twitter"]["test_handle"]

async def setup_hook(self):
self.loop.create_task(
self.initialize_constant_objects(),
Expand Down
104 changes: 48 additions & 56 deletions Discord/cogs/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,70 +427,62 @@ async def check_tweets(self):
f"{nitter_instance_url}/{handle}/rss"
) as resp:
# TODO: Use structural pattern matching with Python 3.10
if resp.status in (400, 404):
'''
channel_records = await self.bot.db.fetch(
"""
SELECT channel_id
FROM twitter.handles
WHERE handle = $1
""",
handle
)
for record in channel_records:
text_channel = self.bot.get_channel(
record["channel_id"]
)
if not text_channel:
# TODO: Handle channel no longer accessible
continue
try:
await text_channel.send(
embed = discord.Embed(
color = self.bot.bot_color,
description = (
f"`{handle}` doesn't appear to be "
"a valid Twitter user at "
f"https://twitter.com/{handle} "
"anymore, so this channel is no "
"longer following that handle\n"
"(Their account may have been "
"suspended or restricted / made "
"unavailable, their Tweets may be "
"private/protected now, or they "
"may have changed their "
"handle/username or deactived "
"their account)"
)
)
)
except discord.Forbidden:
# TODO: Handle unable to send messages in text channel
self.bot.print(
"Twitter Task: Missing permissions to send message "
f"in #{text_channel.name} in {text_channel.guild.name}"
if resp.status == 404:
async with self.bot.aiohttp_session.get(
f"{nitter_instance_url}/{self.bot.twitter_test_handle}/rss"
) as resp:
if resp.status == 200:
deleted = await self.bot.db.fetch(
"""
DELETE FROM twitter.handles
WHERE handle = $1
RETURNING *
""",
handle
)
for record in deleted:
notice = (
f"`{record['handle']}` doesn't appear "
"to be a valid Twitter user at "
f"https://twitter.com/{record['handle']} "
"anymore, so "
f"<#{record['channel_id']}> is no "
"longer following that handle\n"
"(Their account may have been "
"suspended or restricted / made "
"unavailable, their Tweets may be "
"private/protected now, or they may "
"have changed their handle/username "
"or deactived their account)"
)
self.bot.print(notice)
try:
channel = (
self.bot.get_channel(
record["channel_id"]
) or (
await self.bot.fetch_channel(
record["channel_id"]
)
)
)
await channel.send(notice)
except (
discord.Forbidden, discord.NotFound
):
await self.bot.last_resort_notices_channel.send(
notice
)
continue
await self.bot.db.execute(
"""
DELETE FROM twitter.handles
WHERE channel_id = $1 AND handle = $2
""",
record["channel_id"], handle
)
'''
self.bot.print(
f"Twitter feed returned {resp.status} "
f"status with handle, {handle}"
)
continue

elif resp.status in (
500, 502, 503, 504, 520, 521, 522, 524
):
# TODO: Log
await asyncio.sleep(1)
continue
elif resp.status != 200:

if resp.status != 200:
self.bot.print(
f"Twitter feed returned {resp.status} "
f"status with handle, {handle}, and "
Expand Down
3 changes: 3 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ game_statuses = [
last_resort_notices_channel_id = 955950052747137104
stream_url = "https://www.twitch.tv/harmonbot"

[Twitter]
test_handle = "Harmon758Public"

[mock]
ip = "0.0.0.0"
location = "Geneva, Switzerland"

0 comments on commit 137634d

Please sign in to comment.