Exemple #1
0
    async def verify(self, ctx, user_token=None):

        # If not DM, clear the message so that the token isn't kept publicly.
        if not isinstance(ctx.channel, DMChannel):
            await ctx.message.delete()

            msg = await ctx.send(s_verify["error_not_dm"])
            time.sleep(15)
            await msg.delete()

            return

        # If no token sends message...
        if user_token == None:
            await ctx.send(s_verify["usage"])
            return
        
        # Otherwise, keeps going:
        else:
            cmdResult = ""

            data = await api_fetch(c_api_token, user_token)
            success = data["success"]

            # If the token isn't valid.
            if success == False:
                cmdResult = s_verify["token_not_found"]
                await ctx.send(cmdResult)
            else:
                ### Retrieves needed variables and updates DB if needed. ###

                server = self.bot.get_guild(id_guild)

                # If server ID is wrong.
                if server == None:
                    await ctx.send(s_verify["error"] + "ERROR: Server not found.")
                    return
                

                user = ctx.message.author
                member = server.get_member(user.id)

                # If user isn't a member.
                if member == None:
                    database.remove_user_by_discord_uid(ctx.author.id)
                    await ctx.send(s_verify["not_a_member"])
                    return


                dm_channel = await member.create_dm()

                result = database.get_user_by_thm_token(db, user_token)

                # If user is not in DB; add him.
                if len(result) == 0:
                    database.add_user(db, member.id, user_token)

                ### Updates roles for the user. ###
                await update(member, dm_channel, data)
    async def lookup(self, ctx, *arg):
        arg = ' '.join(arg)
        self.conn = database.connect_to_db()

        # Token-based search check
        if token_regex.match(arg):
            match_type = 'token'

            db_result = database.get_user_by_thm_token(self.conn, arg)
        else:
            # ID-based search checks
            if mention_regex.match(arg):
                match_type = 'mention'
                user_id = mention_regex.search(arg).group(1)
            elif user_id_regex.match(arg):
                match_type = 'user id'
                user_id = arg
            elif user_discrim_regex.match(arg):
                match_type = 'user#discrim'
                user = self.bot.get_guild(id_guild).get_member_named(arg)
                if user is None:
                    return await ctx.send(
                        f"Failed to find a user with that discriminator")
                user_id = user.id
            else:
                return await ctx.send(s_lookup["match_failed"])

            try:
                db_result = database.get_user_by_discord_uid(
                    self.conn, user_id)
            except:
                return await ctx.send(s_lookup["db_fetch_failed"])

        # Loops over the results (also handles multiple-row results if they occur)
        for row in db_result:
            u_id, u_token = row

            try:
                thm_user = get_user_by_token(u_token)
            except:
                return await ctx.send(s_lookup["thm_fetch_failed"])

            response = officialEmbed("Token match",
                                     footer=f"Matched with {match_type}")

            response.add_field(name="Discord mention", value=f"<@{u_id}>")
            response.add_field(name="Discord ID", value=u_id)
            response.add_field(name="THM username", value=thm_user["username"])
            response.add_field(name="THM profile",
                               value=thm_user_link.format(
                                   thm_user["username"]))
            response.add_field(name="THM token", value=u_token)

            await ctx.send(embed=response)
        if len(db_result) == 0:
            await ctx.send("No results.")
    async def verify(self, ctx, input_token=None):

        # If not DM, clear the message so that the token isn't kept publicly.
        if not isinstance(ctx.channel, DMChannel):
            await ctx.message.delete()

            msg = await ctx.send(s_verify["error_not_dm"])
            time.sleep(15)
            await msg.delete()

            return

        # If no token sends message...
        if input_token == None:
            await ctx.send(s_verify["usage"])
            return

        # Otherwise, keeps going:
        else:
            cmdResult = ""

            data = await api_fetch(c_api_token, input_token)
            success = data["success"]

            # If the token isn't valid.
            if success == False:
                cmdResult = s_verify["token_not_found"]
                await ctx.send(cmdResult)
            else:
                ### Verifies that the token is being use only once and that the discord account doesn't have multiple tokens. ###

                # If user already has got a token (the provided token and one found are different)
                user_tokens = database.get_user_by_discord_uid(
                    db, ctx.author.id)

                if len(user_tokens
                       ) > 0 and not user_tokens[0][1] == input_token:
                    cmdResult = s_verify["already_verified"]

                    await ctx.send(cmdResult)
                    return

                # If the token is already used
                token_accounts = database.get_user_by_thm_token(
                    db, input_token)

                if len(token_accounts) > 0 and not token_accounts[0][0] == str(
                        ctx.author.id):
                    cmdResult = s_verify["token_in_use"]

                    await ctx.send(cmdResult)
                    return

                ### Retrieves needed variables and updates DB if needed. ###

                # If server ID is wrong.
                server = self.bot.get_guild(id_guild)

                if server == None:
                    await ctx.send(s_verify["error"] +
                                   "ERROR: Server not found.")
                    return

                # If user isn't a member.
                user = ctx.message.author
                member = await server.fetch_member(user.id)

                if member == None:
                    database.remove_user_by_discord_uid(db, ctx.author.id)
                    await ctx.send(s_verify["not_a_member"])
                    return

                dm_channel = await member.create_dm()

                result = database.get_user_by_thm_token(db, input_token)

                # If user is not in DB; add him.
                if len(result) == 0:
                    database.add_user(db, member.id, input_token)

                ### Updates roles for the user. ###
                await update(member, dm_channel, data)