update verification process
This commit is contained in:
1 parent
65bbaff0cb
commit
744c4f47f9
5 files changed
+279
-29
No files matched your search
+124
-29
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
import time
|
||||
from collections import deque
|
||||
from datetime import datetime, timedelta, timezone
|
||||
@@ -7,7 +8,9 @@ from discord import app_commands
|
||||
from discord.ext import commands, tasks
|
||||
|
||||
import config
|
||||
import data
|
||||
import guildconf
|
||||
import onboarding
|
||||
|
||||
VERIFY_TIMEOUT = 24 * 3600
|
||||
REVIEW_TIMEOUT = 7 * 24 * 3600
|
||||
@@ -16,10 +19,7 @@ YOUNG_ACCOUNT = timedelta(days=30)
|
||||
SPIKE_JOINS = 5
|
||||
SPIKE_WINDOW = 60
|
||||
|
||||
PANEL_TEXT = (
|
||||
"**Welcome!**\n"
|
||||
"Press the button below to get access to the rest of the server."
|
||||
)
|
||||
RULES_EMOJI = "\u2705"
|
||||
|
||||
REVIEW_ACTIONS = {
|
||||
"approve": ("Approve", discord.ButtonStyle.success),
|
||||
@@ -51,9 +51,9 @@ class VerifyView(discord.ui.View):
|
||||
def __init__(self):
|
||||
super().__init__(timeout=None)
|
||||
|
||||
@discord.ui.button(label="Verify", style=discord.ButtonStyle.success,
|
||||
@discord.ui.button(label="Continue", style=discord.ButtonStyle.success,
|
||||
custom_id="verify:start")
|
||||
async def verify(self, interaction: discord.Interaction, button):
|
||||
async def start(self, interaction: discord.Interaction, button):
|
||||
await interaction.client.get_cog("Verification").handle_verify(interaction)
|
||||
|
||||
|
||||
@@ -138,7 +138,30 @@ class Verification(commands.Cog):
|
||||
if not member.bot and role not in member.roles:
|
||||
await self.track(member)
|
||||
|
||||
async def send_review(self, member, flags, joined_at):
|
||||
async def grant(self, member, reason):
|
||||
"""Gives the member their chosen roles plus Verified in one call."""
|
||||
guild = member.guild
|
||||
row = await self.bot.db.fetchone(
|
||||
"SELECT choices FROM pending_verifications WHERE user_id = ?", member.id)
|
||||
names = json.loads(row["choices"]) if row and row["choices"] else []
|
||||
by_name = {r.name: r for r in guild.roles}
|
||||
roles = [by_name[n] for n in names if n in by_name]
|
||||
roles.append(await self.verified_role(guild))
|
||||
await member.add_roles(*roles, reason=reason)
|
||||
|
||||
async def clear_rules_reaction(self, guild, user_id):
|
||||
channel_id = await guildconf.get_id(self.bot, guildconf.VERIFY_CHANNEL)
|
||||
message_id = await guildconf.get_id(self.bot, guildconf.VERIFY_RULES_MSG)
|
||||
channel = guild.get_channel(channel_id) if channel_id else None
|
||||
if channel is None or not message_id:
|
||||
return
|
||||
try:
|
||||
await channel.get_partial_message(message_id).remove_reaction(
|
||||
RULES_EMOJI, discord.Object(id=user_id))
|
||||
except discord.HTTPException:
|
||||
pass
|
||||
|
||||
async def send_review(self, member, flags, joined_at, choices):
|
||||
channel = await self.review_channel(member.guild)
|
||||
if channel is None:
|
||||
return False
|
||||
@@ -153,6 +176,8 @@ class Verification(commands.Cog):
|
||||
embed.add_field(name="Joined", value=f"<t:{joined_at}:R>")
|
||||
embed.add_field(
|
||||
name="Flags", value="\n".join(f"- {f}" for f in flags), inline=False)
|
||||
embed.add_field(
|
||||
name="Selected roles", value=", ".join(choices) or "None", inline=False)
|
||||
embed.set_footer(text=f"User ID: {member.id}")
|
||||
try:
|
||||
await channel.send(embed=embed, view=review_view(member.id))
|
||||
@@ -161,18 +186,17 @@ class Verification(commands.Cog):
|
||||
return True
|
||||
|
||||
async def handle_verify(self, interaction):
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
member, guild = interaction.user, interaction.guild
|
||||
role = await self.verified_role(guild)
|
||||
|
||||
async def reply(text):
|
||||
await interaction.followup.send(text, ephemeral=True)
|
||||
async def say(text):
|
||||
await interaction.response.send_message(text, ephemeral=True)
|
||||
|
||||
if role is None or not await self.enabled():
|
||||
await reply("Verification isn't active right now.")
|
||||
await say("Verification isn't active right now.")
|
||||
return
|
||||
if role in member.roles:
|
||||
await reply("You're already verified.")
|
||||
await say("You're already verified.")
|
||||
return
|
||||
|
||||
await self.track(member)
|
||||
@@ -180,13 +204,43 @@ class Verification(commands.Cog):
|
||||
"SELECT * FROM pending_verifications WHERE user_id = ?", member.id)
|
||||
|
||||
if row["in_review"]:
|
||||
await reply("A moderator is already reviewing your account. "
|
||||
"You'll get access once it's approved.")
|
||||
await say("A moderator is already reviewing your account. "
|
||||
"You'll get access once it's approved.")
|
||||
return
|
||||
if not row["rules_ok"]:
|
||||
await say(f"Please react with {RULES_EMOJI} on the rules message first. "
|
||||
"If it's already there, remove it, add it again, "
|
||||
"then press Continue.")
|
||||
return
|
||||
|
||||
missing = onboarding.missing_roles(guild)
|
||||
if missing:
|
||||
print(f"verify: missing roles: {missing[:10]}", flush=True)
|
||||
await say("Setup isn't finished yet. Please let a moderator know.")
|
||||
return
|
||||
|
||||
state = onboarding.State(self)
|
||||
await interaction.response.send_message(
|
||||
onboarding.PARTY_TEXT, view=onboarding.party_view(state), ephemeral=True)
|
||||
|
||||
async def complete(self, interaction, state):
|
||||
await interaction.response.edit_message(content="Finishing up...", view=None)
|
||||
member = interaction.user
|
||||
names = state.role_names()
|
||||
|
||||
async def reply(text):
|
||||
await interaction.edit_original_response(content=text, view=None)
|
||||
|
||||
await self.track(member)
|
||||
await self.bot.db.execute(
|
||||
"UPDATE pending_verifications SET choices = ? WHERE user_id = ?",
|
||||
json.dumps(names), member.id)
|
||||
row = await self.bot.db.fetchone(
|
||||
"SELECT * FROM pending_verifications WHERE user_id = ?", member.id)
|
||||
|
||||
if row["flags"]:
|
||||
sent = await self.send_review(
|
||||
member, row["flags"].split(";"), row["joined_at"])
|
||||
member, row["flags"].split(";"), row["joined_at"], names)
|
||||
if not sent:
|
||||
await reply("Verification is temporarily unavailable. "
|
||||
"Please try again later.")
|
||||
@@ -200,13 +254,14 @@ class Verification(commands.Cog):
|
||||
return
|
||||
|
||||
try:
|
||||
await member.add_roles(role, reason="Passed verification")
|
||||
except discord.HTTPException as e:
|
||||
print(f"verify: could not add role: {e}", flush=True)
|
||||
await self.grant(member, "Passed verification")
|
||||
except (discord.HTTPException, AttributeError) as e:
|
||||
print(f"verify: could not grant roles: {e}", flush=True)
|
||||
await reply("Something went wrong. Please contact a moderator.")
|
||||
return
|
||||
await self.forget(member.id)
|
||||
await reply("You're verified. Welcome!")
|
||||
await reply("You're verified. Welcome! You can change your roles "
|
||||
"later from the role panels.")
|
||||
|
||||
async def handle_review(self, interaction, action, user_id):
|
||||
check = guildconf.is_admin if action == "ban" else guildconf.is_mod
|
||||
@@ -218,7 +273,6 @@ class Verification(commands.Cog):
|
||||
await interaction.response.defer()
|
||||
guild = interaction.guild
|
||||
member = guild.get_member(user_id)
|
||||
role = await self.verified_role(guild)
|
||||
by = interaction.user
|
||||
reason = f"Verification review by {by}"
|
||||
|
||||
@@ -228,7 +282,7 @@ class Verification(commands.Cog):
|
||||
else:
|
||||
try:
|
||||
if action == "approve":
|
||||
await member.add_roles(role, reason=reason)
|
||||
await self.grant(member, reason)
|
||||
outcome, colour = "Approved", discord.Colour.green()
|
||||
elif action == "kick":
|
||||
await dm(member, f"You were removed from {guild.name} after a "
|
||||
@@ -247,7 +301,8 @@ class Verification(commands.Cog):
|
||||
except (discord.Forbidden, AttributeError):
|
||||
await interaction.followup.send(
|
||||
"I couldn't do that. Check the Verified role exists and that "
|
||||
"my role is above the member's.", ephemeral=True)
|
||||
"my role is above the member's and above the roles they chose.",
|
||||
ephemeral=True)
|
||||
return
|
||||
await self.forget(user_id)
|
||||
|
||||
@@ -278,6 +333,27 @@ class Verification(commands.Cog):
|
||||
async def on_member_remove(self, member):
|
||||
if member.guild.id == config.GUILD.id:
|
||||
await self.forget(member.id)
|
||||
await self.clear_rules_reaction(member.guild, member.id)
|
||||
|
||||
async def rules_reaction(self, payload, value):
|
||||
if (payload.guild_id != config.GUILD.id
|
||||
or payload.user_id == self.bot.user.id
|
||||
or str(payload.emoji) != RULES_EMOJI):
|
||||
return
|
||||
message_id = await guildconf.get_id(self.bot, guildconf.VERIFY_RULES_MSG)
|
||||
if payload.message_id != message_id:
|
||||
return
|
||||
await self.bot.db.execute(
|
||||
"UPDATE pending_verifications SET rules_ok = ? WHERE user_id = ?",
|
||||
value, payload.user_id)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_raw_reaction_add(self, payload):
|
||||
await self.rules_reaction(payload, 1)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_raw_reaction_remove(self, payload):
|
||||
await self.rules_reaction(payload, 0)
|
||||
|
||||
@tasks.loop(minutes=1)
|
||||
async def sweep(self):
|
||||
@@ -328,6 +404,7 @@ class Verification(commands.Cog):
|
||||
or discord.utils.get(guild.roles, name="Verified"))
|
||||
reason = "Verification setup"
|
||||
|
||||
step = "creating the Verified role"
|
||||
try:
|
||||
if role is None:
|
||||
role = await guild.create_role(
|
||||
@@ -335,26 +412,33 @@ class Verification(commands.Cog):
|
||||
permissions=discord.Permissions(view_channel=True),
|
||||
reason=reason)
|
||||
elif not role.permissions.view_channel:
|
||||
step = "editing the Verified role"
|
||||
perms = role.permissions
|
||||
perms.view_channel = True
|
||||
await role.edit(permissions=perms, reason=reason)
|
||||
|
||||
step = f"setting permissions for @everyone in {channel.mention}"
|
||||
await channel.set_permissions(
|
||||
guild.default_role, reason=reason,
|
||||
overwrite=discord.PermissionOverwrite(
|
||||
view_channel=True, read_message_history=True,
|
||||
send_messages=False, add_reactions=False))
|
||||
add_reactions=True, send_messages=False))
|
||||
|
||||
step = f"setting permissions for Verified in {channel.mention}"
|
||||
await channel.set_permissions(
|
||||
role, reason=reason,
|
||||
overwrite=discord.PermissionOverwrite(view_channel=False))
|
||||
|
||||
step = f"setting permissions for the bot in {channel.mention}"
|
||||
await channel.set_permissions(
|
||||
guild.me, reason=reason,
|
||||
overwrite=discord.PermissionOverwrite(
|
||||
view_channel=True, send_messages=True, embed_links=True))
|
||||
except discord.Forbidden:
|
||||
view_channel=True, send_messages=True, embed_links=True,
|
||||
add_reactions=True, read_message_history=True,
|
||||
manage_messages=True))
|
||||
except discord.Forbidden as e:
|
||||
await interaction.followup.send(
|
||||
"I need Manage Roles and Manage Channels, and my role must be "
|
||||
"above the Verified role.", ephemeral=True)
|
||||
f"Discord refused while {step}: {e.text}", ephemeral=True)
|
||||
return
|
||||
|
||||
db = self.bot.db
|
||||
@@ -369,7 +453,7 @@ class Verification(commands.Cog):
|
||||
ephemeral=True)
|
||||
|
||||
@group.command(name="panel",
|
||||
description="Post the verification button in the verify channel")
|
||||
description="Post the rules and Continue button in the verify channel")
|
||||
@guildconf.admin_only()
|
||||
async def panel(self, interaction: discord.Interaction):
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
@@ -378,7 +462,13 @@ class Verification(commands.Cog):
|
||||
if channel is None:
|
||||
await interaction.followup.send("Run `/verify setup` first.", ephemeral=True)
|
||||
return
|
||||
await channel.send(content=PANEL_TEXT, view=VerifyView())
|
||||
message = await channel.send(
|
||||
embed=discord.Embed(
|
||||
description=data.RULES_TEXT,
|
||||
colour=discord.Colour(config.LIGHT_BLUE)),
|
||||
view=VerifyView())
|
||||
await message.add_reaction(RULES_EMOJI)
|
||||
await self.bot.db.set_setting(guildconf.VERIFY_RULES_MSG, message.id)
|
||||
await interaction.followup.send(f"Posted in {channel.mention}.", ephemeral=True)
|
||||
|
||||
@group.command(name="grandfather",
|
||||
@@ -412,14 +502,19 @@ class Verification(commands.Cog):
|
||||
guild = interaction.guild
|
||||
role = await self.verified_role(guild)
|
||||
channel_id = await guildconf.get_id(self.bot, guildconf.VERIFY_CHANNEL)
|
||||
rules_id = await guildconf.get_id(self.bot, guildconf.VERIFY_RULES_MSG)
|
||||
|
||||
problem = None
|
||||
if role is None or not channel_id or guild.get_channel(channel_id) is None:
|
||||
problem = "Run `/verify setup` first."
|
||||
elif not rules_id:
|
||||
problem = "Run `/verify panel` first."
|
||||
elif await self.review_channel(guild) is None:
|
||||
problem = "Set a mod log (`/config modlog`) or a review channel first."
|
||||
elif not guild.me.guild_permissions.view_channel:
|
||||
problem = "My role needs the View Channels permission."
|
||||
elif onboarding.missing_roles(guild):
|
||||
problem = "Some self-assign roles are missing. Run `/setup_roles` first."
|
||||
else:
|
||||
missing = [m for m in guild.members if role not in m.roles]
|
||||
if missing:
|
||||
|
||||
Reference in new issue
Block a user