Flameso_o16

python discord id lookup

Feb 15th, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. import os
  2. import json
  3. import requests
  4. import tkinter as tk
  5. from tkinter import messagebox
  6. from tkinter import ttk
  7. from PIL import Image, ImageTk
  8.  
  9. DISCORD_API_BASE = "https://discord.com/api/v10"
  10. TOKEN_FILE = "bot_token.txt"
  11.  
  12. def read_bot_token():
  13. """
  14. Reads the stored bot token from a local file, if it exists.
  15. Returns the token or an empty string if not found.
  16. """
  17. if os.path.exists(TOKEN_FILE):
  18. with open(TOKEN_FILE, "r", encoding="utf-8") as f:
  19. return f.read().strip()
  20. return ""
  21.  
  22. def save_bot_token(token):
  23. """
  24. Saves the bot token to a local file.
  25. Overwrites any existing token.
  26. """
  27. with open(TOKEN_FILE, "w", encoding="utf-8") as f:
  28. f.write(token.strip())
  29.  
  30. def lookup_user():
  31. """Fetch and display the Discord user data based on the provided user ID and bot token."""
  32. user_id = entry_user_id.get().strip()
  33. bot_token = entry_bot_token.get().strip()
  34.  
  35. if not user_id.isdigit():
  36. messagebox.showerror("Error", "Please enter a valid numeric User ID.")
  37. return
  38. if not bot_token:
  39. messagebox.showerror("Error", "Please enter a valid Bot Token.")
  40. return
  41.  
  42. save_bot_token(bot_token)
  43.  
  44. url = f"{DISCORD_API_BASE}/users/{user_id}"
  45. headers = {
  46. "Authorization": f"Bot {bot_token}",
  47. "User-Agent": "DiscordBot (https://example.com, 0.1)"
  48. }
  49.  
  50. try:
  51. response = requests.get(url, headers=headers)
  52.  
  53. if response.status_code == 200:
  54. user_data = response.json()
  55. display_user_data(user_data)
  56. else:
  57. if response.status_code == 401:
  58. messagebox.showerror("Authorization Error",
  59. "Invalid bot token or insufficient permissions.")
  60. elif response.status_code == 403:
  61. messagebox.showerror("Forbidden",
  62. "The bot does not have permission to access this user.")
  63. elif response.status_code == 404:
  64. messagebox.showerror("Not Found",
  65. "User not found. The bot may not share a guild with this user.")
  66. else:
  67. messagebox.showerror("Error",
  68. f"Unexpected status code {response.status_code}:\n{response.text}")
  69. except requests.exceptions.RequestException as e:
  70. messagebox.showerror("Connection Error", f"An error occurred while requesting data:\n{e}")
  71.  
  72. def fetch_discord_image(url):
  73. """
  74. Fetches an image from Discord’s CDN and returns a Pillow Image object or None if fails.
  75. """
  76. if not url:
  77. return None
  78. try:
  79. resp = requests.get(url, stream=True)
  80. if resp.status_code == 200:
  81. return Image.open(resp.raw)
  82. except Exception:
  83. pass
  84. return None
  85.  
  86. def display_user_data(data):
  87. """Creates a popup window displaying textual data and images (avatar, banner)."""
  88. popup = tk.Toplevel(root)
  89. popup.title(f"User Info - {data.get('username', 'Unknown User')}")
  90.  
  91. main_frame = ttk.Frame(popup, padding=10)
  92. main_frame.pack(fill="both", expand=True)
  93.  
  94. avatar_url = None
  95. if data.get("avatar"):
  96. avatar_url = f"https://cdn.discordapp.com/avatars/{data['id']}/{data['avatar']}.png"
  97. else:
  98. pass
  99.  
  100. banner_url = None
  101. if data.get("banner"):
  102. banner_url = f"https://cdn.discordapp.com/banners/{data['id']}/{data['banner']}.png"
  103.  
  104. text_frame = ttk.Frame(main_frame)
  105. text_frame.pack(side="left", fill="y", padx=10, pady=10)
  106.  
  107. lbl_info = ttk.Label(text_frame, text="User Information", font=("Helvetica", 12, "bold"))
  108. lbl_info.pack(anchor="w", pady=(0, 5))
  109.  
  110. info_lines = []
  111. possible_fields = [
  112. "id", "username", "global_name", "discriminator", "avatar",
  113. "bot", "public_flags", "banner", "accent_color", "banner_color",
  114. "avatar_decoration"
  115. ]
  116. for field in possible_fields:
  117. if field in data:
  118. info_lines.append(f"{field}: {data[field]}")
  119.  
  120. info_text = "\n".join(info_lines)
  121. lbl_data = ttk.Label(text_frame, text=info_text, justify="left")
  122. lbl_data.pack(anchor="w")
  123.  
  124. img_frame = ttk.Frame(main_frame)
  125. img_frame.pack(side="right", fill="both", expand=True, padx=10, pady=10)
  126.  
  127. if avatar_url:
  128. avatar_img = fetch_discord_image(avatar_url)
  129. if avatar_img:
  130. avatar_img = avatar_img.resize((128, 128), Image.Resampling.LANCZOS)
  131. avatar_tk = ImageTk.PhotoImage(avatar_img)
  132.  
  133. lbl_avatar = ttk.Label(img_frame, text="Avatar", font=("Helvetica", 11, "bold"))
  134. lbl_avatar.pack()
  135.  
  136. lbl_avatar_img = ttk.Label(img_frame, image=avatar_tk)
  137. lbl_avatar_img.image = avatar_tk
  138. lbl_avatar_img.pack(pady=5)
  139. else:
  140. ttk.Label(img_frame, text="Unable to load avatar").pack(pady=5)
  141. else:
  142. ttk.Label(img_frame, text="No Avatar").pack(pady=5)
  143.  
  144. if banner_url:
  145. banner_img = fetch_discord_image(banner_url)
  146. if banner_img:
  147. w, h = banner_img.size
  148. if w > 300:
  149. ratio = 300 / w
  150. new_w = 300
  151. new_h = int(h * ratio)
  152. banner_img = banner_img.resize((new_w, new_h), Image.Resampling.LANCZOS)
  153. banner_tk = ImageTk.PhotoImage(banner_img)
  154.  
  155. lbl_banner = ttk.Label(img_frame, text="Banner", font=("Helvetica", 11, "bold"))
  156. lbl_banner.pack(pady=(20, 0))
  157.  
  158. lbl_banner_img = ttk.Label(img_frame, image=banner_tk)
  159. lbl_banner_img.image = banner_tk
  160. lbl_banner_img.pack(pady=5)
  161. else:
  162. ttk.Label(img_frame, text="Unable to load banner").pack(pady=5)
  163.  
  164. popup.geometry("600x400")
  165.  
  166. root = tk.Tk()
  167. root.title("Discord User Lookup")
  168.  
  169. input_frame = ttk.Frame(root, padding=15)
  170. input_frame.pack(fill="both", expand=True)
  171.  
  172. label_user_id = ttk.Label(input_frame, text="Discord User ID:")
  173. label_user_id.grid(row=0, column=0, padx=5, pady=5, sticky="e")
  174.  
  175. entry_user_id = ttk.Entry(input_frame, width=30)
  176. entry_user_id.grid(row=0, column=1, padx=5, pady=5)
  177.  
  178. label_bot_token = ttk.Label(input_frame, text="Bot Token:")
  179. label_bot_token.grid(row=1, column=0, padx=5, pady=5, sticky="e")
  180.  
  181. entry_bot_token = ttk.Entry(input_frame, width=30, show="*")
  182. entry_bot_token.grid(row=1, column=1, padx=5, pady=5)
  183.  
  184. saved_token = read_bot_token()
  185. if saved_token:
  186. entry_bot_token.insert(0, saved_token)
  187.  
  188. btn_lookup = ttk.Button(input_frame, text="Lookup User", command=lookup_user)
  189. btn_lookup.grid(row=2, column=0, columnspan=2, pady=10)
  190.  
  191. root.mainloop()
  192. main.py
  193. 7 KB
Add Comment
Please, Sign In to add comment