Skip to content

Instantly share code, notes, and snippets.

@nonchris
Created May 3, 2026 13:55
Show Gist options
  • Select an option

  • Save nonchris/469756e8988d141f9c6152d3aab80524 to your computer and use it in GitHub Desktop.

Select an option

Save nonchris/469756e8988d141f9c6152d3aab80524 to your computer and use it in GitHub Desktop.
send a quick discord message via bot (for error reporting/ monitoring purposes)
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