Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
test
Browse files
app.py
CHANGED
|
@@ -76,7 +76,7 @@ async def on_interaction(interaction: discord.Interaction):
|
|
| 76 |
|
| 77 |
|
| 78 |
class DMButton(Button):
|
| 79 |
-
def __init__(self
|
| 80 |
super().__init__(
|
| 81 |
label="Verify Discord Account",
|
| 82 |
style=discord.ButtonStyle.primary,
|
|
@@ -88,18 +88,35 @@ class DMButton(Button):
|
|
| 88 |
await interaction.response.defer(ephemeral=True) # to fix interaction issue
|
| 89 |
|
| 90 |
user_id = interaction.user.id
|
| 91 |
-
|
| 92 |
guild = interaction.guild
|
| 93 |
-
member = guild.get_member(user_id)
|
| 94 |
-
pending_role = guild.get_role(1380908157475360899) # pending verification role, this is to verify that the discord account actually clicked the button at least once
|
| 95 |
|
| 96 |
-
if member and pending_role:
|
| 97 |
-
await member.add_roles(pending_role)
|
| 98 |
-
print(f"Assigned 'Pending Verification' role to {member.name}") # should log this in #admin-logs properly
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
# new
|
|
@@ -109,54 +126,44 @@ class PersistentVerifyView(discord.ui.View):
|
|
| 109 |
self.add_item(DMButton())
|
| 110 |
|
| 111 |
async def on_error(self, error: Exception, item, interaction: discord.Interaction):
|
| 112 |
-
import logging
|
| 113 |
logging.exception("Component error")
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
@bot.event
|
| 122 |
async def on_ready():
|
| 123 |
-
|
| 124 |
-
print(f'Logged in as {bot.user.name}')
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
channel = bot.get_channel(900125909984624713)
|
| 129 |
-
msg = await channel.fetch_message(1271145797433557023)
|
| 130 |
-
await msg.edit(view=PersistentVerifyView()) # <- updates the message’s components
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
guild = bot.get_guild(879548962464493619)
|
| 135 |
-
await guild.chunk() # get all users into bot cache
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
print(f"Message with ID 1271145797433557023 not found.")
|
| 151 |
-
except discord.HTTPException as e:
|
| 152 |
-
print(f"Failed to fetch message with ID 1271145797433557023: {e}")
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
"""
|
| 156 |
-
|
| 157 |
-
print("------------------------------------------------------------------------")
|
| 158 |
-
except Exception as e:
|
| 159 |
-
print(f"on_ready Error: {e}")
|
| 160 |
|
| 161 |
|
| 162 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
|
@@ -172,31 +179,47 @@ threading.Thread(target=run_bot).start()
|
|
| 172 |
def verify_button(profile: gr.OAuthProfile | None, request: gr.Request) -> str:
|
| 173 |
query_params = parse_qs(urlparse(str(request.url)).query)
|
| 174 |
user_id = query_params.get('user_id', [None])[0]
|
| 175 |
-
|
| 176 |
if not user_id:
|
| 177 |
return "# ❌ Missing Discord user ID."
|
| 178 |
-
|
| 179 |
if profile is None:
|
| 180 |
return "# ❌ You're not logged in with Hugging Face."
|
| 181 |
|
| 182 |
async def upgrade_role():
|
|
|
|
| 183 |
guild = bot.get_guild(879548962464493619)
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
| 190 |
return
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
global discord_loop
|
| 200 |
if discord_loop:
|
| 201 |
asyncio.run_coroutine_threadsafe(upgrade_role(), discord_loop)
|
| 202 |
return f"# ✅ Verification successful! Welcome, {profile.username} 🎉"
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
class DMButton(Button):
|
| 79 |
+
def __init__(self):
|
| 80 |
super().__init__(
|
| 81 |
label="Verify Discord Account",
|
| 82 |
style=discord.ButtonStyle.primary,
|
|
|
|
| 88 |
await interaction.response.defer(ephemeral=True) # to fix interaction issue
|
| 89 |
|
| 90 |
user_id = interaction.user.id
|
|
|
|
| 91 |
guild = interaction.guild
|
|
|
|
|
|
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# safer than cache:
|
| 95 |
+
try:
|
| 96 |
+
member = await guild.fetch_member(user_id)
|
| 97 |
+
except discord.NotFound:
|
| 98 |
+
await interaction.followup.send("Could not find your member record.", ephemeral=True)
|
| 99 |
+
return
|
| 100 |
+
|
| 101 |
+
pending_role = guild.get_role(1380908157475360899)
|
| 102 |
+
|
| 103 |
+
if pending_role:
|
| 104 |
+
try:
|
| 105 |
+
await member.add_roles(pending_role, reason="Clicked verification button")
|
| 106 |
+
logging.info("Assigned Pending to %s (%s)", member, member.id)
|
| 107 |
+
except discord.Forbidden:
|
| 108 |
+
logging.error("Missing permissions or role hierarchy below Pending.")
|
| 109 |
+
except Exception:
|
| 110 |
+
logging.exception("Failed to add Pending role")
|
| 111 |
+
|
| 112 |
+
unique_link = f"<{GRADIO_APP_URL}?user_id={user_id}>"
|
| 113 |
+
msg = (
|
| 114 |
+
"Login link generated! To complete verification:\n"
|
| 115 |
+
"- 1 Visit this link,\n- 2 click '🤗 Sign in with Hugging Face'\n\n"
|
| 116 |
+
f"{unique_link}"
|
| 117 |
+
)
|
| 118 |
+
# since we deferred:
|
| 119 |
+
await interaction.followup.send(msg, ephemeral=True)
|
| 120 |
|
| 121 |
|
| 122 |
# new
|
|
|
|
| 126 |
self.add_item(DMButton())
|
| 127 |
|
| 128 |
async def on_error(self, error: Exception, item, interaction: discord.Interaction):
|
|
|
|
| 129 |
logging.exception("Component error")
|
| 130 |
+
try:
|
| 131 |
+
if interaction.response.is_done():
|
| 132 |
+
await interaction.followup.send("Something broke while handling that click.", ephemeral=True)
|
| 133 |
+
else:
|
| 134 |
+
await interaction.response.send_message("Something broke while handling that click.", ephemeral=True)
|
| 135 |
+
except Exception:
|
| 136 |
+
logging.exception("Failed to notify user after component error.")
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
@bot.event
|
| 141 |
async def on_ready():
|
| 142 |
+
logging.info("Logged in as %s (%s)", bot.user, bot.user.id)
|
|
|
|
| 143 |
|
| 144 |
+
view = PersistentVerifyView()
|
| 145 |
+
bot.add_view(view) # binds handler for the custom_id
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
+
channel = bot.get_channel(900125909984624713)
|
| 148 |
+
msg = await channel.fetch_message(1271145797433557023)
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
# Step A: clear existing components
|
| 151 |
+
await msg.edit(view=None)
|
| 152 |
+
# Step B: attach the new persistent view (with stable custom_id)
|
| 153 |
+
await msg.edit(view=view)
|
| 154 |
|
| 155 |
+
# fetch and print the custom_ids currently on the message
|
| 156 |
+
msg2 = await channel.fetch_message(msg.id)
|
| 157 |
+
try:
|
| 158 |
+
current_ids = []
|
| 159 |
+
for action_row in msg2.components:
|
| 160 |
+
for comp in action_row.children:
|
| 161 |
+
current_ids.append(getattr(comp, "custom_id", None))
|
| 162 |
+
logging.info("Message components now: %s", current_ids)
|
| 163 |
+
except Exception:
|
| 164 |
+
logging.exception("Could not introspect message components")
|
| 165 |
+
|
| 166 |
+
logging.info("------------------------------------------------------------------------")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
|
| 169 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
|
|
|
| 179 |
def verify_button(profile: gr.OAuthProfile | None, request: gr.Request) -> str:
|
| 180 |
query_params = parse_qs(urlparse(str(request.url)).query)
|
| 181 |
user_id = query_params.get('user_id', [None])[0]
|
|
|
|
| 182 |
if not user_id:
|
| 183 |
return "# ❌ Missing Discord user ID."
|
|
|
|
| 184 |
if profile is None:
|
| 185 |
return "# ❌ You're not logged in with Hugging Face."
|
| 186 |
|
| 187 |
async def upgrade_role():
|
| 188 |
+
await bot.wait_until_ready()
|
| 189 |
guild = bot.get_guild(879548962464493619)
|
| 190 |
+
if not guild:
|
| 191 |
+
logging.error("Guild not ready")
|
| 192 |
+
return
|
| 193 |
|
| 194 |
+
try:
|
| 195 |
+
member = await guild.fetch_member(int(user_id))
|
| 196 |
+
except discord.NotFound:
|
| 197 |
+
logging.error("User %s not found in guild", user_id)
|
| 198 |
return
|
| 199 |
|
| 200 |
+
pending_role = guild.get_role(1380908157475360899)
|
| 201 |
+
verified_role = guild.get_role(900063512829755413)
|
| 202 |
+
if not verified_role:
|
| 203 |
+
logging.error("Verified role missing")
|
| 204 |
+
return
|
| 205 |
+
|
| 206 |
+
# remove pending if present
|
| 207 |
+
if pending_role and pending_role in member.roles:
|
| 208 |
+
try:
|
| 209 |
+
await member.remove_roles(pending_role, reason="Verification complete")
|
| 210 |
+
except Exception:
|
| 211 |
+
logging.exception("Remove pending failed")
|
| 212 |
+
|
| 213 |
+
# always try to add verified
|
| 214 |
+
if verified_role not in member.roles:
|
| 215 |
+
try:
|
| 216 |
+
await member.add_roles(verified_role, reason=f"HF verified {profile.username}")
|
| 217 |
+
logging.info("Added Verified to %s", member)
|
| 218 |
+
except discord.Forbidden:
|
| 219 |
+
logging.error("Forbidden adding Verified (perm/hierarchy)")
|
| 220 |
+
except Exception:
|
| 221 |
+
logging.exception("Add Verified failed")
|
| 222 |
|
|
|
|
| 223 |
if discord_loop:
|
| 224 |
asyncio.run_coroutine_threadsafe(upgrade_role(), discord_loop)
|
| 225 |
return f"# ✅ Verification successful! Welcome, {profile.username} 🎉"
|