1
1
# Imports
2
2
import discord
3
+ import random
4
+ import json
3
5
from discord import option , ApplicationContext
4
6
from discord .commands import SlashCommandGroup
5
7
from discord .ext import commands
12
14
class ServerConfig (commands .Cog ):
13
15
def __init__ (self , bot ):
14
16
self .bot = bot
17
+
18
+ # Load Verification Database
19
+ with open ("database/serververification.json" , 'r' , encoding = "utf-8" ) as f :
20
+ self .verification_db : dict = json .load (f )
15
21
16
22
serverconfig_cmds = SlashCommandGroup (name = "serverconfig" , description = "Commands related to server customization and configuration." )
17
23
@@ -65,6 +71,98 @@ async def autorole(self, ctx: ApplicationContext, channel: discord.TextChannel =
65
71
)
66
72
await ctx .respond (embed = localembed )
67
73
74
+ # Server Member Verification System
75
+ @serverconfig_cmds .command (
76
+ name = "enable_verification" ,
77
+ description = "Enable new member verification for this server."
78
+ )
79
+ @option (name = "verified_role" , description = "The role to provide to all verified members." , type = discord .Role )
80
+ async def enable_verification (self , ctx : ApplicationContext , verified_role : discord .Role ):
81
+ """Enable new user verification for this server."""
82
+ if not ctx .author .guild_permissions .administrator :
83
+ return await ctx .respond ("You can't use this command! You need the `Administrator` permission to run this." , ephemeral = True )
84
+ serverconf .set_verification_role (ctx .guild .id , verified_role .id )
85
+ localembed = discord .Embed (
86
+ title = f":white_check_mark: Server Member Verification successfully enabled for **{ ctx .guild .name } **!" ,
87
+ description = f"From now onwards, all new members will have to verify with `/verify` command, and will receive the { verified_role .mention } once verified." ,
88
+ color = discord .Color .green ()
89
+ )
90
+ await ctx .respond (embed = localembed )
91
+
92
+ @serverconfig_cmds .command (
93
+ name = "disable_verification" ,
94
+ description = "Disable new member verification for this server."
95
+ )
96
+ async def disable_verification (self , ctx : ApplicationContext ):
97
+ """Disable new member verification for this server."""
98
+ if not ctx .author .guild_permissions .administrator :
99
+ return await ctx .respond ("You can't use this command! You need the `Administrator` permission to run this." , ephemeral = True )
100
+ serverconf .set_verification_role (ctx .guild .id , None )
101
+ localembed = discord .Embed (
102
+ title = f":white_check_mark: Server Member Verification successfully disabled for **{ ctx .guild .name } **" ,
103
+ description = f"New members now won't have to verify in the server." ,
104
+ color = discord .Color .green ()
105
+ )
106
+ await ctx .respond (embed = localembed )
107
+
108
+ @commands .slash_command (
109
+ name = "start_verification" ,
110
+ description = "Start your verification process in this server."
111
+ )
112
+ @commands .guild_only ()
113
+ async def start_verification (self , ctx : ApplicationContext ):
114
+ """Start your verification process in this server."""
115
+ verification_role = serverconf .fetch_verification_role (ctx .guild .id )
116
+ if verification_role is None :
117
+ return await ctx .respond (":warning: Verification system is disabled for this server!" , ephemeral = True )
118
+ if ctx .author .get_role (verification_role ) is not None :
119
+ return await ctx .respond (":warning: You are already verified in this server!" , ephemeral = True )
120
+
121
+ # Construct verification data
122
+ verify_code = random .randint (100000 , 999999 )
123
+ if str (ctx .author .id ) not in self .verification_db :
124
+ self .verification_db [str (ctx .author .id )] = {}
125
+
126
+ for code in self .verification_db [str (ctx .author .id )]:
127
+ if self .verification_db [str (ctx .author .id )][str (code )]["guild_id" ] == ctx .guild .id :
128
+ return await ctx .respond ("Your verification process is already ongoing in this server!" , ephemeral = True )
129
+
130
+ self .verification_db [str (ctx .author .id )][str (verify_code )] = {"guild_id" : ctx .guild .id }
131
+ with open ("database/serververification.json" , 'w+' , encoding = "utf-8" ) as f :
132
+ json .dump (self .verification_db , f , indent = 4 )
133
+
134
+ localembed = discord .Embed (
135
+ title = f"Verification for { ctx .author .name } in { ctx .guild .name } has started" ,
136
+ description = f"Your one-time verification code is `{ verify_code } `. **DO NOT share this code with anyone!**\n \n Go to isobot's DMs, and run the `/verify` command entering your verification code." ,
137
+ color = discord .Color .orange ()
138
+ )
139
+ await ctx .respond (embed = localembed , ephemeral = True )
140
+
141
+ @commands .slash_command (
142
+ name = "verify" ,
143
+ description = "Enter your one-time verification code to verify membership in a server. (DM-ONLY)"
144
+ )
145
+ @commands .dm_only ()
146
+ @option (name = "verification_code" , description = "Your one-time verification code. (6-digit number)" , type = int )
147
+ async def verify (self , ctx : ApplicationContext , verification_code : int ):
148
+ """Enter your one-time verification code to verify membership in a server."""
149
+ if str (ctx .author .id ) not in self .verification_db .keys ():
150
+ return await ctx .respond ("You are not pending verification in any servers." , ephemeral = True )
151
+ if str (verification_code ) not in self .verification_db [str (ctx .author .id )].keys ():
152
+ return await ctx .respond (":x: This verification code is invalid. Please double-check and try a different code!" , ephemeral = True )
153
+
154
+ verification_role_id = serverconf .fetch_verification_role (self .verification_db [str (ctx .author .id )][str (verification_code )]["guild_id" ])
155
+ vcode_guild : discord .Guild = self .bot .get_guild (self .verification_db [str (ctx .author .id )][str (verification_code )]["guild_id" ])
156
+ verification_role = discord .Guild .get_role (vcode_guild , verification_role_id )
157
+ server_context_user : discord .Member = vcode_guild .get_member (ctx .author .id )
158
+ await server_context_user .add_roles (verification_role , reason = "Member has been successfully verified in server." )
159
+
160
+ del self .verification_db [str (ctx .author .id )][str (verification_code )]
161
+ with open ("database/serververification.json" , 'w+' , encoding = "utf-8" ) as f :
162
+ json .dump (self .verification_db , f , indent = 4 )
163
+
164
+ return await ctx .respond (f"You have been successfully verified in **{ vcode_guild .name } **!" )
165
+
68
166
def setup (bot ):
69
167
bot .add_cog (ServerConfig (bot ))
70
168
0 commit comments