Advertisement
xosski

Discord hwid bot

Dec 25th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands, tasks
  3. import aiohttp
  4. import sqlite3
  5. from datetime import datetime, timedelta
  6.  
  7. # Configuration
  8. TOKEN = "YOUR_DISCORD_BOT_TOKEN"
  9. API_URL = "https://safeguard.lol/api/"
  10. ADMIN_TOKEN = "YOUR_ADMIN_TOKEN"
  11. STAFF_CHANNEL_ID = 123456789012345678 # Replace with your staff channel ID
  12. ALLOWED_ROLES = ["Lifetime Client", "1"] # Roles allowed to use the /hwid command
  13.  
  14. # Initialize bot
  15. intents = discord.Intents.default()
  16. intents.message_content = True
  17. bot = commands.Bot(command_prefix="!", intents=intents)
  18.  
  19. # Database setup
  20. conn = sqlite3.connect("hwid_resets.db")
  21. cursor = conn.cursor()
  22. cursor.execute("""
  23. CREATE TABLE IF NOT EXISTS hwid_resets (
  24. discord_id TEXT PRIMARY KEY,
  25. key TEXT,
  26. last_reset TIMESTAMP,
  27. first_reset TIMESTAMP,
  28. total_resets INTEGER DEFAULT 0
  29. )
  30. """)
  31. conn.commit()
  32.  
  33. # Helper function to log messages to console
  34. def log_to_console(log_type, message):
  35. log_types = {
  36. "INFO": "\033[94m[INFO]\033[0m",
  37. "SUCCESS": "\033[92m[SUCCESS]\033[0m",
  38. "ERROR": "\033[91m[ERROR]\033[0m",
  39. "WARNING": "\033[93m[WARNING]\033[0m"
  40. }
  41. print(f"{log_types.get(log_type, '[LOG]')} {datetime.now()} - {message}")
  42.  
  43. # Helper function: Reset HWID via API
  44. async def reset_hwid(hwid):
  45. async with aiohttp.ClientSession() as session:
  46. headers = {
  47. "Content-Type": "application/json",
  48. "X-API-Key": ADMIN_TOKEN
  49. }
  50. payload = {
  51. "type": "admin_command",
  52. "command": "reset_hwid",
  53. "hwid": hwid
  54. }
  55. async with session.post(API_URL, headers=headers, json=payload) as response:
  56. return await response.json()
  57.  
  58. # Helper function to check if user has the required role
  59. def has_allowed_role(member):
  60. roles = [role.name for role in member.roles]
  61. return any(role in ALLOWED_ROLES for role in roles)
  62.  
  63. # Helper function: Check rate limits for HWID reset
  64. def is_rate_limited(user_id):
  65. cursor.execute("SELECT last_reset, total_resets, first_reset FROM hwid_resets WHERE discord_id = ?", (str(user_id),))
  66. result = cursor.fetchone()
  67.  
  68. if result:
  69. last_reset, total_resets, first_reset = result
  70. last_reset = datetime.fromisoformat(last_reset)
  71. cooldown = timedelta(minutes=30)
  72. if datetime.now() - last_reset < cooldown:
  73. remaining = last_reset + cooldown - datetime.now()
  74. return True, remaining
  75. return False, None
  76.  
  77. # Helper function to handle sending messages to users and logging to staff
  78. async def send_message(ctx, message, log_type="INFO"):
  79. await ctx.send(message)
  80. log_to_console(log_type, f"{ctx.author}: {message}")
  81.  
  82. # Command to reset HWID
  83. @bot.command()
  84. async def hwid(ctx):
  85. # Check if user has the required role
  86. if not has_allowed_role(ctx.author):
  87. await send_message(ctx, "❌ You do not have the required role to use this command.", "WARNING")
  88. return
  89.  
  90. def check(msg):
  91. return msg.author == ctx.author and msg.channel == ctx.channel
  92.  
  93. # Prompt user for the HWID key
  94. await ctx.send("Please enter your key:")
  95. try:
  96. hwid_key = await bot.wait_for("message", check=check, timeout=60.0)
  97. hwid_key = hwid_key.content.strip()
  98. except:
  99. await send_message(ctx, "You took too long to respond. Please try again.", "WARNING")
  100. return
  101.  
  102. # Validate HWID ownership
  103. cursor.execute("SELECT discord_id FROM hwid_resets WHERE key = ?", (hwid_key,))
  104. key_owner = cursor.fetchone()
  105. if key_owner and key_owner[0] != str(ctx.author.id):
  106. await send_message(ctx, "❌ This HWID key is already registered to another user!", "ERROR")
  107. return
  108.  
  109. # Check rate limits
  110. is_limited, remaining_time = is_rate_limited(ctx.author.id)
  111. if is_limited:
  112. await ctx.send(f"You can reset your HWID again in {remaining_time.seconds // 60} minutes and {remaining_time.seconds % 60} seconds.")
  113. log_to_console("WARNING", f"{ctx.author} attempted to reset HWID before cooldown expired.")
  114. return
  115.  
  116. # Attempt HWID reset via API
  117. response = await reset_hwid(hwid_key)
  118. if response["status"] == "success":
  119. # Save to database
  120. cursor.execute("""
  121. INSERT OR REPLACE INTO hwid_resets (discord_id, key, last_reset, first_reset, total_resets)
  122. VALUES (?, ?, ?, ?, ?)
  123. """, (str(ctx.author.id), hwid_key, datetime.now().isoformat(), datetime.now().isoformat(), 1)) # First reset
  124. conn.commit()
  125.  
  126. await send_message(ctx, "✅ Your HWID has been reset successfully!", "SUCCESS")
  127.  
  128. # Log to staff channel
  129. staff_channel = bot.get_channel(STAFF_CHANNEL_ID)
  130. embed = discord.Embed(
  131. title="🔧 HWID Reset Log",
  132. color=discord.Color.green(),
  133. timestamp=datetime.utcnow()
  134. )
  135. embed.add_field(name="User", value=f"{ctx.author.mention} (`{ctx.author.id}`)", inline=False)
  136. embed.add_field(name="HWID Key", value=f"`{hwid_key}`", inline=False)
  137. embed.add_field(name="Status", value="✅ Success", inline=False)
  138. embed.set_footer(text="HWID Reset System")
  139. await staff_channel.send(embed=embed)
  140. else:
  141. await send_message(ctx, f"❌ Failed to reset HWID: {response.get('message', 'Unknown error')}", "ERROR")
  142.  
  143. # Log failure to staff channel
  144. staff_channel = bot.get_channel(STAFF_CHANNEL_ID)
  145. embed = discord.Embed(
  146. title="🔧 HWID Reset Log",
  147. color=discord.Color.red(),
  148. timestamp=datetime.utcnow()
  149. )
  150. embed.add_field(name="User", value=f"{ctx.author.mention} (`{ctx.author.id}`)", inline=False)
  151. embed.add_field(name="HWID Key", value=f"`{hwid_key}`", inline=False)
  152. embed.add_field(name="Status", value="❌ Failed", inline=False)
  153. embed.add_field(name="Error Message", value=response.get('message', 'Unknown error'), inline=False)
  154. embed.set_footer(text="HWID Reset System")
  155. await staff_channel.send(embed=embed)
  156.  
  157. # Bot events
  158. @bot.event
  159. async def on_ready():
  160. print("\033[92mBot is online and ready!\033[0m")
  161. log_to_console("INFO", f"Logged in as {bot.user}")
  162. update_activity.start()
  163.  
  164. # Start the bot
  165. bot.run(TOKEN)
Tags: discord bot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement