2024-04-06 00:27:25 +01:00
|
|
|
#!/usr/bin/env nix-shell
|
|
|
|
#!nix-shell -i python3 -p bash python312 python312Packages.requests python312Packages.discordpy
|
|
|
|
# permission int: 277025409024
|
|
|
|
import discord
|
|
|
|
from discord import app_commands
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
import requests
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
intents = discord.Intents.default()
|
|
|
|
client = discord.Client(intents=intents, activity=discord.Game('Swearing at Twitter'))
|
|
|
|
tree = app_commands.CommandTree(client)
|
|
|
|
|
|
|
|
|
2024-05-04 15:09:56 +01:00
|
|
|
nitter_internal = os.environ['NITTER_URL']
|
|
|
|
nitter_external = os.getenv('NITTER_EXTERNAL_URL', nitter_internal)
|
|
|
|
|
|
|
|
|
2024-04-06 00:27:25 +01:00
|
|
|
@client.event
|
|
|
|
async def on_ready():
|
|
|
|
await tree.sync()
|
|
|
|
print(f'We have logged in as {client.user}')
|
|
|
|
|
|
|
|
@tree.command()
|
|
|
|
async def nitter(interaction: discord.Interaction, link: str):
|
|
|
|
"""Create a nitter.gmem.ca link"""
|
|
|
|
if link[:4] != 'http':
|
|
|
|
link = f'//{link}'
|
|
|
|
|
|
|
|
try:
|
|
|
|
urlparsed = urlparse(link)
|
|
|
|
except:
|
|
|
|
print(f'failed to parse {link}')
|
|
|
|
|
|
|
|
if 'twitter.com' not in urlparsed.netloc:
|
|
|
|
await interaction.response.send_message('invalid twitter link', ephemeral=True)
|
|
|
|
return
|
|
|
|
|
2024-05-04 15:09:56 +01:00
|
|
|
internal_nitter_url = f'{nitter_internal}{urlparsed.path}'
|
|
|
|
nitter_url = f'{nitter_external}{urlparsed.path}'
|
|
|
|
|
|
|
|
response = requests.get(internal_nitter_url)
|
2024-04-06 00:27:25 +01:00
|
|
|
# 4xx error codes
|
|
|
|
if 399 < response.status_code < 500:
|
|
|
|
await interaction.response.send_message('could not find tweet/user', ephemeral=True)
|
|
|
|
return
|
|
|
|
# 5xx error codes
|
|
|
|
|
|
|
|
if 499 < response.status_code < 600:
|
|
|
|
await interaction.response.send_message('internal nitter server error', ephemeral=True)
|
|
|
|
return
|
|
|
|
|
|
|
|
await interaction.response.send_message(nitter_url)
|
|
|
|
|
|
|
|
token = os.environ['DISCORD_BOT_TOKEN']
|
|
|
|
client.run(token)
|