Created
July 21, 2024 10:48
-
-
Save notmarek/56ded8f62b231f788f582c3ccff779d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import re | |
from requests_toolbelt.multipart.encoder import MultipartEncoder | |
WEBHOOK = "" | |
TOKEN = "" | |
CHANNEL_ID = "" | |
AVATAR_URL = lambda uid, aid: f"https://cdn.discordapp.com/avatars/{uid}/{aid}.webp?size=96" | |
EMOJI = lambda name, id: f"[{name}](https://media.discordapp.net/emojis/{id}.webp?size=48&quality=lossless&name={name})" | |
STICKER = lambda name, id: f"[{name}](https://media.discordapp.net/stickers/{id}.webp?size=240)" | |
def REMOVE_EVERYONE(content): | |
return content.replace("@everyone", "@noone") | |
def EMOJI_FIX(content): | |
return re.sub(r"<:(.*?):(\d*?)>", r"[\1](https://media.discordapp.net/emojis/\2.webp?size=48&quality=lossless&name=\1)", content) | |
def STICKER_FIX(content, stickers): | |
for sticker in stickers: | |
content += f"\n{STICKER(sticker['name'], sticker['id'])}" | |
return content | |
def POLL_FIX(content, poll): | |
if poll is None: | |
return content | |
content += f"\n# {poll['question']['text']} {' **(final)**' if poll['results']['is_finalized'] else ''}" | |
for answer in poll["answers"]: | |
media = answer["poll_media"] | |
answer_count = [x for x in poll['results']['answer_counts'] if x["id"] == answer["answer_id"]][0] | |
if "emoji" in media: | |
content += f"\n\t{EMOJI(media['emoji']['name'], media['emoji']['id'])} {media['text']}: {answer_count['count']} votes" | |
else: | |
content += f"\n\t{media['text']}: {answer_count['count']} votes" | |
return content | |
LAST_KNOWN_MSG_FILE = ".last_known_msg_id" | |
headers = { | |
"Authorization": TOKEN, | |
} | |
last_sent_id = 0 | |
try: | |
with open(LAST_KNOWN_MSG_FILE, "r") as f: | |
val = f.read() | |
try: | |
last_sent_id = int(val) | |
except Exception: | |
pass | |
except FileNotFoundError: | |
pass | |
res = requests.get("https://discord.com/api/v9/channels/{CHANNEL_ID}/messages?limit=500", headers=headers) | |
data = res.json() | |
data.sort(key=lambda x: int(x["id"])) | |
data = [x for x in data if int(x["id"]) > last_sent_id] | |
for x in data: | |
x["content"] = REMOVE_EVERYONE(POLL_FIX(STICKER_FIX(EMOJI_FIX(x["content"]), x["sticker_items"] if "sticker_items" in x else []), x["poll"] if "poll" in x else None)) | |
for x in data: | |
webhook_json = { | |
"content": x["content"], | |
"username": x["author"]["username"], | |
"avatar_url": AVATAR_URL(x["author"]["id"], x["author"]["avatar"]), | |
} | |
attachments = [(x["filename"], requests.get(x['url']).content, x['content_type']) for x in x["attachments"]] | |
fields = { | |
"payload_json": (None, json.dumps(webhook_json), "application/json"), | |
} | |
for i, at in enumerate(attachments): | |
fields[f"files[{i}]"] = at | |
files = MultipartEncoder(fields=fields) | |
requests.post(WEBHOOK, data=files, headers={'Content-Type': files.content_type}) | |
last_sent_id = int(x["id"]) | |
with open(LAST_KNOWN_MSG_FILE, "w") as f: | |
f.write(str(last_sent_id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment