Advertisement
xosski

Discord hwid reset bot

Dec 25th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. {
  2. "token": "YOUR_DISCORD_BOT_TOKEN",
  3. "api_url": "https://safeguard.lol/api/",
  4. "admin_token": "YOUR_ADMIN_TOKEN",
  5. "staff_channel_id": 123456789012345678,
  6. "allowed_roles": {
  7. "Lifetime Client": 30,
  8. "GHOSTY": 60
  9. }
  10. }
  11.  
  12. import discord
  13. from discord import app_commands
  14. from discord.ext import commands, tasks
  15. import aiosqlite
  16. import aiohttp
  17. import logging
  18. from datetime import datetime, timedelta
  19. import json
  20.  
  21. # Load configuration
  22. with open("config.json", "r") as config_file:
  23. config = json.load(config_file)
  24.  
  25. TOKEN = config["token"]
  26. API_URL = config["api_url"]
  27. ADMIN_TOKEN = config["admin_token"]
  28. STAFF_CHANNEL_ID = config["staff_channel_id"]
  29. ALLOWED_ROLES = config["allowed_roles"]
  30.  
  31. # Initialize bot
  32. intents = discord.Intents.default()
  33. bot = commands.Bot(command_prefix="!", intents=intents)
  34.  
  35. # Logger setup
  36. logging.basicConfig(
  37. filename="bot.log",
  38. level=logging.INFO,
  39. format="%(asctime)s - %(levelname)s - %(message)s",
  40. )
  41.  
  42. def log(log_type, message):
  43. log_colors = {
  44. "INFO": "\033[94m[INFO]\033[0m",
  45. "SUCCESS": "\033[92m[SUCCESS]\033[0m",
  46. "ERROR": "\033[91m[ERROR]\033[0m",
  47. "WARNING": "\033[93m[WARNING]\033[0m",
  48. }
  49. print(f"{log_colors.get(log_type, '[LOG]')} {datetime.now()} - {message}")
  50. logging.info(f"{log_type}: {message}")
  51.  
  52. # Database setup
  53. async def setup_database():
  54. async with aiosqlite.connect("hwid_resets.db") as db:
  55. await db.execute("""
  56. CREATE TABLE IF NOT EXISTS hwid_resets (
  57. discord_id TEXT PRIMARY KEY,
  58. key TEXT,
  59. last_reset TIMESTAMP,
  60. first_reset TIMESTAMP,
  61. total_resets INTEGER DEFAULT 0
  62. )
  63. """)
  64. await db.commit()
  65. log("INFO", "Database setup completed.")
  66.  
  67. # Update bot status
  68. @tasks.loop(seconds=30)
  69. async def update_activity():
  70. await bot.change_presence(
  71. activity=discord.Streaming(
  72. name="Reset HWID Bot | /hwid {key}", url="https://twitch.tv/example"
  73. )
  74. )
  75.  
  76. # HWID reset via API
  77. async def reset_hwid_via_api(key):
  78. async with aiohttp.ClientSession() as session:
  79. headers = {"Content-Type": "application/json", "X-API-Key": ADMIN_TOKEN}
  80. payload = {"type": "admin_command", "command": "reset_hwid", "hwid": key}
  81.  
  82. try:
  83. async with session.post(API_URL, headers=headers, json=payload) as response:
  84. if response.status == 200:
  85. return await response.json()
  86. else:
  87. return {"status": "error", "message": f"HTTP {response.status}"}
  88. except Exception as e:
  89. return {"status": "error", "message": str(e)}
  90.  
  91. # Slash command: Reset HWID
  92. @bot.tree.command(name="hwid", description="Reset your HWID key.")
  93. @app_commands.describe(key="Your HWID key.")
  94. async def hwid(interaction: discord.Interaction, key: str):
  95. # Check user roles
  96. user_roles = [role.name for role in interaction.user.roles]
  97. valid_role = None
  98. for role, cooldown_minutes in ALLOWED_ROLES.items():
  99. if role in user_roles:
  100. valid_role = role
  101. cooldown = timedelta(minutes=cooldown_minutes)
  102. break
  103.  
  104. if not valid_role:
  105. await interaction.response.send_message(
  106. "❌ You do not have the required role to use this command.", ephemeral=True
  107. )
  108. log("WARNING", f"{interaction.user} attempted to use /hwid without valid roles.")
  109. return
  110.  
  111. # Database operations
  112. async with aiosqlite.connect("hwid_resets.db") as db:
  113. async with db.execute(
  114. "SELECT last_reset, total_resets, first_reset FROM hwid_resets WHERE discord_id = ?",
  115. (str(interaction.user.id),),
  116. ) as cursor:
  117. result = await cursor.fetchone()
  118.  
  119. if result:
  120. last_reset, total_resets, first_reset = result
  121. last_reset = datetime.fromisoformat(last_reset)
  122. if datetime.now() - last_reset < cooldown:
  123. remaining = last_reset + cooldown - datetime.now()
  124. await interaction.response.send_message(
  125. f"⏳ You can reset your HWID in {remaining.seconds // 60} minutes and {remaining.seconds % 60} seconds.",
  126. ephemeral=True,
  127. )
  128. log("WARNING", f"{interaction.user} attempted a reset during cooldown.")
  129. return
  130. else:
  131. total_resets, first_reset = 0, None
  132.  
  133. # HWID reset API
  134. response = await reset_hwid_via_api(key)
  135. if response.get("status") == "success":
  136. now = datetime.now()
  137. if not result:
  138. first_reset = now
  139.  
  140. await db.execute(
  141. """
  142. INSERT OR REPLACE INTO hwid_resets (discord_id, key, last_reset, first_reset, total_resets)
  143. VALUES (?, ?, ?, ?, ?)
  144. """,
  145. (
  146. str(interaction.user.id),
  147. key,
  148. now.isoformat(),
  149. first_reset.isoformat() if first_reset else None,
  150. total_resets + 1,
  151. ),
  152. )
  153. await db.commit()
  154.  
  155. # Notify user and staff
  156. await interaction.response.send_message("✅ HWID reset successful!", ephemeral=True)
  157. log("SUCCESS", f"{interaction.user} reset HWID {key} successfully.")
  158. staff_channel = bot.get_channel(STAFF_CHANNEL_ID)
  159. embed = discord.Embed(
  160. title="🔧 HWID Reset Log",
  161. color=discord.Color.green(),
  162. timestamp=datetime.utcnow(),
  163. )
  164. embed.add_field(name="User", value=f"{interaction.user.mention}", inline=False)
  165. embed.add_field(name="HWID Key", value=f"`{key}`", inline=False)
  166. embed.add_field(name="Total Resets", value=str(total_resets + 1), inline=False)
  167. await staff_channel.send(embed=embed)
  168. else:
  169. error_message = response.get("message", "Unknown error.")
  170. await interaction.response.send_message(
  171. f"❌ Failed to reset HWID: {error_message}", ephemeral=True
  172. )
  173. log("ERROR", f"HWID reset failed for {interaction.user} - {error_message}")
  174.  
  175. # Event: Bot Ready
  176. @bot.event
  177. async def on_ready():
  178. log("SUCCESS", f"Logged in as {bot.user}")
  179. await setup_database()
  180. update_activity.start()
  181. await bot.tree.sync()
  182. log("INFO", "Slash commands synced with Discord.")
  183.  
  184. # Start the bot
  185. bot.run(TOKEN)
  186.  
Tags: discord bot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement