mirror of
https://gitlab.com/pancakes1234/wdiscordbotserver.git
synced 2025-06-16 07:14:21 -06:00
303 lines
14 KiB
Python
303 lines
14 KiB
Python
import discord
|
|
from discord.ext import commands
|
|
from discord import app_commands
|
|
from typing import Optional
|
|
import json
|
|
import os
|
|
|
|
class UserInfoCog(commands.Cog):
|
|
def __init__(self, bot: commands.Bot):
|
|
self.bot = bot
|
|
self.user_data_file = "user_data.json"
|
|
self.user_data = self._load_user_data()
|
|
self.developer_id = 1141746562922459136 # The developer's ID
|
|
|
|
def _load_user_data(self):
|
|
"""Load user data from JSON file"""
|
|
if os.path.exists(self.user_data_file):
|
|
try:
|
|
with open(self.user_data_file, 'r') as f:
|
|
return json.load(f)
|
|
except Exception as e:
|
|
print(f"Error loading user data: {e}")
|
|
return {}
|
|
return {}
|
|
|
|
def _save_user_data(self):
|
|
"""Save user data to JSON file"""
|
|
try:
|
|
with open(self.user_data_file, 'w') as f:
|
|
json.dump(self.user_data, f, indent=4)
|
|
except Exception as e:
|
|
print(f"Error saving user data: {e}")
|
|
|
|
@app_commands.command(name="setuser", description="Set custom data for a user")
|
|
@app_commands.describe(
|
|
user="The user to set data for",
|
|
nickname="Custom nickname",
|
|
pronouns="User's pronouns",
|
|
bio="Short biography",
|
|
birthday="User's birthday (YYYY-MM-DD)",
|
|
location="User's location",
|
|
custom_field="Name of custom field",
|
|
custom_value="Value for custom field"
|
|
)
|
|
async def setuser(
|
|
self,
|
|
interaction: discord.Interaction,
|
|
user: discord.Member,
|
|
nickname: Optional[str] = None,
|
|
pronouns: Optional[str] = None,
|
|
bio: Optional[str] = None,
|
|
birthday: Optional[str] = None,
|
|
location: Optional[str] = None,
|
|
custom_field: Optional[str] = None,
|
|
custom_value: Optional[str] = None
|
|
):
|
|
"""Set custom data for a user - only works for the developer"""
|
|
# Check if the command user is the developer
|
|
if interaction.user.id != self.developer_id:
|
|
await interaction.response.send_message("This command is only available to the bot developer.", ephemeral=True)
|
|
return
|
|
|
|
# Initialize user data if not exists
|
|
user_id = str(user.id)
|
|
if user_id not in self.user_data:
|
|
self.user_data[user_id] = {}
|
|
|
|
# Update fields if provided
|
|
fields_updated = []
|
|
|
|
if nickname is not None:
|
|
self.user_data[user_id]["nickname"] = nickname
|
|
fields_updated.append("Nickname")
|
|
|
|
if pronouns is not None:
|
|
self.user_data[user_id]["pronouns"] = pronouns
|
|
fields_updated.append("Pronouns")
|
|
|
|
if bio is not None:
|
|
self.user_data[user_id]["bio"] = bio
|
|
fields_updated.append("Bio")
|
|
|
|
if birthday is not None:
|
|
self.user_data[user_id]["birthday"] = birthday
|
|
fields_updated.append("Birthday")
|
|
|
|
if location is not None:
|
|
self.user_data[user_id]["location"] = location
|
|
fields_updated.append("Location")
|
|
|
|
if custom_field is not None and custom_value is not None:
|
|
if "custom_fields" not in self.user_data[user_id]:
|
|
self.user_data[user_id]["custom_fields"] = {}
|
|
self.user_data[user_id]["custom_fields"][custom_field] = custom_value
|
|
fields_updated.append(f"Custom field '{custom_field}'")
|
|
|
|
# Save data
|
|
self._save_user_data()
|
|
|
|
# Create response embed
|
|
embed = discord.Embed(
|
|
title=f"User Data Updated",
|
|
description=f"Updated data for {user.mention}",
|
|
color=discord.Color.green()
|
|
)
|
|
|
|
if fields_updated:
|
|
embed.add_field(name="Fields Updated", value=", ".join(fields_updated), inline=False)
|
|
|
|
# Show current values
|
|
embed.add_field(name="Current Values", value="User's current data:", inline=False)
|
|
for field, value in self.user_data[user_id].items():
|
|
if field != "custom_fields":
|
|
embed.add_field(name=field.capitalize(), value=value, inline=True)
|
|
|
|
# Show custom fields if any
|
|
if "custom_fields" in self.user_data[user_id] and self.user_data[user_id]["custom_fields"]:
|
|
custom_fields_text = "\n".join([f"**{k}**: {v}" for k, v in self.user_data[user_id]["custom_fields"].items()])
|
|
embed.add_field(name="Custom Fields", value=custom_fields_text, inline=False)
|
|
else:
|
|
embed.description = "No fields were updated."
|
|
|
|
await interaction.response.send_message(embed=embed, ephemeral=True)
|
|
|
|
@app_commands.command(name="clearuserdata", description="Clear custom data for a user")
|
|
@app_commands.describe(user="The user to clear data for")
|
|
async def clearuserdata(self, interaction: discord.Interaction, user: discord.Member):
|
|
"""Clear all custom data for a user - only works for the developer"""
|
|
# Check if the command user is the developer
|
|
if interaction.user.id != self.developer_id:
|
|
await interaction.response.send_message("This command is only available to the bot developer.", ephemeral=True)
|
|
return
|
|
|
|
user_id = str(user.id)
|
|
if user_id in self.user_data:
|
|
del self.user_data[user_id]
|
|
self._save_user_data()
|
|
await interaction.response.send_message(f"All custom data for {user.mention} has been cleared.", ephemeral=True)
|
|
else:
|
|
await interaction.response.send_message(f"No custom data found for {user.mention}.", ephemeral=True)
|
|
|
|
@app_commands.command(name="aboutuser", description="Display info about a user or yourself.")
|
|
@app_commands.describe(user="The user to get info about (optional)")
|
|
async def aboutuser(self, interaction: discord.Interaction, user: Optional[discord.Member] = None):
|
|
member = user or interaction.user
|
|
# Fetch up-to-date member info
|
|
if interaction.guild:
|
|
member = interaction.guild.get_member(member.id) or member
|
|
# Fetch user object for banner/profile
|
|
user_obj = member._user if hasattr(member, '_user') else member
|
|
# Banner fetching (API call)
|
|
banner_url = None
|
|
try:
|
|
user_obj = await self.bot.fetch_user(member.id)
|
|
if user_obj.banner:
|
|
banner_url = user_obj.banner.url
|
|
except Exception:
|
|
pass
|
|
# Status
|
|
if isinstance(member, discord.Member):
|
|
status = str(member.status).capitalize()
|
|
else:
|
|
status = "Unknown"
|
|
# Devices (accurate for discord.py 2.3+)
|
|
device_map = {
|
|
"desktop": "Desktop",
|
|
"mobile": "Mobile",
|
|
"web": "Web"
|
|
}
|
|
devices = set()
|
|
if hasattr(member, "devices") and member.devices:
|
|
for dev in member.devices:
|
|
devices.add(device_map.get(str(dev), str(dev).capitalize()))
|
|
else:
|
|
# Fallback for older discord.py
|
|
if hasattr(member, "desktop_status") and member.desktop_status != discord.Status.offline:
|
|
devices.add("Desktop")
|
|
if hasattr(member, "mobile_status") and member.mobile_status != discord.Status.offline:
|
|
devices.add("Mobile")
|
|
if hasattr(member, "web_status") and member.web_status != discord.Status.offline:
|
|
devices.add("Web")
|
|
device_str = ", ".join(devices) if devices else "Offline/Unknown"
|
|
# Activities (show all, including custom, game, music, etc.)
|
|
activities = []
|
|
if hasattr(member, "activities") and member.activities:
|
|
for activity in member.activities:
|
|
if isinstance(activity, discord.Game):
|
|
activities.append(f"Playing {activity.name}")
|
|
elif isinstance(activity, discord.Spotify):
|
|
activities.append(f"Listening to {activity.title} by {', '.join(activity.artists)}")
|
|
elif isinstance(activity, discord.CustomActivity):
|
|
if activity.name:
|
|
activities.append(f"Custom Status: {activity.name}")
|
|
elif isinstance(activity, discord.Activity):
|
|
# General fallback for other activity types
|
|
act_type = getattr(activity.type, 'name', str(activity.type)).title()
|
|
activities.append(f"{act_type}: {activity.name}")
|
|
activity_str = ", ".join(activities) if activities else "None"
|
|
# Roles
|
|
if isinstance(member, discord.Member) and interaction.guild:
|
|
roles = [role.mention for role in member.roles if role != interaction.guild.default_role]
|
|
roles_str = ", ".join(roles) if roles else "None"
|
|
else:
|
|
roles_str = "None"
|
|
|
|
# Badges
|
|
badge_map = {
|
|
"staff": "Discord Staff 🛡️",
|
|
"partner": "Partner ⭐",
|
|
"hypesquad": "HypeSquad Event 🏆",
|
|
"bug_hunter": "Bug Hunter 🐛",
|
|
"hypesquad_bravery": "Bravery 🦁",
|
|
"hypesquad_brilliance": "Brilliance 🧠",
|
|
"hypesquad_balance": "Balance ⚖️",
|
|
"early_supporter": "Early Supporter 🕰️",
|
|
"team_user": "Team User 👥",
|
|
"system": "System 🤖",
|
|
"bug_hunter_level_2": "Bug Hunter Level 2 🐞",
|
|
"verified_bot": "Verified Bot 🤖",
|
|
"verified_developer": "Early Verified Bot Dev 🛠️",
|
|
"discord_certified_moderator": "Certified Mod 🛡️",
|
|
"active_developer": "Active Developer 🧑💻"
|
|
}
|
|
user_flags = getattr(user_obj, 'public_flags', None)
|
|
badges = []
|
|
if user_flags:
|
|
for flag in badge_map:
|
|
if getattr(user_flags, flag, False):
|
|
badges.append(badge_map[flag])
|
|
badge_str = ", ".join(badges) if badges else ""
|
|
if member.id == self.developer_id:
|
|
badge_str = (badge_str + ", " if badge_str else "") + "Bot Developer 🛠️"
|
|
# Embed
|
|
embed = discord.Embed(
|
|
title=f"User Info: {member.display_name}",
|
|
color=member.color if hasattr(member, 'color') else discord.Color.blurple(),
|
|
description=f"Profile of {member.mention}"
|
|
)
|
|
if badge_str:
|
|
embed.add_field(name="Badge", value=badge_str, inline=False)
|
|
# If banner_url exists, set as embed image at the top
|
|
if banner_url:
|
|
embed.set_image(url=banner_url)
|
|
embed.set_thumbnail(url=member.display_avatar.url)
|
|
embed.add_field(name="Nickname", value=member.nick or "None", inline=True)
|
|
embed.add_field(name="Username", value=f"{member.name}#{member.discriminator}", inline=True)
|
|
embed.add_field(name="User ID", value=member.id, inline=True)
|
|
embed.add_field(name="Status", value=status, inline=True)
|
|
embed.add_field(name="Device", value=device_str, inline=True)
|
|
embed.add_field(name="Activity", value=activity_str, inline=True)
|
|
embed.add_field(name="Roles", value=roles_str, inline=False)
|
|
# Account created
|
|
embed.add_field(name="Account Created", value=member.created_at.strftime('%Y-%m-%d %H:%M:%S UTC'), inline=True)
|
|
if hasattr(member, 'joined_at') and member.joined_at:
|
|
embed.add_field(name="Joined Server", value=member.joined_at.strftime('%Y-%m-%d %H:%M:%S UTC'), inline=True)
|
|
# --- Additional User Info (TOS-compliant) ---
|
|
# Accent color
|
|
accent_color = getattr(user_obj, 'accent_color', None)
|
|
if accent_color:
|
|
embed.add_field(name="Accent Color", value=str(accent_color), inline=True)
|
|
# Avatar Decoration (if available)
|
|
avatar_decoration = getattr(user_obj, 'avatar_decoration', None)
|
|
if avatar_decoration:
|
|
embed.add_field(name="Avatar Decoration", value=str(avatar_decoration), inline=True)
|
|
# Nitro status (public flag)
|
|
if user_flags and getattr(user_flags, 'premium', False):
|
|
embed.add_field(name="Nitro", value="Yes", inline=True)
|
|
|
|
# Add custom user data if available
|
|
user_id = str(member.id)
|
|
if user_id in self.user_data:
|
|
# Add custom data fields
|
|
for field, value in self.user_data[user_id].items():
|
|
if field != "custom_fields":
|
|
embed.add_field(name=field.capitalize(), value=value, inline=True)
|
|
|
|
# Add custom fields if any
|
|
if "custom_fields" in self.user_data[user_id] and self.user_data[user_id]["custom_fields"]:
|
|
custom_fields_text = "\n".join([f"**{k}**: {v}" for k, v in self.user_data[user_id]["custom_fields"].items()])
|
|
embed.add_field(name="Custom Fields", value=custom_fields_text, inline=False)
|
|
|
|
# Pronouns (if available)
|
|
pronouns = getattr(user_obj, 'pronouns', None)
|
|
if pronouns and user_id not in self.user_data: # Only show Discord's pronouns if we don't have custom ones
|
|
embed.add_field(name="Pronouns", value=pronouns, inline=True)
|
|
# Locale/language (if available)
|
|
locale = getattr(user_obj, 'locale', None)
|
|
if locale:
|
|
embed.add_field(name="Locale", value=locale, inline=True)
|
|
# Server boosting status
|
|
if isinstance(member, discord.Member) and getattr(member, 'premium_since', None):
|
|
embed.add_field(name="Server Booster", value=f"Since {member.premium_since.strftime('%Y-%m-%d %H:%M:%S UTC')}", inline=True)
|
|
# Mutual servers (if bot shares more than one)
|
|
if hasattr(self.bot, 'guilds'):
|
|
mutual_guilds = [g.name for g in self.bot.guilds if g.get_member(member.id)]
|
|
if len(mutual_guilds) > 1:
|
|
embed.add_field(name="Mutual Servers", value=", ".join(mutual_guilds), inline=False)
|
|
# End of additional info
|
|
embed.set_footer(text=f"Requested by {interaction.user.display_name}", icon_url=interaction.user.display_avatar.url)
|
|
await interaction.response.send_message(embed=embed)
|
|
|
|
async def setup(bot: commands.Bot):
|
|
await bot.add_cog(UserInfoCog(bot) |