Created
May 3, 2026 13:55
-
-
Save nonchris/469756e8988d141f9c6152d3aab80524 to your computer and use it in GitHub Desktop.
send a quick discord message via bot (for error reporting/ monitoring purposes)
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 os | |
| from pathlib import Path | |
| import requests | |
| DISCORD_API = "https://discord.com/api/v10" | |
| USER_ID = 00000 # UR ID | |
| def send_message(msg: str, token: str) -> None: | |
| """Send a Discord DM to the configured user via the Discord REST API. | |
| Opens (or reuses) a DM channel with USER_ID and posts the message. | |
| Raises an exception on HTTP errors. | |
| """ | |
| headers = { | |
| "Authorization": f"Bot {token}", | |
| "Content-Type": "application/json", | |
| } | |
| channel_resp = requests.post( | |
| f"{DISCORD_API}/users/@me/channels", | |
| json={"recipient_id": USER_ID}, | |
| headers=headers, | |
| timeout=10, | |
| ) | |
| channel_resp.raise_for_status() | |
| channel_id = channel_resp.json()["id"] | |
| msg_resp = requests.post( | |
| f"{DISCORD_API}/channels/{channel_id}/messages", | |
| json={"content": msg}, | |
| headers=headers, | |
| timeout=10, | |
| ) | |
| msg_resp.raise_for_status() | |
| if __name__ == "__main__": | |
| TOKEN = Path("TOKEN").read_text().strip() | |
| send_message("TEST", token=TOKEN) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment